]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php
Use HTML email for 2FA
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Mailer / AuthCodeMailer.php
1 <?php
2
3 namespace Wallabag\UserBundle\Mailer;
4
5 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
6 use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
7 use Symfony\Component\Translation\TranslatorInterface;
8
9 /**
10 * Custom mailer for TwoFactorBundle email.
11 * It adds a custom template to the email so user won't get a lonely authentication code but a complete email.
12 */
13 class AuthCodeMailer implements AuthCodeMailerInterface
14 {
15 /**
16 * SwiftMailer.
17 *
18 * @var \Swift_Mailer
19 */
20 private $mailer;
21
22 /**
23 * Twig to render the html's email.
24 *
25 * @var \Twig_Environment
26 */
27 private $twig;
28
29 /**
30 * Sender email address.
31 *
32 * @var string
33 */
34 private $senderEmail;
35
36 /**
37 * Sender name.
38 *
39 * @var string
40 */
41 private $senderName;
42
43 /**
44 * Support URL to report any bugs.
45 *
46 * @var string
47 */
48 private $supportUrl;
49
50 /**
51 * Initialize the auth code mailer with the SwiftMailer object.
52 *
53 * @param \Swift_Mailer $mailer
54 * @param \Twig_Environment $twig
55 * @param string $senderEmail
56 * @param string $senderName
57 * @param string $supportUrl
58 */
59 public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, $supportUrl)
60 {
61 $this->mailer = $mailer;
62 $this->twig = $twig;
63 $this->senderEmail = $senderEmail;
64 $this->senderName = $senderName;
65 $this->supportUrl = $supportUrl;
66 }
67
68 /**
69 * Send the auth code to the user via email.
70 *
71 * @param TwoFactorInterface $user
72 */
73 public function sendAuthCode(TwoFactorInterface $user)
74 {
75 $template = $this->twig->loadTemplate('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig');
76
77 $subject = $template->renderBlock('subject', array());
78 $bodyHtml = $template->renderBlock('body_html', [
79 'user' => $user->getName(),
80 'code' => $user->getEmailAuthCode(),
81 'support' => $this->supportUrl,
82 ]);
83 $bodyText = $template->renderBlock('body_text', [
84 'user' => $user->getName(),
85 'code' => $user->getEmailAuthCode(),
86 'support' => $this->supportUrl,
87 ]);
88
89 $message = new \Swift_Message();
90 $message
91 ->setTo($user->getEmail())
92 ->setFrom($this->senderEmail, $this->senderName)
93 ->setSubject($subject)
94 ->setBody($bodyText, 'text/plain')
95 ->addPart($bodyHtml, 'text/html')
96 ;
97
98 $this->mailer->send($message);
99 }
100 }