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