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 " 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); + } +} diff --git a/app/library/Services.php b/app/library/Services.php index c0b0fc3..a1485fd 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, @@ -381,9 +382,18 @@ class Services extends DiDefault return $view; } - protected function _initSharedSendgrid() + /** + * Register the mail service. + * + * @return MailService + */ + protected function _initSharedMail() { - return new \SendGrid($this->get('config')->sendgrid->key); + $config = $this->get('config'); + + $sendgrid = new \SendGrid($config->sendgrid->key); + + return new MailService($sendgrid); } protected function _initOauth($provider)