]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Use HTML email for 2FA
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 10 Jan 2016 11:49:43 +0000 (12:49 +0100)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Thu, 21 Jan 2016 10:03:27 +0000 (11:03 +0100)
Related to #1490

src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php
src/Wallabag/UserBundle/Resources/config/services.yml
src/Wallabag/UserBundle/Resources/translations/wallabag_user.en.yml
src/Wallabag/UserBundle/Resources/translations/wallabag_user.fr.yml
src/Wallabag/UserBundle/Resources/views/TwoFactor/email_auth_code.html.twig [new file with mode: 0644]
src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php

index be370e71d98677fbb0e48940b18f9bf3868c56b6..c04720aeaa52700dcb352a0277f6df769aa34f54 100644 (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);
index 9109b6a378ab0c2dee6970f159055a38fda8f494..aa2fd8b98480e6bf31718c57de7c214e35cd9670 100644 (file)
@@ -3,7 +3,7 @@ services:
         class: Wallabag\UserBundle\Mailer\AuthCodeMailer
         arguments:
             - "@mailer"
-            - "@translator"
+            - "@twig"
             - "%scheb_two_factor.email.sender_email%"
             - "%scheb_two_factor.email.sender_name%"
             - "%wallabag_support_url%"
index f806d1d6f7ecfcde406a019c6362fb43bf53958b..7298bb28b52b398e7c624efb945dc0dd43a1cdaf 100644 (file)
@@ -1,10 +1,9 @@
 # Two factor mail
 auth_code.mailer.subject: 'Wallabag authentication Code'
-auth_code.mailer.body: |
-    Hi %user%,
-
+auth_code.mailer.body.hello: "Hi %user%,"
+auth_code.mailer.body.content: |
     Since you enable two factor authentication on your wallabag account and you just logged in from a new device (computer, phone, etc.), we send you a code to validate your connection.
     Here is the code: %code%
-
+auth_code.mailer.body.signature: |
     Please don't hesitate to contact us if you have any problems: %support%
     The wallabag team
index 386b2d9eb24757157c6f5cabf0a394bec2241466..0c492d044f3d7f47a93f1008cabb437f32d056c7 100644 (file)
@@ -1,10 +1,9 @@
 # Two factor mail
 auth_code.mailer.subject: "Code d'authentification wallabag"
-auth_code.mailer.body: |
-    Bonjour %user%,
-
+auth_code.mailer.body.hello: "Bonjour %user%,"
+auth_code.mailer.body.content: |
     Comme vous avez activé la double authentification sur votre compte wallabag et que vous venez de vous connecter depuis un nouvel appareil (ordinateur, téléphone, etc.), nous vous envoyons un code pour valider votre connexion.
     Voici le code à renseigner: %code%
-
+auth_code.mailer.body.signature: |
     Si vous avez un problème de connexion, n'hésitez pas à contacter le support: %support%
     L'équipe wallabag
diff --git a/src/Wallabag/UserBundle/Resources/views/TwoFactor/email_auth_code.html.twig b/src/Wallabag/UserBundle/Resources/views/TwoFactor/email_auth_code.html.twig
new file mode 100644 (file)
index 0000000..b726d14
--- /dev/null
@@ -0,0 +1,18 @@
+{% block subject %}
+{{ "auth_code.mailer.subject"|trans({}, 'wallabag_user') }}
+{% endblock %}
+
+{% block body_html %}
+<p><b>{{ "auth_code.mailer.body.hello"|trans({'%user%': user}, 'wallabag_user') }}</b></p>
+
+<p>{{ "auth_code.mailer.body.content"|trans({'%code%': code}, 'wallabag_user') }}</p>
+
+<p>{{ "auth_code.mailer.body.signature"|trans({'%support%': support}, 'wallabag_user') }}</p>
+{% endblock %}
+
+{% block body_text %}
+{{ "auth_code.mailer.body.hello"|trans({'%user%': user}, 'wallabag_user') }}
+
+{{ "auth_code.mailer.body.content"|trans({'%code%': code}, 'wallabag_user') }}
+{{ "auth_code.mailer.body.signature"|trans({'%support%': support}, 'wallabag_user') }}
+{% endblock %}
index 61e960f9cdefe4b179b0635ea2b35974c4b22172..857cdcbeca38d36b0a0e41af48d6dae613bd3d4d 100644 (file)
@@ -27,7 +27,7 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
 {
     protected $mailer;
     protected $spool;
-    protected $translator;
+    protected $twig;
 
     protected function setUp()
     {
@@ -38,12 +38,11 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
         );
         $this->mailer = new \Swift_Mailer($transport);
 
-        $this->translator = new Translator('en');
-        $this->translator->addLoader('array', new ArrayLoader());
-        $this->translator->addResource('array', array(
-            'auth_code.mailer.subject' => 'auth_code subject',
-            'auth_code.mailer.body' => 'Hi %user%, here is the code: %code% and the support: %support%',
-        ), 'en', 'wallabag_user');
+        $this->twig = new \Twig_Environment(new \Twig_Loader_Array(array('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig' => '
+{% block subject %}subject{% endblock %}
+{% block body_html %}html body{% endblock %}
+{% block body_text %}text body{% endblock %}
+')));
     }
 
     public function testSendEmail()
@@ -56,7 +55,7 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
 
         $authCodeMailer = new AuthCodeMailer(
             $this->mailer,
-            $this->translator,
+            $this->twig,
             'nobody@test.io',
             'wallabag test',
             'http://0.0.0.0'
@@ -69,7 +68,8 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
         $msg = $this->spool->getMessages()[0];
         $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
         $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom());
-        $this->assertEquals('auth_code subject', $msg->getSubject());
-        $this->assertContains('Hi Bob, here is the code: 666666 and the support: http://0.0.0.0', $msg->toString());
+        $this->assertEquals('subject', $msg->getSubject());
+        $this->assertContains('text body', $msg->toString());
+        $this->assertContains('html body', $msg->toString());
     }
 }