ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilMailMimeTransportBase.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPMailer\PHPMailer\PHPMailer;
22
24{
25 protected PHPMailer $mailer;
26
27 public function __construct(protected ilSetting $settings, private readonly ilAppEventHandler $event_handler)
28 {
29 $mail = new PHPMailer();
30 $this->setMailer($mail);
31 }
32
33 protected function getMailer(): PHPMailer
34 {
35 return $this->mailer;
36 }
37
38 protected function setMailer(PHPMailer $mailer): void
39 {
40 $this->mailer = $mailer;
41 }
42
43 protected function resetMailer(): void
44 {
45 $this->getMailer()->clearAllRecipients();
46 $this->getMailer()->clearAttachments();
47 $this->getMailer()->clearReplyTos();
48 $this->getMailer()->ErrorInfo = '';
49 }
50
51 protected function onBeforeSend(): void
52 {
53 }
54
55 final public function send(ilMimeMail $mail): bool
56 {
57 $this->resetMailer();
58
59 $this->getMailer()->XMailer = ' ';
60
61 foreach ($mail->getTo() as $recipients) {
62 $recipient_pieces = array_filter(array_map('trim', explode(',', $recipients)));
63 foreach ($recipient_pieces as $recipient) {
64 if (!$this->getMailer()->addAddress($recipient)) {
65 ilLoggerFactory::getLogger('mail')->warning('{error}', ['error' => $this->getMailer()->ErrorInfo]);
66 }
67 }
68 }
69
70 foreach ($mail->getCc() as $carbon_copies) {
71 $cc_pieces = array_filter(array_map('trim', explode(',', $carbon_copies)));
72 foreach ($cc_pieces as $carbon_copy) {
73 if (!$this->getMailer()->addCC($carbon_copy)) {
74 ilLoggerFactory::getLogger('mail')->warning('{error}', ['error' => $this->getMailer()->ErrorInfo]);
75 }
76 }
77 }
78
79 foreach ($mail->getBcc() as $blind_carbon_copies) {
80 $bcc_pieces = array_filter(array_map('trim', explode(',', $blind_carbon_copies)));
81 foreach ($bcc_pieces as $blind_carbon_copy) {
82 if (!$this->getMailer()->addBCC($blind_carbon_copy)) {
83 ilLoggerFactory::getLogger('mail')->warning('{error}', ['error' => $this->getMailer()->ErrorInfo]);
84 }
85 }
86 }
87
88 $this->getMailer()->Subject = $mail->getSubject();
89
90 if ($mail->getFrom()->hasReplyToAddress() && !$this->getMailer()->addReplyTo(
91 $mail->getFrom()->getReplyToAddress(),
92 $mail->getFrom()->getReplyToName()
93 )) {
94 ilLoggerFactory::getLogger('mail')->warning('{error}', ['error' => $this->getMailer()->ErrorInfo]);
95 }
96 if ($mail->getFrom()->hasEnvelopFromAddress()) {
97 $this->getMailer()->Sender = $mail->getFrom()->getEnvelopFromAddress();
98 }
99
100 if (!$this->getMailer()->setFrom($mail->getFrom()->getFromAddress(), $mail->getFrom()->getFromName(), false)) {
101 ilLoggerFactory::getLogger('mail')->warning('{error}', ['error' => $this->getMailer()->ErrorInfo]);
102 }
103
104 foreach ($mail->getAttachments() as $attachment) {
105 if (!$this->getMailer()->addAttachment($attachment['path'], $attachment['name'])) {
106 ilLoggerFactory::getLogger('mail')->warning('{error}', ['error' => $this->getMailer()->ErrorInfo]);
107 }
108 }
109
110 foreach ($mail->getImages() as $image) {
111 if (!$this->getMailer()->addEmbeddedImage($image['path'], $image['cid'], $image['name'])) {
112 ilLoggerFactory::getLogger('mail')->warning('{error}', ['error' => $this->getMailer()->ErrorInfo]);
113 }
114 }
115
116 if ($mail->getFinalBodyalt() !== '') {
117 $this->getMailer()->isHTML(true);
118 $this->getMailer()->AltBody = $mail->getFinalBodyalt();
119 } else {
120 $this->getMailer()->isHTML(false);
121 $this->getMailer()->AltBody = '';
122 }
123 $this->getMailer()->Body = $mail->getFinalBody();
124 $this->getMailer()->AllowEmpty = true;
125
126 ilLoggerFactory::getLogger('mail')->info(
127 'Trying to delegate external email delivery:' .
128 ' Initiated by: {login} ({id}) ' .
129 '| To: {to} | CC: {cc} | BCC: {bcc} | Subject: {subject} ' .
130 '| From: {from_address} / {from_name} ' .
131 '| ReplyTo: {reply_to_address} / {reply_to_name} ' .
132 '| EnvelopeFrom: {envelope_from}',
133 [
134 'login' => $GLOBALS['DIC']->user()->getLogin(),
135 'id' => $GLOBALS['DIC']->user()->getId(),
136 'to' => implode(', ', $mail->getTo()),
137 'cc' => implode(', ', $mail->getCc()),
138 'bcc' => implode(', ', $mail->getBcc()),
139 'subject' => $mail->getSubject(),
140 'from_address' => $mail->getFrom()->getFromAddress(),
141 'from_name' => $mail->getFrom()->getFromName(),
142 'reply_to_address' => $mail->getFrom()->getReplyToAddress(),
143 'reply_to_name' => $mail->getFrom()->getReplyToName(),
144 'envelope_from' => $mail->getFrom()->getEnvelopFromAddress(),
145 ]
146 );
147
149 ->debug('Mail Alternative Body: {body}', ['body' => $this->getMailer()->AltBody]);
151 ->debug('Mail Body: {body}', ['body' => $this->getMailer()->Body]);
152
153 $this->getMailer()->CharSet = 'utf-8';
154
155 $this->mailer->Debugoutput = static function (string $message, $level): void {
156 if (str_contains($message, 'Invalid address') ||
157 str_contains($message, 'Message body empty')) {
158 ilLoggerFactory::getLogger('mail')->warning('{message}', ['message' => $message]);
159 } else {
160 ilLoggerFactory::getLogger('mail')->debug('{message}', ['message' => $message]);
161 }
162 };
163
164 $this->onBeforeSend();
165 $result = $this->getMailer()->send();
166 if ($result) {
168 ->info('Successfully delegated external mail delivery');
169
170 if ($this->getMailer()->ErrorInfo !== '') {
171 ilLoggerFactory::getLogger('mail')->warning(
172 '... with most recent errors: {error}',
173 ['error' => $this->getMailer()->ErrorInfo]
174 );
175 }
176 } else {
177 ilLoggerFactory::getLogger('mail')->warning(
178 'Could not deliver external email: {error}',
179 ['error' => $this->getMailer()->ErrorInfo]
180 );
181 }
182
183 $this->event_handler->raise('components/ILIAS/Mail', 'externalEmailDelegated', [
184 'mail' => $mail,
185 'result' => $result,
186 ]);
187
188 return $result;
189 }
190}
Global event handler.
static getLogger(string $a_component_id)
Get component logger.
__construct(protected ilSetting $settings, private readonly ilAppEventHandler $event_handler)
ILIAS Setting Class.
$GLOBALS["DIC"]
Definition: wac.php:54