diff options
Diffstat (limited to 'src/Wallabag/UserBundle/Mailer')
-rw-r--r-- | src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php new file mode 100644 index 00000000..f1960070 --- /dev/null +++ b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php | |||
@@ -0,0 +1,93 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\Mailer; | ||
4 | |||
5 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; | ||
6 | use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface; | ||
7 | use Symfony\Component\Translation\DataCollectorTranslator; | ||
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 | /** | ||
23 | * Translator for email content. | ||
24 | * | ||
25 | * @var DataCollectorTranslator | ||
26 | */ | ||
27 | private $translator; | ||
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 | * | ||
53 | * @param \Swift_Mailer $mailer | ||
54 | * @param DataCollectorTranslator $translator | ||
55 | * @param string $senderEmail | ||
56 | * @param string $senderName | ||
57 | * @param string $supportUrl | ||
58 | */ | ||
59 | public function __construct(\Swift_Mailer $mailer, DataCollectorTranslator $translator, $senderEmail, $senderName, $supportUrl) | ||
60 | { | ||
61 | $this->mailer = $mailer; | ||
62 | $this->translator = $translator; | ||
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 | { | ||
75 | $message = new \Swift_Message(); | ||
76 | $message | ||
77 | ->setTo($user->getEmail()) | ||
78 | ->setFrom($this->senderEmail, $this->senderName) | ||
79 | ->setSubject($this->translator->trans('auth_code.mailer.subject', array(), 'wallabag_user')) | ||
80 | ->setBody($this->translator->trans( | ||
81 | 'auth_code.mailer.body', | ||
82 | [ | ||
83 | '%user%' => $user->getName(), | ||
84 | '%code%' => $user->getEmailAuthCode(), | ||
85 | '%support%' => $this->supportUrl, | ||
86 | ], | ||
87 | 'wallabag_user' | ||
88 | )) | ||
89 | ; | ||
90 | |||
91 | $this->mailer->send($message); | ||
92 | } | ||
93 | } | ||