ILIAS  release_7 Revision v7.30-3-g800a261c036
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
5use PHPMailer\PHPMailer\PHPMailer;
6
11{
13 protected $mailer;
14
16 protected $settings;
17
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->Debugoutput = function ($message, $level) {
165 if (
166 strpos($message, 'Invalid address') !== false ||
167 strpos($message, 'Message body empty') !== false
168 ) {
169 ilLoggerFactory::getLogger('mail')->warning($message);
170 } else {
171 ilLoggerFactory::getLogger('mail')->debug($message);
172 }
173 };
174
175 $this->onBeforeSend();
176 $result = $this->getMailer()->Send();
177 if ($result) {
178 ilLoggerFactory::getLogger('mail')->info(sprintf(
179 'Successfully delegated external mail delivery'
180 ));
181
182 if (strlen($this->getMailer()->ErrorInfo) > 0) {
183 ilLoggerFactory::getLogger('mail')->warning(sprintf(
184 '... with most recent errors: %s',
185 $this->getMailer()->ErrorInfo
186 ));
187 }
188 } else {
189 ilLoggerFactory::getLogger('mail')->warning(sprintf(
190 'Could not deliver external email: %s',
191 $this->getMailer()->ErrorInfo
192 ));
193 }
194
195 $this->eventHandler->raise('Services/Mail', 'externalEmailDelegated', [
196 'mail' => $mail,
197 'result' => (bool) $result
198 ]);
199
200 return (bool) $result;
201 }
202}
$result
user()
Definition: user.php:4
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
Global event handler.
static getLogger($a_component_id)
Get component logger.
Class ilMailMimeTransportBase.
__construct(ilSetting $settings, ilAppEventHandler $eventHandler)
ilMailMimeTransportBase constructor.
Class ilMimeMail.
ILIAS Setting Class.
Interface ilMailMimeTransport.
settings()
Definition: settings.php:2
$message
Definition: xapiexit.php:14