1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-26 18:21:02 +00:00

Use HTML email for 2FA

Related to #1490
This commit is contained in:
Jeremy Benoist 2016-01-10 12:49:43 +01:00
parent e72a943ad2
commit 7ce895bf5e
6 changed files with 62 additions and 39 deletions

View file

@ -20,11 +20,11 @@ class AuthCodeMailer implements AuthCodeMailerInterface
private $mailer;
/**
* Translator for email content.
* Twig to render the html's email.
*
* @var TranslatorInterface
* @var \Twig_Environment
*/
private $translator;
private $twig;
/**
* Sender email address.
@ -50,16 +50,16 @@ class AuthCodeMailer implements AuthCodeMailerInterface
/**
* Initialize the auth code mailer with the SwiftMailer object.
*
* @param \Swift_Mailer $mailer
* @param TranslatorInterface $translator
* @param string $senderEmail
* @param string $senderName
* @param string $supportUrl
* @param \Swift_Mailer $mailer
* @param \Twig_Environment $twig
* @param string $senderEmail
* @param string $senderName
* @param string $supportUrl
*/
public function __construct(\Swift_Mailer $mailer, TranslatorInterface $translator, $senderEmail, $senderName, $supportUrl)
public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, $supportUrl)
{
$this->mailer = $mailer;
$this->translator = $translator;
$this->twig = $twig;
$this->senderEmail = $senderEmail;
$this->senderName = $senderName;
$this->supportUrl = $supportUrl;
@ -72,20 +72,27 @@ class AuthCodeMailer implements AuthCodeMailerInterface
*/
public function sendAuthCode(TwoFactorInterface $user)
{
$template = $this->twig->loadTemplate('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig');
$subject = $template->renderBlock('subject', array());
$bodyHtml = $template->renderBlock('body_html', [
'user' => $user->getName(),
'code' => $user->getEmailAuthCode(),
'support' => $this->supportUrl,
]);
$bodyText = $template->renderBlock('body_text', [
'user' => $user->getName(),
'code' => $user->getEmailAuthCode(),
'support' => $this->supportUrl,
]);
$message = new \Swift_Message();
$message
->setTo($user->getEmail())
->setFrom($this->senderEmail, $this->senderName)
->setSubject($this->translator->trans('auth_code.mailer.subject', array(), 'wallabag_user'))
->setBody($this->translator->trans(
'auth_code.mailer.body',
[
'%user%' => $user->getName(),
'%code%' => $user->getEmailAuthCode(),
'%support%' => $this->supportUrl,
],
'wallabag_user'
))
->setSubject($subject)
->setBody($bodyText, 'text/plain')
->addPart($bodyHtml, 'text/html')
;
$this->mailer->send($message);