Archived
1
0
Fork 0

Merge branch '27-di-mail-service' into 'dev'

Resolve "DI: Mail service"

Closes #27

See merge request pnx/httpcb!23
This commit is contained in:
Henrik Hautakoski 2018-09-25 22:24:53 +00:00
commit f2e9a7e899
3 changed files with 56 additions and 13 deletions

View file

@ -6,8 +6,7 @@ use App\Controller\ControllerBase,
App\Form\UserSettings as UserSettingsForm, App\Form\UserSettings as UserSettingsForm,
App\Model\Data\ActivityLog, App\Model\Data\ActivityLog,
App\Model\Data\PasswordLink, App\Model\Data\PasswordLink,
App\Model\Data\User, App\Model\Data\User;
SendGrid\Mail\Mail as SendGridMail;
class UserController extends ControllerBase class UserController extends ControllerBase
{ {
@ -45,19 +44,15 @@ class UserController extends ControllerBase
->setPassword($hash) ->setPassword($hash)
->save(); ->save();
// Render the email content.
$tpl = $this->di->get('template'); $tpl = $this->di->get('template');
$body = $tpl->render('mail/password_activation', [ $content = $tpl->render('mail/password_activation', [
'link' => $link->getPublicId() 'link' => $link->getPublicId()
]); ]);
$mail = new SendGridMail(); // Send the email.
$mail->setFrom('noreply@shufflingpixels.com'); $this->di->getMail()->send('Httpcb password activation',
$mail->setSubject('Httpcb password activation'); $user->getEmail(), $content);
$mail->addTo($user->getEmail());
$mail->addContent('text/html', $body);
$sendgrid = $this->di->get('sendgrid');
$sendgrid->send($mail);
$msg = "For security reasons. Before a password can be created " $msg = "For security reasons. Before a password can be created "
. "a email has been sent to <strong>{$user->getEmail()}</strong> with " . "a email has been sent to <strong>{$user->getEmail()}</strong> with "

38
app/library/Mail.php Normal file
View file

@ -0,0 +1,38 @@
<?php
namespace Httpcb;
use SendGrid\Mail\Mail as SendGridMail;
use SendGrid;
class Mail
{
/**
* @var SendGrid
*/
protected $_sendgrid;
public function __construct(SendGrid $sendgrid)
{
$this->_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);
}
}

View file

@ -27,6 +27,7 @@ use Httpcb\Auth,
Httpcb\Acl, Httpcb\Acl,
Httpcb\Navigation, Httpcb\Navigation,
Httpcb\Menu, Httpcb\Menu,
Httpcb\Mail as MailService,
Httpcb\ViewHelper\Volt\Extension as ViewHelperVoltExtension; Httpcb\ViewHelper\Volt\Extension as ViewHelperVoltExtension;
use App\Listener\AclListener, use App\Listener\AclListener,
@ -381,9 +382,18 @@ class Services extends DiDefault
return $view; 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) protected function _initOauth($provider)