From 7ce895bf5e0531b96b2cf0692b9da3814fb742f8 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 10 Jan 2016 12:49:43 +0100 Subject: Use HTML email for 2FA Related to #1490 --- src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php | 47 +++++++++++++---------- 1 file changed, 27 insertions(+), 20 deletions(-) (limited to 'src/Wallabag/UserBundle/Mailer') diff --git a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php index be370e71..c04720ae 100644 --- a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php +++ b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php @@ -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); -- cgit v1.2.3 From d1f1333f481a6814bb310c934b798d9f594f0b04 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 10 Jan 2016 13:01:00 +0100 Subject: CS --- src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'src/Wallabag/UserBundle/Mailer') diff --git a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php index c04720ae..fa682ae7 100644 --- a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php +++ b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php @@ -4,7 +4,6 @@ namespace Wallabag\UserBundle\Mailer; use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface; -use Symfony\Component\Translation\TranslatorInterface; /** * Custom mailer for TwoFactorBundle email. @@ -50,11 +49,11 @@ class AuthCodeMailer implements AuthCodeMailerInterface /** * Initialize the auth code mailer with the SwiftMailer object. * - * @param \Swift_Mailer $mailer - * @param \Twig_Environment $twig - * @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, \Twig_Environment $twig, $senderEmail, $senderName, $supportUrl) { @@ -74,7 +73,7 @@ class AuthCodeMailer implements AuthCodeMailerInterface { $template = $this->twig->loadTemplate('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig'); - $subject = $template->renderBlock('subject', array()); + $subject = $template->renderBlock('subject', array()); $bodyHtml = $template->renderBlock('body_html', [ 'user' => $user->getName(), 'code' => $user->getEmailAuthCode(), -- cgit v1.2.3 From abd454c45633e335d9d63bcd250e760b968f8138 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 20 Jan 2016 17:43:10 +0100 Subject: Update HTML 2FA template Split paragraph in translation Inject wallabag_url for image in HTML template Remove username & password from config_dev.yml (null are already the default value) --- src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/UserBundle/Mailer') diff --git a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php index fa682ae7..6b108dd3 100644 --- a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php +++ b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php @@ -46,6 +46,13 @@ class AuthCodeMailer implements AuthCodeMailerInterface */ private $supportUrl; + /** + * Url for the wallabag instance. + * + * @var string + */ + private $wallabagUrl; + /** * Initialize the auth code mailer with the SwiftMailer object. * @@ -54,14 +61,16 @@ class AuthCodeMailer implements AuthCodeMailerInterface * @param string $senderEmail * @param string $senderName * @param string $supportUrl + * @param string $wallabagUrl */ - public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, $supportUrl) + public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, $supportUrl, $wallabagUrl) { $this->mailer = $mailer; $this->twig = $twig; $this->senderEmail = $senderEmail; $this->senderName = $senderName; $this->supportUrl = $supportUrl; + $this->wallabagUrl = $wallabagUrl; } /** @@ -77,12 +86,13 @@ class AuthCodeMailer implements AuthCodeMailerInterface $bodyHtml = $template->renderBlock('body_html', [ 'user' => $user->getName(), 'code' => $user->getEmailAuthCode(), - 'support' => $this->supportUrl, + 'support_url' => $this->supportUrl, + 'wallabag_url' => $this->wallabagUrl, ]); $bodyText = $template->renderBlock('body_text', [ 'user' => $user->getName(), 'code' => $user->getEmailAuthCode(), - 'support' => $this->supportUrl, + 'support_url' => $this->supportUrl, ]); $message = new \Swift_Message(); -- cgit v1.2.3