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