ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailMimeTransportBase.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 
10 {
12  protected $mailer;
13 
15  protected $settings;
16 
18  private $eventHandler;
19 
26  {
27  $this->settings = $settings;
28  $this->eventHandler = $eventHandler;
29 
30  $mail = new PHPMailer();
31  $this->setMailer($mail);
32  }
33 
37  protected function getMailer()
38  {
39  return $this->mailer;
40  }
41 
45  protected function setMailer($mailer)
46  {
47  $this->mailer = $mailer;
48  }
49 
50  protected function resetMailer()
51  {
52  $this->getMailer()->clearAllRecipients();
53  $this->getMailer()->clearAttachments();
54  $this->getMailer()->clearReplyTos();
55  $this->getMailer()->ErrorInfo = '';
56  }
57 
61  protected function onBeforeSend()
62  {
63  }
64 
68  final public function send(\ilMimeMail $mail) : bool
69  {
70  $this->resetMailer();
71 
72  $this->getMailer()->XMailer = ' ';
73 
74  foreach ($mail->getTo() as $recipients) {
75  $recipient_pieces = array_filter(array_map('trim', explode(',', $recipients)));
76  foreach ($recipient_pieces as $recipient) {
77  $this->getMailer()->AddAddress($recipient, '');
78  }
79  }
80 
81  foreach ($mail->getCc() as $carbon_copies) {
82  $cc_pieces = array_filter(array_map('trim', explode(',', $carbon_copies)));
83  foreach ($cc_pieces as $carbon_copy) {
84  $this->getMailer()->AddCC($carbon_copy, '');
85  }
86  }
87 
88  foreach ($mail->getBcc() as $blind_carbon_copies) {
89  $bcc_pieces = array_filter(array_map('trim', explode(',', $blind_carbon_copies)));
90  foreach ($bcc_pieces as $blind_carbon_copy) {
91  $this->getMailer()->AddBCC($blind_carbon_copy, '');
92  }
93  }
94 
95  $this->getMailer()->Subject = $mail->getSubject();
96 
97  if ($mail->getFrom()->hasReplyToAddress()) {
98  $this->getMailer()->addReplyTo($mail->getFrom()->getReplyToAddress(), $mail->getFrom()->getReplyToName());
99  }
100  if ($mail->getFrom()->hasEnvelopFromAddress()) {
101  $this->getMailer()->Sender = $mail->getFrom()->getEnvelopFromAddress();
102  }
103  $this->getMailer()->setFrom($mail->getFrom()->getFromAddress(), $mail->getFrom()->getFromName(), false);
104 
105  foreach ($mail->getAttachments() as $attachment) {
106  $this->getMailer()->AddAttachment($attachment['path'], $attachment['name']);
107  }
108 
109  foreach ($mail->getImages() as $image) {
110  $this->getMailer()->AddEmbeddedImage($image['path'], $image['cid'], $image['name']);
111  }
112 
113  if ($mail->getFinalBodyAlt()) {
114  $this->getMailer()->IsHTML(true);
115  $this->getMailer()->AltBody = $mail->getFinalBodyAlt();
116  $this->getMailer()->Body = $mail->getFinalBody();
117  } else {
118  $this->getMailer()->IsHTML(false);
119  $this->getMailer()->AltBody = '';
120  $this->getMailer()->Body = $mail->getFinalBody();
121  }
122 
123  ilLoggerFactory::getLogger('mail')->info(sprintf(
124  "Trying to delegate external email delivery:" .
125  " Initiated by: %s (%s) " .
126  "| To: %s | CC: %s | BCC: %s | Subject: %s " .
127  "| From: %s / %s " .
128  "| ReplyTo: %s / %s " .
129  "| EnvelopeFrom: %s",
130  $GLOBALS['DIC']->user()->getLogin(),
131  $GLOBALS['DIC']->user()->getId(),
132  implode(', ', $mail->getTo()),
133  implode(', ', $mail->getCc()),
134  implode(', ', $mail->getBcc()),
135  $mail->getSubject(),
136  $mail->getFrom()->getFromAddress(),
137  $mail->getFrom()->getFromName(),
138  $mail->getFrom()->getReplyToAddress(),
139  $mail->getFrom()->getReplyToName(),
140  $mail->getFrom()->getEnvelopFromAddress()
141  ));
142 
143  ilLoggerFactory::getLogger('mail')->debug(sprintf("Mail Alternative Body: %s", $this->getMailer()->AltBody));
144  ilLoggerFactory::getLogger('mail')->debug(sprintf("Mail Body: %s", $this->getMailer()->Body));
145 
146  $this->getMailer()->CharSet = 'utf-8';
147 
148  $this->mailer->SMTPDebug = 4;
149  $this->mailer->Debugoutput = function ($message, $level) {
150  if (
151  strpos($message, 'Invalid address') !== false ||
152  strpos($message, 'Message body empty') !== false
153  ) {
154  ilLoggerFactory::getLogger('mail')->warning($message);
155  } else {
156  ilLoggerFactory::getLogger('mail')->debug($message);
157  }
158  };
159 
160  $this->onBeforeSend();
161  $result = $this->getMailer()->Send();
162  if ($result) {
163  ilLoggerFactory::getLogger('mail')->info(sprintf(
164  'Successfully delegated external mail delivery'
165  ));
166 
167  if (strlen($this->getMailer()->ErrorInfo) > 0) {
168  ilLoggerFactory::getLogger('mail')->warning(sprintf(
169  '... with most recent errors: %s',
170  $this->getMailer()->ErrorInfo
171  ));
172  }
173  } else {
174  ilLoggerFactory::getLogger('mail')->warning(sprintf(
175  'Could not deliver external email: %s',
176  $this->getMailer()->ErrorInfo
177  ));
178  }
179 
180  $this->eventHandler->raise('Services/Mail', 'externalEmailDelegated', [
181  'mail' => $mail,
182  'result' => (bool) $result
183  ]);
184 
185  return (bool) $result;
186  }
187 }
Global event handler.
settings()
Definition: settings.php:2
$result
user()
Definition: user.php:4
PHPMailer - PHP email creation and transport class.
catch(Exception $e) $message
Interface ilMailMimeTransport.
Class ilMimeMail.
__construct(\ilSetting $settings, \ilAppEventHandler $eventHandler)
ilMailMimeTransportBase constructor.
Class ilMailMimeTransportBase.
static getLogger($a_component_id)
Get component logger.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.