ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
4 require_once 'Services/Mail/classes/Mime/Transport/interface.ilMailMimeTransport.php';
5 require_once 'Services/Logging/classes/public/class.ilLoggerFactory.php';
6 
11 {
15  protected $mailer;
16 
20  protected $settings;
21 
26  public function __construct(\ilSetting $settings)
27  {
28  $this->settings = $settings;
29 
30  require_once 'libs/composer/vendor/autoload.php';
31  $mail = new PHPMailer();
32  $this->setMailer($mail);
33  }
34 
38  protected function getMailer()
39  {
40  return $this->mailer;
41  }
42 
46  protected function setMailer($mailer)
47  {
48  $this->mailer = $mailer;
49  }
50 
51  protected function resetMailer()
52  {
53  $this->getMailer()->clearAllRecipients();
54  $this->getMailer()->clearAttachments();
55  $this->getMailer()->clearReplyTos();
56  }
57 
61  protected function onBeforeSend()
62  {
63  }
64 
68  final public function send(ilMimeMail $mail)
69  {
70  $this->resetMailer();
71 
72  foreach ($mail->getTo() as $recipients) {
73  $recipient_pieces = array_filter(array_map('trim', explode(',', $recipients)));
74  foreach ($recipient_pieces as $recipient) {
75  $this->getMailer()->AddAddress($recipient, '');
76  }
77  }
78 
79  foreach ($mail->getCc() as $carbon_copies) {
80  $cc_pieces = array_filter(array_map('trim', explode(',', $carbon_copies)));
81  foreach ($cc_pieces as $carbon_copy) {
82  $this->getMailer()->AddCC($carbon_copy, '');
83  }
84  }
85 
86  foreach ($mail->getBcc() as $blind_carbon_copies) {
87  $bcc_pieces = array_filter(array_map('trim', explode(',', $blind_carbon_copies)));
88  foreach ($bcc_pieces as $blind_carbon_copy) {
89  $this->getMailer()->AddBCC($blind_carbon_copy, '');
90  }
91  }
92 
93  $this->getMailer()->Subject = $mail->getSubject();
94 
95  if ($mail->getFrom()->hasReplyToAddress()) {
96  $this->getMailer()->addReplyTo($mail->getFrom()->getReplyToAddress(), $mail->getFrom()->getReplyToName());
97  }
98  if ($mail->getFrom()->hasEnvelopFromAddress()) {
99  $this->getMailer()->Sender = $mail->getFrom()->getEnvelopFromAddress();
100  }
101  $this->getMailer()->setFrom($mail->getFrom()->getFromAddress(), $mail->getFrom()->getFromName(), false);
102 
103  foreach ($mail->getAttachments() as $attachment) {
104  $this->getMailer()->AddAttachment($attachment['path'], $attachment['name']);
105  }
106 
107  foreach ($mail->getImages() as $image) {
108  $this->getMailer()->AddEmbeddedImage($image['path'], $image['cid'], $image['name']);
109  }
110 
111  if ($mail->getFinalBodyAlt()) {
112  $this->getMailer()->IsHTML(true);
113  $this->getMailer()->AltBody = $mail->getFinalBodyAlt();
114  $this->getMailer()->Body = $mail->getFinalBody();
115  } else {
116  $this->getMailer()->IsHTML(false);
117  $this->getMailer()->AltBody = '';
118  $this->getMailer()->Body = $mail->getFinalBody();
119  }
120 
121  ilLoggerFactory::getLogger('mail')->info(sprintf(
122  "Trying to delegate external email delivery:" .
123  " Initiated by: %s (%s) " .
124  "| To: %s | CC: %s | BCC: %s | Subject: %s " .
125  "| From: %s / %s " .
126  "| ReplyTo: %s / %s " .
127  "| EnvelopeFrom: %s",
128  $GLOBALS['DIC']->user()->getLogin(),
129  $GLOBALS['DIC']->user()->getId(),
130  implode(', ', $mail->getTo()),
131  implode(', ', $mail->getCc()),
132  implode(', ', $mail->getBcc()),
133  $mail->getSubject(),
134  $mail->getFrom()->getFromAddress(),
135  $mail->getFrom()->getFromName(),
136  $mail->getFrom()->getReplyToAddress(),
137  $mail->getFrom()->getReplyToName(),
138  $mail->getFrom()->getEnvelopFromAddress()
139  ));
140 
141  $this->getMailer()->CharSet = 'utf-8';
142 
143  $this->mailer->SMTPDebug = 4;
144  $this->mailer->Debugoutput = function ($message, $level) {
145  ilLoggerFactory::getLogger('mail')->debug($message);
146  };
147 
148  $this->onBeforeSend();
149  $result = $this->getMailer()->Send();
150  if ($result) {
151  ilLoggerFactory::getLogger('mail')->info(sprintf(
152  'Successfully delegated external mail delivery'
153  ));
154  } else {
155  ilLoggerFactory::getLogger('mail')->warning(sprintf(
156  'Could not deliver external email: %s',
157  $this->getMailer()->ErrorInfo
158  ));
159  }
160 
161  return $result;
162  }
163 }
$result
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
__construct(\ilSetting $settings)
ilMailMimeTransportBase constructor.
user()
Definition: user.php:4
PHPMailer - PHP email creation and transport class.
catch(Exception $e) $message
Interface ilMailMimeTransport.
Class ilMimeMail.
Class ilMailMimeTransportBase.
settings()
Definition: settings.php:2
static getLogger($a_component_id)
Get component logger.