]>
Commit | Line | Data |
---|---|---|
23ff8d36 JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\UserBundle\Mailer; | |
4 | ||
23ff8d36 | 5 | use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface; |
f808b016 | 6 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; |
a2f4efe6 | 7 | use Twig\Environment; |
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 | */ | |
13 | class 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 | * |
ac5844a6 | 25 | * @var 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 | ||
abd454c4 | 50 | /** |
855a4c68 | 51 | * Url for the wallabag instance (only used for image in the HTML email template). |
abd454c4 JB |
52 | * |
53 | * @var string | |
54 | */ | |
55 | private $wallabagUrl; | |
56 | ||
23ff8d36 JB |
57 | /** |
58 | * Initialize the auth code mailer with the SwiftMailer object. | |
59 | * | |
8d4ed0df JB |
60 | * @param string $senderEmail |
61 | * @param string $senderName | |
62 | * @param string $supportUrl wallabag support url | |
63 | * @param string $wallabagUrl wallabag instance url | |
23ff8d36 | 64 | */ |
a2f4efe6 | 65 | public function __construct(\Swift_Mailer $mailer, Environment $twig, $senderEmail, $senderName, $supportUrl, $wallabagUrl) |
23ff8d36 JB |
66 | { |
67 | $this->mailer = $mailer; | |
7ce895bf | 68 | $this->twig = $twig; |
23ff8d36 JB |
69 | $this->senderEmail = $senderEmail; |
70 | $this->senderName = $senderName; | |
4b3c983a JB |
71 | $this->supportUrl = $supportUrl; |
72 | $this->wallabagUrl = $wallabagUrl; | |
23ff8d36 JB |
73 | } |
74 | ||
75 | /** | |
76 | * Send the auth code to the user via email. | |
23ff8d36 | 77 | */ |
a6b242a1 | 78 | public function sendAuthCode(TwoFactorInterface $user): void |
23ff8d36 | 79 | { |
855a4c68 | 80 | $template = $this->twig->loadTemplate('WallabagUserBundle:TwoFactor:email_auth_code.html.twig'); |
7ce895bf | 81 | |
4094ea47 | 82 | $subject = $template->renderBlock('subject', []); |
7ce895bf JB |
83 | $bodyHtml = $template->renderBlock('body_html', [ |
84 | 'user' => $user->getName(), | |
85 | 'code' => $user->getEmailAuthCode(), | |
abd454c4 JB |
86 | 'support_url' => $this->supportUrl, |
87 | 'wallabag_url' => $this->wallabagUrl, | |
7ce895bf JB |
88 | ]); |
89 | $bodyText = $template->renderBlock('body_text', [ | |
90 | 'user' => $user->getName(), | |
91 | 'code' => $user->getEmailAuthCode(), | |
abd454c4 | 92 | 'support_url' => $this->supportUrl, |
7ce895bf JB |
93 | ]); |
94 | ||
23ff8d36 JB |
95 | $message = new \Swift_Message(); |
96 | $message | |
43ccf4b1 | 97 | ->setTo($user->getEmailAuthRecipient()) |
23ff8d36 | 98 | ->setFrom($this->senderEmail, $this->senderName) |
7ce895bf JB |
99 | ->setSubject($subject) |
100 | ->setBody($bodyText, 'text/plain') | |
101 | ->addPart($bodyHtml, 'text/html') | |
23ff8d36 JB |
102 | ; |
103 | ||
104 | $this->mailer->send($message); | |
105 | } | |
106 | } |