1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the TheAlternativeZurich/events project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Service; |
13
|
|
|
|
14
|
|
|
use App\Entity\User; |
15
|
|
|
use App\Service\Interfaces\EmailServiceInterface; |
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
17
|
|
|
use Exception; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
20
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
21
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
22
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
23
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
24
|
|
|
|
25
|
|
|
class EmailService implements EmailServiceInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \Doctrine\Persistence\ObjectManager |
29
|
|
|
*/ |
30
|
|
|
private $manager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Symfony\Component\HttpFoundation\Request|null |
34
|
|
|
*/ |
35
|
|
|
private $request; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TranslatorInterface |
39
|
|
|
*/ |
40
|
|
|
private $translator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var LoggerInterface |
44
|
|
|
*/ |
45
|
|
|
private $logger; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var MailerInterface |
49
|
|
|
*/ |
50
|
|
|
private $mailer; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $mailerFromEmail; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* EmailService constructor. |
59
|
|
|
*/ |
60
|
|
|
public function __construct(RequestStack $requestStack, ManagerRegistry $managerRegistry, TranslatorInterface $translator, LoggerInterface $logger, MailerInterface $mailer, string $mailerFromEmail) |
61
|
|
|
{ |
62
|
|
|
$this->manager = $managerRegistry->getManager(); |
63
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
64
|
|
|
$this->translator = $translator; |
65
|
|
|
$this->logger = $logger; |
66
|
|
|
$this->mailer = $mailer; |
67
|
|
|
$this->mailerFromEmail = $mailerFromEmail; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws Exception |
72
|
|
|
*/ |
73
|
|
|
public function sendAuthenticateLink(User $user): bool |
74
|
|
|
{ |
75
|
|
|
$user->generateAuthenticationHash(); |
76
|
|
|
|
77
|
|
|
$httpHost = $this->request ? $this->request->getHttpHost() : 'localhost'; |
78
|
|
|
|
79
|
|
|
$message = (new TemplatedEmail()) |
80
|
|
|
->subject($this->translator->trans('email.send_authentication_link.subject', ['%page%' => $httpHost], 'email')) |
81
|
|
|
->from($this->mailerFromEmail) |
82
|
|
|
->to($user->getEmail()); |
83
|
|
|
|
84
|
|
|
//construct plain body |
85
|
|
|
$message->textTemplate('email/authentication_link.txt.twig'); |
86
|
|
|
$message->htmlTemplate('email/authentication_link.html.twig'); |
87
|
|
|
|
88
|
|
|
$message->context(['user' => $user]); |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$this->mailer->send($message); |
92
|
|
|
|
93
|
|
|
$this->manager->persist($user); |
94
|
|
|
$this->manager->flush(); |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} catch (TransportExceptionInterface $exception) { |
98
|
|
|
$this->logger->error('email send failed', ['exception' => $exception]); |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|