]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php
Use HTML email for 2FA
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Mailer / AuthCodeMailer.php
CommitLineData
23ff8d36
JB
1<?php
2
3namespace Wallabag\UserBundle\Mailer;
4
5use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
6use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
e678c475 7use Symfony\Component\Translation\TranslatorInterface;
23ff8d36
JB
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 */
13class AuthCodeMailer implements AuthCodeMailerInterface
14{
15 /**
16 * SwiftMailer.
17 *
18 * @var \Swift_Mailer
19 */
20 private $mailer;
21
22 /**
7ce895bf 23 * Twig to render the html's email.
23ff8d36 24 *
7ce895bf 25 * @var \Twig_Environment
23ff8d36 26 */
7ce895bf 27 private $twig;
23ff8d36
JB
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 *
7ce895bf
JB
53 * @param \Swift_Mailer $mailer
54 * @param \Twig_Environment $twig
55 * @param string $senderEmail
56 * @param string $senderName
57 * @param string $supportUrl
23ff8d36 58 */
7ce895bf 59 public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, $supportUrl)
23ff8d36
JB
60 {
61 $this->mailer = $mailer;
7ce895bf 62 $this->twig = $twig;
23ff8d36
JB
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 {
7ce895bf
JB
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
23ff8d36
JB
89 $message = new \Swift_Message();
90 $message
91 ->setTo($user->getEmail())
92 ->setFrom($this->senderEmail, $this->senderName)
7ce895bf
JB
93 ->setSubject($subject)
94 ->setBody($bodyText, 'text/plain')
95 ->addPart($bodyHtml, 'text/html')
23ff8d36
JB
96 ;
97
98 $this->mailer->send($message);
99 }
100}