ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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($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($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($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($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($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($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($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(sprintf(
127 "Trying to delegate external email delivery:" .
128 " Initiated by: %s (%s) " .
129 "| To: %s | CC: %s | BCC: %s | Subject: %s " .
130 "| From: %s / %s " .
131 "| ReplyTo: %s / %s " .
132 "| EnvelopeFrom: %s",
133 $GLOBALS['DIC']->user()->getLogin(),
134 $GLOBALS['DIC']->user()->getId(),
135 implode(', ', $mail->getTo()),
136 implode(', ', $mail->getCc()),
137 implode(', ', $mail->getBcc()),
138 $mail->getSubject(),
139 $mail->getFrom()->getFromAddress(),
140 $mail->getFrom()->getFromName(),
141 $mail->getFrom()->getReplyToAddress(),
142 $mail->getFrom()->getReplyToName(),
143 $mail->getFrom()->getEnvelopFromAddress()
144 ));
145
147 ->debug(sprintf("Mail Alternative Body: %s", $this->getMailer()->AltBody));
149 ->debug(sprintf("Mail Body: %s", $this->getMailer()->Body));
150
151 $this->getMailer()->CharSet = 'utf-8';
152
153 $this->mailer->Debugoutput = static function (string $message, $level): void {
154 if (
155 strpos($message, 'Invalid address') ||
156 strpos($message, 'Message body empty')
157 ) {
158 ilLoggerFactory::getLogger('mail')->warning($message);
159 } else {
160 ilLoggerFactory::getLogger('mail')->debug($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(sprintf(
172 '... with most recent errors: %s',
173 $this->getMailer()->ErrorInfo
174 ));
175 }
176 } else {
177 ilLoggerFactory::getLogger('mail')->warning(sprintf(
178 'Could not deliver external email: %s',
179 $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