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