forked from Probesys/bileto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendMessageEmailHandler.php
More file actions
198 lines (159 loc) · 6.59 KB
/
SendMessageEmailHandler.php
File metadata and controls
198 lines (159 loc) · 6.59 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
191
192
193
194
195
196
197
198
<?php
// This file is part of Bileto.
// Copyright 2022-2025 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace App\MessageHandler;
use App\Entity;
use App\Repository;
use App\Message;
use App\Service;
use App\Utils;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
#[AsMessageHandler]
class SendMessageEmailHandler
{
use EmailSender;
public function __construct(
private Repository\MessageRepository $messageRepository,
private Service\MessageDocumentStorage $messageDocumentStorage,
private TranslatorInterface $translator,
private TransportInterface $transportInterface,
private LoggerInterface $logger,
private UrlGeneratorInterface $urlGenerator,
#[Autowire(env: 'MAILER_FROM')]
private string $mailerFrom,
) {
}
public function __invoke(Message\SendMessageEmail $data): void
{
$messageId = $data->getMessageId();
$message = $this->messageRepository->find($messageId);
if (!$message) {
$this->logger->error("Message {$messageId} cannot be found in SendMessageEmailHandler.");
return;
}
if ($message->isConfidential()) {
// If the message is confidential, it should not be sent to recipients
// who don't have the orga:see:tickets:messages:confidential
// permission. At the moment, I don't know what's the best solution
// to handle that so I've decided to never send the notification in
// that case. This will have to be improved in the future.
return;
}
$ticket = $message->getTicket();
$author = $message->getCreatedBy();
$requester = $ticket->getRequester();
$observers = $ticket->getObservers();
$assignee = $ticket->getAssignee();
$locale = $author->getLocale();
$recipients = [];
if ($requester !== $author) {
$recipients[$requester->getEmail()] = $requester;
}
foreach ($observers as $observer) {
if ($observer !== $author) {
$recipients[$observer->getEmail()] = $observer;
}
}
if ($assignee && $assignee !== $author) {
$recipients[$assignee->getEmail()] = $assignee;
}
$recipients = array_filter($recipients, function (?Entity\User $user): bool {
// Remove the anonymous users from the list of recipients to avoid
// to send emails to wrong addresses.
return $user && !$user->isAnonymized();
});
if (empty($recipients)) {
return;
}
$previousMessage = $ticket->getMessageBefore($message, confidential: false);
$subject = "[#{$ticket->getId()}]";
if ($ticket->isFinished()) {
$status = $this->translator->trans($ticket->getStatusLabel(), locale: $locale);
$subject = "{$subject} [{$status}]";
}
$subject = "{$subject} {$ticket->getTitle()}";
if ($previousMessage) {
$subject = "Re: {$subject}";
}
$email = new TemplatedEmail();
$from = new Address($this->mailerFrom, $author->getDisplayName());
$email->from($from);
$email->subject($subject);
$email->locale($locale);
$replacingMapping = [];
// Attach the message documents as attachments to the email
foreach ($message->getMessageDocuments() as $messageDocument) {
$filepath = $this->messageDocumentStorage->getPathname($messageDocument);
$file = new File($filepath);
$dataPart = new DataPart(
$file,
$messageDocument->getName(),
$messageDocument->getMimetype()
);
$email->addPart($dataPart);
$initialUrl = $this->urlGenerator->generate(
'message document',
[
'uid' => $messageDocument->getUid(),
'extension' => $messageDocument->getExtension(),
],
UrlGeneratorInterface::ABSOLUTE_URL
);
$replacingMapping[$initialUrl] = 'cid:' . $dataPart->getContentId();
}
// Change the images URLs in the email in order to point to the "cid" references.
$content = $message->getContent();
$content = Utils\DomHelper::replaceImagesUrls($content, $replacingMapping);
$email->htmlTemplate('emails/message.html.twig');
$email->textTemplate('emails/message.txt.twig');
// Set correct references headers so email clients can add the email to
// the conversation thread.
$emailReferences = [];
foreach ($ticket->getMessages(confidential: false) as $message) {
$references = $message->getEmailNotificationsReferences();
if ($references) {
$emailReferences = array_merge($emailReferences, $references);
}
}
if ($emailReferences) {
$email->getHeaders()->addIdHeader('References', $emailReferences);
}
if ($previousMessage) {
$references = $previousMessage->getEmailNotificationsReferences();
if ($references) {
$email->getHeaders()->addIdHeader('In-Reply-To', $references[0]);
}
}
// Ask compliant autoresponders to not reply to this email
$email->getHeaders()->addTextHeader('X-Auto-Response-Suppress', 'All');
foreach ($recipients as $user) {
$email->context([
'subject' => $subject,
'ticket' => $ticket,
'content' => $content,
'linkToBileto' => $user->canLogin(),
]);
$email->to($user->getEmailAddress());
$emailId = $this->sendEmail($email);
$message->addEmailNotificationReference($emailId);
}
try {
$this->messageRepository->save($message, true);
} catch (EntityNotFoundException $e) {
$this->logger->error(
"Message #{$message->getId()} notification reference cannot be saved: {$e->getMessage()}"
);
}
}
}