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