1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\ServiceHelper; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
11
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
12
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
13
|
|
|
use Symfony\Component\Mime\Address; |
14
|
|
|
use Symfony\Component\Mime\BodyRendererInterface; |
15
|
|
|
|
16
|
|
|
final class MailHelper |
17
|
|
|
{ |
18
|
|
|
public function __construct( |
19
|
|
|
private readonly MailerInterface $mailer, |
20
|
|
|
private readonly BodyRendererInterface $bodyRenderer, |
21
|
|
|
) {} |
22
|
|
|
|
23
|
|
|
public function send( |
24
|
|
|
string $recipientName, |
25
|
|
|
string $recipientEmail, |
26
|
|
|
string $subject, |
27
|
|
|
string $body, |
28
|
|
|
?string $senderName = null, |
29
|
|
|
?string $senderEmail = null, |
30
|
|
|
array $extra_headers = [], |
31
|
|
|
array $data_file = [], |
32
|
|
|
bool $embeddedImage = false, |
33
|
|
|
array $additionalParameters = [], |
34
|
|
|
?string $sendErrorTo = null, |
35
|
|
|
): bool { |
36
|
|
|
if (!api_valid_email($recipientEmail)) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$templatedEmail = new TemplatedEmail(); |
41
|
|
|
|
42
|
|
|
api_set_noreply_and_from_address_to_mailer( |
43
|
|
|
$templatedEmail, |
44
|
|
|
['name' => $senderName, 'email' => $senderEmail], |
45
|
|
|
!empty($extra_headers['reply_to']) ? $extra_headers['reply_to'] : [] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
if ($sendErrorTo) { |
49
|
|
|
$templatedEmail |
50
|
|
|
->getHeaders() |
51
|
|
|
->addIdHeader('Errors-To', $sendErrorTo) |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Reply to first |
56
|
|
|
$replyToName = ''; |
57
|
|
|
$replyToEmail = ''; |
58
|
|
|
if (isset($extra_headers['reply_to'])) { |
59
|
|
|
$replyToEmail = $extra_headers['reply_to']['mail']; |
60
|
|
|
$replyToName = $extra_headers['reply_to']['name']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
$templatedEmail->subject($subject); |
65
|
|
|
|
66
|
|
|
$list = api_get_setting('announcement.send_all_emails_to', true); |
67
|
|
|
|
68
|
|
|
if (!empty($list) && isset($list['emails'])) { |
69
|
|
|
foreach ($list['emails'] as $email) { |
70
|
|
|
$templatedEmail->cc($email); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Attachment |
75
|
|
|
if (!empty($data_file)) { |
76
|
|
|
foreach ($data_file as $file_attach) { |
77
|
|
|
if (!empty($file_attach['path']) && !empty($file_attach['filename'])) { |
78
|
|
|
$templatedEmail->attachFromPath($file_attach['path'], $file_attach['filename']); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$noReply = api_get_setting('noreply_email_address'); |
84
|
|
|
$automaticEmailText = ''; |
85
|
|
|
|
86
|
|
|
if (!empty($noReply)) { |
87
|
|
|
$automaticEmailText = '<br />'.get_lang('This is an automatic email message. Please do not reply to it.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$params = [ |
91
|
|
|
'mail_header_style' => api_get_setting('mail.mail_header_style'), |
92
|
|
|
'mail_content_style' => api_get_setting('mail.mail_content_style'), |
93
|
|
|
'link' => $additionalParameters['link'] ?? '', |
94
|
|
|
'automatic_email_text' => $automaticEmailText, |
95
|
|
|
'content' => $body, |
96
|
|
|
'theme' => api_get_visual_theme(), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
if (!empty($recipientEmail)) { |
100
|
|
|
$templatedEmail->to(new Address($recipientEmail, $recipientName)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!empty($replyToEmail)) { |
104
|
|
|
$templatedEmail->replyTo(new Address($replyToEmail, $replyToName)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$templatedEmail |
108
|
|
|
->htmlTemplate('@ChamiloCore/Mailer/Default/default.html.twig') |
109
|
|
|
->context($params) |
110
|
|
|
; |
111
|
|
|
|
112
|
|
|
$this->bodyRenderer->render($templatedEmail); |
113
|
|
|
$this->mailer->send($templatedEmail); |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} catch (Exception|TransportExceptionInterface $e) { |
117
|
|
|
error_log($e->getMessage()); |
118
|
|
|
|
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |