]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php
Convert array + phpDoc
[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;
63e40f2d 7use Craue\ConfigBundle\Util\Config;
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
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 *
d1f1333f
JB
60 * @param \Swift_Mailer $mailer
61 * @param \Twig_Environment $twig
62 * @param string $senderEmail
63 * @param string $senderName
63e40f2d 64 * @param Config $craueConfig Craue\Config instance to get wallabag support url from database
23ff8d36 65 */
63e40f2d 66 public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, Config $craueConfig)
23ff8d36
JB
67 {
68 $this->mailer = $mailer;
7ce895bf 69 $this->twig = $twig;
23ff8d36
JB
70 $this->senderEmail = $senderEmail;
71 $this->senderName = $senderName;
63e40f2d
JB
72 $this->supportUrl = $craueConfig->get('wallabag_support_url');
73 $this->wallabagUrl = $craueConfig->get('wallabag_url');
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}