Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Email Service #36

Merged
merged 15 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"nesbot/carbon": "^2.16"
"nesbot/carbon": "^2.16",
"phpmailer/phpmailer": "^6.2"
}
}
}
2 changes: 1 addition & 1 deletion src/Console/Kernel.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Kernel

public function __construct(ApplicationLoader $applicationLoader, ServiceManager $serviceManager, ConsoleApplication $console)
{
$applicationLoader->loadOnly(['elements', 'services']);
$applicationLoader->loadOnly(['elements', 'services', 'emails']);

$serviceManager->processServiceSuppliers(true);

Expand Down
92 changes: 92 additions & 0 deletions src/Email/Email.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Polyel\Email;

use Polyel\View\ViewTools;

abstract class Email
{
use ViewTools;

public string $fromName;

public string $subject;

public string $message;

public bool $usingHTML = false;

abstract public function setFromName();

abstract public function setSubject();

abstract public function setMessage();

protected function name(string $fromName)
{
$this->fromName = $fromName;

return $this;
}

protected function subject(string $subject)
{
$this->subject = $subject;

return $this;
}

protected function text(string $message)
{
$this->message = $message;

return $this;
}

protected function html(string $message)
{
$this->message = $message;

$this->usingHTML = true;

return $this;
}

protected function view(string $template)
{
// Replace any dot syntax within the template name
$template = str_replace(".", "/", $template);
$template = APP_DIR . '/resources/' . $template . '.html';

// Check if the email template exists first...
if(file_exists($template))
{
$emailTemplateContent = file_get_contents($template);

$emailTemplateVars = $this->getStringsBetween(
$emailTemplateContent,
'{{', '}}'
);

// Get publicly accessible class properties which are used for passing in view data
$emailViewData = get_object_vars($this);

// Loop through each view key and replace any that are found
foreach($emailViewData as $viewKey => $viewData)
{
/*
* The tag is checked if it is there within this function,
* before injecting any data, that is why the $emailTemplateVars
* are passed in. XSS filtering also takes place inside this
* function as well.
*/
$this->replaceTag($viewKey, $viewData, $emailTemplateVars, $emailTemplateContent);
}

// Set the email body to a HTML message
$this->html($emailTemplateContent);
}

return $this;
}
}
96 changes: 96 additions & 0 deletions src/Email/EmailManager.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Polyel\Email;

use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class EmailManager
{
public function __construct()
{

}

private function createNewMailer()
{
$mailer = new PHPMailer(true);

$mailer->isSMTP();
$mailer->SMTPAuth = true;

return $mailer;
}

private function setMailerEncryption(string $from, $mailer)
{
$encryptionTypes = [
'starttls' => PHPMailer::ENCRYPTION_STARTTLS,
'smtps' => PHPMailer::ENCRYPTION_SMTPS,
];

$mailer->SMTPSecure = $encryptionTypes[config("email.senders.$from.encryption")];

return $mailer;
}

private function setSmtpHost(string $from, $mailer)
{
$mailer->Host = config("email.senders.$from.host");

return $mailer;
}

private function setSmtpPort(string $from, $mailer)
{
$mailer->Port = (int)config("email.senders.$from.port");

return $mailer;
}

private function setSmtpAuthCredentials(string $from, $mailer)
{
$mailer->Username = config("email.senders.$from.username");
$mailer->Password = config("email.senders.$from.password");

return $mailer;
}

public function send(string $to, string $from, array $ccs, array $bccs, Email $email)
{
if(empty($from))
{
$from = config('email.default');
}

$mailer = $this->createNewMailer();

$mailer = $this->setMailerEncryption($from, $mailer);
$mailer = $this->setSmtpHost($from, $mailer);
$mailer = $this->setSmtpPort($from, $mailer);
$mailer = $this->setSmtpAuthCredentials($from, $mailer);

$mailer->setFrom($mailer->Username, $email->fromName);

$mailer->addAddress($to);

foreach($ccs as $cc)
{
$mailer->addCC($cc);
}

foreach($bccs as $bcc)
{
$mailer->addBCC($bcc);
}

$mailer->isHTML($email->usingHTML);

$mailer->Subject = $email->subject;

$mailer->Body = $email->message;

$mailer->send();
}
}
13 changes: 13 additions & 0 deletions src/Email/Facade/SendEmail.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Polyel\Email\Facade;

use Polyel;

class SendEmail
{
public static function __callStatic($method, $arguments)
{
return Polyel::new(Polyel\Email\SendEmail::class)->$method(...$arguments);
}
}
68 changes: 68 additions & 0 deletions src/Email/SendEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Polyel\Email;

class SendEmail
{
private EmailManager $emailManager;

private string $to;

private string $from = '';

private array $ccs = [];

private array $bccs = [];

public function __construct(EmailManager $emailManager)
{
$this->emailManager = $emailManager;
}

public function from(string $from)
{
$this->from = $from;

return $this;
}

public function to(string $to)
{
$this->to = $to;

return $this;
}

public function cc(string $cc)
{
$this->ccs[] = $cc;

return $this;
}

public function bcc(string $bcc)
{
$this->bccs[] = $bcc;

return $this;
}

public function send(Email $email)
{
defer(function() use ($email)
{
$email
->setFromName()
->setSubject()
->setMessage();

$this->emailManager->send(
$this->to,
$this->from,
$this->ccs,
$this->bccs,
$email
);
});
}
}
11 changes: 11 additions & 0 deletions src/System/ApplicationLoader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function load()
$this->loadServices();
$this->loadMiddleware();
$this->loadControllers();
$this->loadEmailClasses();
}

public function loadOnly(array $inclusions)
Expand All @@ -42,6 +43,11 @@ public function loadOnly(array $inclusions)
{
$this->loadControllers();
}

if(in_array('emails', $inclusions, true))
{
$this->loadEmailClasses();
}
}

private function loadDirectory($directory)
Expand Down Expand Up @@ -80,6 +86,11 @@ private function loadElementLogicClasses()
$this->loadDirectory(APP_DIR . '/app/View/Elements/');
}

private function loadEmailClasses()
{
$this->loadDirectory(APP_DIR . '/app/Email/');
}

public function loadThirdPartyPackages()
{
$composerClassmapFileLocation = APP_DIR . '/vendor/composer/autoload_classmap.php';
Expand Down