From 6847312778b6776e93ad126ff90d55be5ba7ea64 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 22 Sep 2018 13:51:53 +0200 Subject: [PATCH 1/4] adding app/library/Mail.php --- app/library/Mail.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app/library/Mail.php diff --git a/app/library/Mail.php b/app/library/Mail.php new file mode 100644 index 0000000..97a8c83 --- /dev/null +++ b/app/library/Mail.php @@ -0,0 +1,38 @@ +_sendgrid = $sendgrid; + } + + /** + * Send an email. + * + * @param string $subject + * @param string $to_address + * @param string $body + * @param string $content_type + */ + public function send($subject, $to_address, $body, $content_type = 'text/html') + { + $mail = new SendGridMail(); + $mail->setFrom('noreply@shufflingpixels.com'); + $mail->setSubject($subject); + $mail->addTo($to_address); + $mail->addContent($content_type, $body); + + $this->_sendgrid->send($mail); + } +} From 5c7145fd9c7f71cd8ccdcd2809ec7328fc716f54 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 22 Sep 2018 13:56:41 +0200 Subject: [PATCH 2/4] app/library/Services.php: adding Mail service. --- app/library/Services.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/library/Services.php b/app/library/Services.php index c0b0fc3..0cbf5a7 100644 --- a/app/library/Services.php +++ b/app/library/Services.php @@ -27,6 +27,7 @@ use Httpcb\Auth, Httpcb\Acl, Httpcb\Navigation, Httpcb\Menu, + Httpcb\Mail as MailService, Httpcb\ViewHelper\Volt\Extension as ViewHelperVoltExtension; use App\Listener\AclListener, @@ -386,6 +387,20 @@ class Services extends DiDefault return new \SendGrid($this->get('config')->sendgrid->key); } + /** + * Register the mail service. + * + * @return MailService + */ + protected function _initSharedMail() + { + $config = $this->get('config'); + + $sendgrid = new \SendGrid($config->sendgrid->key); + + return new MailService($sendgrid); + } + protected function _initOauth($provider) { $config = $this->get('config'); From 9d6e17e646e3b8a3b03896a25ec38bbd231bd99d Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 22 Sep 2018 14:03:33 +0200 Subject: [PATCH 3/4] app/library/Services.php: remove sendgrid service (defined as mail from now on). --- app/library/Services.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/library/Services.php b/app/library/Services.php index 0cbf5a7..a1485fd 100644 --- a/app/library/Services.php +++ b/app/library/Services.php @@ -382,11 +382,6 @@ class Services extends DiDefault return $view; } - protected function _initSharedSendgrid() - { - return new \SendGrid($this->get('config')->sendgrid->key); - } - /** * Register the mail service. * From 2929c640f0b495d6d80f50fe2f2248cbdf31dac8 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 22 Sep 2018 14:03:56 +0200 Subject: [PATCH 4/4] app/controllers/UserController.php: remove usage of SendGrid and use the mail service. --- app/controllers/UserController.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 09bab32..0d03705 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -6,8 +6,7 @@ use App\Controller\ControllerBase, App\Form\UserSettings as UserSettingsForm, App\Model\Data\ActivityLog, App\Model\Data\PasswordLink, - App\Model\Data\User, - SendGrid\Mail\Mail as SendGridMail; + App\Model\Data\User; class UserController extends ControllerBase { @@ -45,19 +44,15 @@ class UserController extends ControllerBase ->setPassword($hash) ->save(); + // Render the email content. $tpl = $this->di->get('template'); - $body = $tpl->render('mail/password_activation', [ + $content = $tpl->render('mail/password_activation', [ 'link' => $link->getPublicId() ]); - $mail = new SendGridMail(); - $mail->setFrom('noreply@shufflingpixels.com'); - $mail->setSubject('Httpcb password activation'); - $mail->addTo($user->getEmail()); - $mail->addContent('text/html', $body); - - $sendgrid = $this->di->get('sendgrid'); - $sendgrid->send($mail); + // Send the email. + $this->di->getMail()->send('Httpcb password activation', + $user->getEmail(), $content); $msg = "For security reasons. Before a password can be created " . "a email has been sent to {$user->getEmail()} with "