ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilMailMimeTransportBase.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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 
125  ilLoggerFactory::getLogger('mail')->info(sprintf(
126  "Trying to delegate external email delivery:" .
127  " Initiated by: %s (%s) " .
128  "| To: %s | CC: %s | BCC: %s | Subject: %s " .
129  "| From: %s / %s " .
130  "| ReplyTo: %s / %s " .
131  "| EnvelopeFrom: %s",
132  $GLOBALS['DIC']->user()->getLogin(),
133  $GLOBALS['DIC']->user()->getId(),
134  implode(', ', $mail->getTo()),
135  implode(', ', $mail->getCc()),
136  implode(', ', $mail->getBcc()),
137  $mail->getSubject(),
138  $mail->getFrom()->getFromAddress(),
139  $mail->getFrom()->getFromName(),
140  $mail->getFrom()->getReplyToAddress(),
141  $mail->getFrom()->getReplyToName(),
142  $mail->getFrom()->getEnvelopFromAddress()
143  ));
144 
146  ->debug(sprintf("Mail Alternative Body: %s", $this->getMailer()->AltBody));
148  ->debug(sprintf("Mail Body: %s", $this->getMailer()->Body));
149 
150  $this->getMailer()->CharSet = 'utf-8';
151 
152  $this->mailer->Debugoutput = static function (string $message, $level): void {
153  if (
154  strpos($message, 'Invalid address') ||
155  strpos($message, 'Message body empty')
156  ) {
157  ilLoggerFactory::getLogger('mail')->warning($message);
158  } else {
159  ilLoggerFactory::getLogger('mail')->debug($message);
160  }
161  };
162 
163  $this->onBeforeSend();
164  $result = $this->getMailer()->send();
165  if ($result) {
167  ->info('Successfully delegated external mail delivery');
168 
169  if ($this->getMailer()->ErrorInfo !== '') {
170  ilLoggerFactory::getLogger('mail')->warning(sprintf(
171  '... with most recent errors: %s',
172  $this->getMailer()->ErrorInfo
173  ));
174  }
175  } else {
176  ilLoggerFactory::getLogger('mail')->warning(sprintf(
177  'Could not deliver external email: %s',
178  $this->getMailer()->ErrorInfo
179  ));
180  }
181 
182  $this->event_handler->raise('components/ILIAS/Mail', 'externalEmailDelegated', [
183  'mail' => $mail,
184  'result' => $result,
185  ]);
186 
187  return $result;
188  }
189 }
Global event handler.
static getLogger(string $a_component_id)
Get component logger.
$GLOBALS["DIC"]
Definition: wac.php:53
__construct(protected ilSetting $settings, private readonly ilAppEventHandler $event_handler)
$message
Definition: xapiexit.php:31