ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMailMimeTransportBase.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 /* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
6 
11 {
13  protected $mailer;
14 
16  protected $settings;
17 
19  private $eventHandler;
20 
27  {
28  $this->settings = $settings;
29  $this->eventHandler = $eventHandler;
30 
31  $mail = new PHPMailer();
32  $this->setMailer($mail);
33  }
34 
38  protected function getMailer() : PHPMailer
39  {
40  return $this->mailer;
41  }
42 
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 
62  protected function onBeforeSend() : void
63  {
64  }
65 
69  final public function send(ilMimeMail $mail) : bool
70  {
71  $this->resetMailer();
72 
73  $this->getMailer()->XMailer = ' ';
74 
75  foreach ($mail->getTo() as $recipients) {
76  $recipient_pieces = array_filter(array_map('trim', explode(',', $recipients)));
77  foreach ($recipient_pieces as $recipient) {
78  if (!$this->getMailer()->AddAddress($recipient, '')) {
79  ilLoggerFactory::getLogger('mail')->warning($this->getMailer()->ErrorInfo);
80  }
81  }
82  }
83 
84  foreach ($mail->getCc() as $carbon_copies) {
85  $cc_pieces = array_filter(array_map('trim', explode(',', $carbon_copies)));
86  foreach ($cc_pieces as $carbon_copy) {
87  if (!$this->getMailer()->AddCC($carbon_copy, '')) {
88  ilLoggerFactory::getLogger('mail')->warning($this->getMailer()->ErrorInfo);
89  }
90  }
91  }
92 
93  foreach ($mail->getBcc() as $blind_carbon_copies) {
94  $bcc_pieces = array_filter(array_map('trim', explode(',', $blind_carbon_copies)));
95  foreach ($bcc_pieces as $blind_carbon_copy) {
96  if (!$this->getMailer()->AddBCC($blind_carbon_copy, '')) {
97  ilLoggerFactory::getLogger('mail')->warning($this->getMailer()->ErrorInfo);
98  }
99  }
100  }
101 
102  $this->getMailer()->Subject = $mail->getSubject();
103 
104  if ($mail->getFrom()->hasReplyToAddress()) {
105  if (!$this->getMailer()->addReplyTo($mail->getFrom()->getReplyToAddress(), $mail->getFrom()->getReplyToName())) {
106  ilLoggerFactory::getLogger('mail')->warning($this->getMailer()->ErrorInfo);
107  }
108  }
109  if ($mail->getFrom()->hasEnvelopFromAddress()) {
110  $this->getMailer()->Sender = $mail->getFrom()->getEnvelopFromAddress();
111  }
112 
113  if (!$this->getMailer()->setFrom($mail->getFrom()->getFromAddress(), $mail->getFrom()->getFromName(), false)) {
114  ilLoggerFactory::getLogger('mail')->warning($this->getMailer()->ErrorInfo);
115  }
116 
117  foreach ($mail->getAttachments() as $attachment) {
118  if (!$this->getMailer()->AddAttachment($attachment['path'], $attachment['name'])) {
119  ilLoggerFactory::getLogger('mail')->warning($this->getMailer()->ErrorInfo);
120  }
121  }
122 
123  foreach ($mail->getImages() as $image) {
124  if (!$this->getMailer()->AddEmbeddedImage($image['path'], $image['cid'], $image['name'])) {
125  ilLoggerFactory::getLogger('mail')->warning($this->getMailer()->ErrorInfo);
126  }
127  }
128 
129  if ($mail->getFinalBodyAlt()) {
130  $this->getMailer()->IsHTML(true);
131  $this->getMailer()->AltBody = $mail->getFinalBodyAlt();
132  $this->getMailer()->Body = $mail->getFinalBody();
133  } else {
134  $this->getMailer()->IsHTML(false);
135  $this->getMailer()->AltBody = '';
136  $this->getMailer()->Body = $mail->getFinalBody();
137  }
138 
139  ilLoggerFactory::getLogger('mail')->info(sprintf(
140  "Trying to delegate external email delivery:" .
141  " Initiated by: %s (%s) " .
142  "| To: %s | CC: %s | BCC: %s | Subject: %s " .
143  "| From: %s / %s " .
144  "| ReplyTo: %s / %s " .
145  "| EnvelopeFrom: %s",
146  $GLOBALS['DIC']->user()->getLogin(),
147  $GLOBALS['DIC']->user()->getId(),
148  implode(', ', $mail->getTo()),
149  implode(', ', $mail->getCc()),
150  implode(', ', $mail->getBcc()),
151  $mail->getSubject(),
152  $mail->getFrom()->getFromAddress(),
153  $mail->getFrom()->getFromName(),
154  $mail->getFrom()->getReplyToAddress(),
155  $mail->getFrom()->getReplyToName(),
156  $mail->getFrom()->getEnvelopFromAddress()
157  ));
158 
159  ilLoggerFactory::getLogger('mail')->debug(sprintf("Mail Alternative Body: %s", $this->getMailer()->AltBody));
160  ilLoggerFactory::getLogger('mail')->debug(sprintf("Mail Body: %s", $this->getMailer()->Body));
161 
162  $this->getMailer()->CharSet = 'utf-8';
163 
164  $this->mailer->SMTPDebug = 4;
165  $this->mailer->Debugoutput = function ($message, $level) {
166  if (
167  strpos($message, 'Invalid address') !== false ||
168  strpos($message, 'Message body empty') !== false
169  ) {
170  ilLoggerFactory::getLogger('mail')->warning($message);
171  } else {
172  ilLoggerFactory::getLogger('mail')->debug($message);
173  }
174  };
175 
176  $this->onBeforeSend();
177  $result = $this->getMailer()->Send();
178  if ($result) {
179  ilLoggerFactory::getLogger('mail')->info(sprintf(
180  'Successfully delegated external mail delivery'
181  ));
182 
183  if (strlen($this->getMailer()->ErrorInfo) > 0) {
184  ilLoggerFactory::getLogger('mail')->warning(sprintf(
185  '... with most recent errors: %s',
186  $this->getMailer()->ErrorInfo
187  ));
188  }
189  } else {
190  ilLoggerFactory::getLogger('mail')->warning(sprintf(
191  'Could not deliver external email: %s',
192  $this->getMailer()->ErrorInfo
193  ));
194  }
195 
196  $this->eventHandler->raise('Services/Mail', 'externalEmailDelegated', [
197  'mail' => $mail,
198  'result' => (bool) $result
199  ]);
200 
201  return (bool) $result;
202  }
203 }
Global event handler.
settings()
Definition: settings.php:2
$result
user()
Definition: user.php:4
__construct(ilSetting $settings, ilAppEventHandler $eventHandler)
ilMailMimeTransportBase constructor.
Interface ilMailMimeTransport.
Class ilMimeMail.
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
Class ilMailMimeTransportBase.
$message
Definition: xapiexit.php:14
static getLogger($a_component_id)
Get component logger.