-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailerTrait.php
More file actions
187 lines (160 loc) · 5.49 KB
/
Copy pathMailerTrait.php
File metadata and controls
187 lines (160 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace ADT\FancyAdmin\Model\Mailer;
use ADT\DoctrineAuthenticator\OTP\OnetimeToken;
use ADT\DoctrineAuthenticator\OTP\OnetimeTokenService;
use ADT\DoctrineAuthenticator\OTP\OnetimeTokenTypeEnum;
use ADT\DoctrineComponents\EntityManager;
use ADT\FancyAdmin\Model\FancyAdmin;
use ADT\FancyAdmin\Model\Entities\Identity;
use ADT\SingleRecipient\SingleRecipient;
use Contributte\Translation\Exceptions\InvalidArgument;
use Contributte\Translation\Translator;
use DateMalformedStringException;
use DateTimeImmutable;
use Exception;
use Nette\Application\LinkGenerator;
use Nette\Application\UI\Component;
use Nette\Application\UI\InvalidLinkException;
use Nette\Application\UI\TemplateFactory;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\Mail\Message;
use ReflectionClass;
use TijsVerkoyen\CssToInlineStyles;
trait MailerTrait
{
use SingleRecipient;
public function __construct(
protected readonly \Nette\Mail\Mailer $mailer,
protected readonly string $from,
protected readonly string $fromName,
protected readonly ?string $singleRecipient,
protected readonly string $supportEmail,
protected readonly string $title,
protected readonly string $web,
protected readonly string $wwwDir,
protected readonly TemplateFactory $templateFactory,
protected readonly Translator $translator,
protected readonly EntityManager $em,
protected readonly LinkGenerator $linkGenerator,
protected readonly FancyAdmin $administration,
protected readonly OnetimeTokenService $onetimeTokenService,
) {
}
public function createMessage(): Message
{
return new Message();
}
/**
* @throws InvalidArgument
*/
public function createTemplateMessage(string $templateName, string $subject, array $data = [], ?string $locale = null, array $translateVariables = []): Message
{
if ($locale) {
$originalLocale = $this->translator->getLocale();
$this->translator->setLocale($locale);
}
$templateName .= '.latte';
$dirname = dirname(new ReflectionClass($this)->getFileName()) . '/templates/';
$templateFile = $dirname . $templateName;
if (!file_exists($templateFile)) {
$templateFile = __DIR__ . '/templates/' . $templateName;
}
$layoutName = '@layout.latte';
$layoutFile = dirname(new ReflectionClass($this)->getFileName()) . '/templates/' . $layoutName;
if (!file_exists($layoutFile)) {
$layoutFile = __DIR__ . '/templates/' . $layoutName;
}
/** @var Template $template */
$template = $this->templateFactory->createTemplate();
$template->addFilter('translate', [$this->translator, 'translate'])
->setFile($templateFile);
$template->projectName = $this->administration->getProjectName();
$template->fromName = $this->fromName;
$template->logoFileName = $this->administration->getLogoBitmapPublicPath();
$template->backgroundColor = $this->administration->getEmailBackgroundColor();
$template->subject = $this->translator->translate($subject, $translateVariables);
$template->layoutFile = $layoutFile;
foreach ($data as $key => $value) {
$template->$key = $value;
}
foreach (['supportEmail', 'title', 'web'] as $privateParam) {
$template->$privateParam = $this->$privateParam;
}
$message = static::createMessage()
->setSubject($this->translator->translate($subject, $translateVariables))
->setHtmlBody(
new CssToInlineStyles\CssToInlineStyles()->convert((string) $template),
$this->wwwDir
);
if (isset($originalLocale)) {
$this->translator->setLocale($originalLocale);
}
return $message;
}
/**
* @throws Exception
*/
public function send(Message $mail): void
{
if (! $mail->getFrom() && $this->from) {
$mail->setFrom($this->from, $this->fromName)
->addReplyTo($this->supportEmail, $this->fromName);
}
if (! empty($this->singleRecipient)) {
$this->applySingleRecipient($mail, $this->singleRecipient);
}
$this->mailer->send($mail);
}
/**
* @throws InvalidArgument
* @throws DateMalformedStringException
* @throws Exception
*/
public function sendAccountCreationEmail(Identity $identity, bool $checkLimit = true): void
{
$this->em->beginTransaction();
$token = $this->onetimeTokenService->saveToken(OnetimeTokenTypeEnum::LOGIN, new DateTimeImmutable('+' . OnetimeToken::PASSWORD_CREATION_VALID_FOR . ' hours'), $identity, checkLimit: $checkLimit);
$message = $this->createTemplateMessage(
'accountCreation',
'Vytvoření účtu',
[
'link' => $this->link(':Portal:Sign:newPassword', ['email' => $identity->getEmail(), 'token' => $token])
]
);
$message->addTo($identity->getEmail());
$this->send($message);
$this->em->commit();
}
/**
* @throws DateMalformedStringException
* @throws InvalidArgument
* @throws Exception
*/
public function sendPasswordRecoveryMail(Identity $identity, int $tokenLifetime, bool $checkLimit = true): void
{
$this->em->beginTransaction();
$token = $this->onetimeTokenService->saveToken(OnetimeTokenTypeEnum::LOGIN, new DateTimeImmutable('+ ' . $tokenLifetime . ' hour'), $identity, checkLimit: $checkLimit);
$message = $this->createTemplateMessage(
'passwordRecovery',
'Nové heslo',
[
'link' => $this->link(':Portal:Sign:newPassword', ['email' => $identity->getEmail(), 'token' => $token]),
]
);
$message->addTo($identity->getEmail());
$this->send($message);
$this->em->commit();
}
/**
* @throws InvalidLinkException
*/
public function link(
string $destination,
array $args = [],
?Component $component = null,
?string $mode = null,
): ?string
{
return $this->linkGenerator->link($destination, $args, $component, $mode);
}
}