-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathMailService.php
More file actions
190 lines (174 loc) · 7.77 KB
/
MailService.php
File metadata and controls
190 lines (174 loc) · 7.77 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
188
189
190
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service;
use OCA\Libresign\Db\File;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Exception\LibresignException;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Mail\IMailer;
use Psr\Log\LoggerInterface;
class MailService {
/** @var array */
private $files = [];
public function __construct(
private LoggerInterface $logger,
private IMailer $mailer,
private FileMapper $fileMapper,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private IAppConfig $appConfig,
) {
}
/**
* @psalm-suppress MixedReturnStatement
*/
private function getFileById(int $fileId): File {
if (!isset($this->files[$fileId])) {
$this->files[$fileId] = $this->fileMapper->getById($fileId);
}
return $this->files[$fileId];
}
/**
* @psalm-suppress MixedMethodCall
*/
public function notifySignDataUpdated(SignRequest $data, string $email, ?string $description = null): void {
$emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
// TRANSLATORS The subject of the email that is sent after changes are made to the signature request that may affect something for the signer who will sign the document. Some possible reasons: URL for signature changed (when the URL expires), the person who requested the signature sent a notification
$emailTemplate->setSubject($this->l10n->t('LibreSign: Changes into a file for you to sign'));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l10n->t('File to sign'), false);
if (!empty($description)) {
$emailTemplate->addBodyText($description);
$emailTemplate->addBodyText('');
}
$emailTemplate->addBodyText($this->l10n->t('Changes have been made in a file that you have to sign. Access the link below:'));
$link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $data->getUuid()]);
$file = $this->getFileById($data->getFileId());
$emailTemplate->addBodyButton(
$this->l10n->t('Sign »%s«', [$file->getName()]),
$link
);
$message = $this->mailer->createMessage();
if ($data->getDisplayName()) {
$message->setTo([$email => $data->getDisplayName()]);
} else {
$message->setTo([$email]);
}
$message->useTemplate($emailTemplate);
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Notify changes in unsigned notification mail could not be sent: ' . $e->getMessage());
throw new LibresignException('Notify unsigned notification mail could not be sent', 1);
}
}
/**
* @psalm-suppress MixedMethodCall
*/
public function notifyUnsignedUser(SignRequest $data, string $email, ?string $description = null): void {
$emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
$emailTemplate->setSubject($this->l10n->t('LibreSign: There is a file for you to sign'));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l10n->t('File to sign'), false);
if (!empty($description)) {
$emailTemplate->addBodyText($description);
$emailTemplate->addBodyText('');
}
$emailTemplate->addBodyText($this->l10n->t('There is a document for you to sign. Access the link below:'));
$link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $data->getUuid()]);
$file = $this->getFileById($data->getFileId());
$emailTemplate->addBodyButton(
$this->l10n->t('Sign »%s«', [$file->getName()]),
$link
);
$message = $this->mailer->createMessage();
if ($data->getDisplayName()) {
$message->setTo([$email => $data->getDisplayName()]);
} else {
$message->setTo([$email]);
}
$message->useTemplate($emailTemplate);
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Notify unsigned notification mail could not be sent: ' . $e->getMessage());
throw new LibresignException('Notify unsigned notification mail could not be sent', 1);
}
}
public function notifySignedUser(SignRequest $signRequest, string $email, File $libreSignFile, string $displayName): void {
$emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
// TRANSLATORS The subject of the email that is sent after a document has been signed by a user. This email is sent to the person who requested the signature.
$emailTemplate->setSubject($this->l10n->t('LibreSign: A file has been signed'));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l10n->t('File signed'), false);
// TRANSLATORS The text in the email that is sent after a document has been signed by a user. %s will be replaced with the name of the user who signed the document.
$emailTemplate->addBodyText($this->l10n->t('%s signed the document. You can access it using the link below:', [$signRequest->getDisplayName()]));
$link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.indexFPath', [
'path' => 'validation/' . $libreSignFile->getUuid(),
]);
$file = $this->getFileById($signRequest->getFileId());
$emailTemplate->addBodyButton(
// TRANSLATORS The button text in the email that is sent after a document has been signed by a user. %s will be replaced with the name of the file that was signed.
$this->l10n->t('View signed file »%s«', [$file->getName()]),
$link
);
$message = $this->mailer->createMessage();
$message->setTo([$email => $displayName]);
$message->useTemplate($emailTemplate);
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Notify signed notification mail could not be sent: ' . $e->getMessage());
throw new LibresignException('Notify signed notification mail could not be sent', 1);
}
}
public function notifyCanceledRequest(SignRequest $signRequest, string $email, File $libreSignFile): void {
$emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
// TRANSLATORS The subject of the email that is sent when a signature request has been canceled.
$emailTemplate->setSubject($this->l10n->t('LibreSign: A signature request has been canceled'));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l10n->t('Signature request canceled'), false);
// TRANSLATORS The text in the email that is sent when a signature request has been canceled. %s will be replaced with the name of the file.
$emailTemplate->addBodyText($this->l10n->t('The request for you to sign "%s" has been canceled.', [$libreSignFile->getName()]));
$message = $this->mailer->createMessage();
if ($signRequest->getDisplayName()) {
$message->setTo([$email => $signRequest->getDisplayName()]);
} else {
$message->setTo([$email]);
}
$message->useTemplate($emailTemplate);
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Notify canceled request mail could not be sent: ' . $e->getMessage());
// Don't throw exception to avoid breaking the flow when mail fails
}
}
public function sendCodeToSign(string $email, string $name, string $code): void {
$emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
$emailTemplate->setSubject($this->l10n->t('LibreSign: Code to sign file'));
$emailTemplate->addHeader();
$emailTemplate->addBodyText($this->l10n->t('Use this code to sign the document:'));
$emailTemplate->addBodyText($code);
$message = $this->mailer->createMessage();
if (!empty($name)) {
$message->setTo([$email => $name]);
} else {
$message->setTo([$email]);
}
$message->useTemplate($emailTemplate);
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Mail with code to sign document could not be sent: ' . $e->getMessage());
throw new LibresignException('Mail with code to sign document could not be sent', 1);
}
}
}