ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMimeMail.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
9 public const MAIL_SUBJECT_PREFIX = '[ILIAS]';
10
12 protected static $defaultTransport = null;
13
15 protected $subject = '';
16
18 protected $body = '';
19
21 protected $finalBody = '';
22
24 protected $finalBodyAlt = '';
25
27 protected $sendto = [];
28
30 protected $acc = [];
31
33 protected $abcc = [];
34
36 protected $images = [];
37
39 protected $aattach = [];
40
42 protected $actype = [];
43
45 protected $adispo = [];
46
48 protected $adisplay = [];
49
51 protected $sender;
52
54 protected $settings;
55
57 protected $subjectBuilder;
58
62 public function __construct()
63 {
64 global $DIC;
65
66 $this->settings = $DIC->settings();
67
68 if (!(self::getDefaultTransport() instanceof ilMailMimeTransport)) {
69 $factory = $DIC["mail.mime.transport.factory"];
70 self::setDefaultTransport($factory->getTransport());
71 }
72
73 $this->subjectBuilder = new ilMailMimeSubjectBuilder($this->settings, self::MAIL_SUBJECT_PREFIX);
74 }
75
80 public static function setDefaultTransport(?ilMailMimeTransport $transport) : void
81 {
82 if ($transport !== null && !($transport instanceof ilMailMimeTransport)) {
83 throw new InvalidArgumentException(sprintf(
84 "The passed argument must be null or of type 'ilMailMimeTransport', %s given!",
85 gettype($transport)
86 ));
87 }
88
89 self::$defaultTransport = $transport;
90 }
91
92 public static function getDefaultTransport() : ?ilMailMimeTransport
93 {
95 }
96
97 public function Subject(string $subject, bool $addPrefix = false, string $contextPrefix = '') : void
98 {
99 $this->subject = $this->subjectBuilder->subject($subject, $addPrefix, $contextPrefix);
100 }
101
102 public function getSubject() : string
103 {
104 return $this->subject;
105 }
106
107 public function From(ilMailMimeSender $sender) : void
108 {
109 $this->sender = $sender;
110 }
111
116 public function To($to) : void
117 {
118 if (is_array($to)) {
119 $this->sendto = $to;
120 } else {
121 $this->sendto[] = $to;
122 }
123 }
124
129 public function Cc($cc) : void
130 {
131 if (is_array($cc)) {
132 $this->acc = $cc;
133 } else {
134 $this->acc[] = $cc;
135 }
136 }
137
142 public function Bcc($bcc) : void
143 {
144 if (is_array($bcc)) {
145 $this->abcc = $bcc;
146 } else {
147 $this->abcc[] = $bcc;
148 }
149 }
150
154 public function getTo() : array
155 {
156 return $this->sendto;
157 }
158
162 public function getCc() : array
163 {
164 return $this->acc;
165 }
166
170 public function getBcc() : array
171 {
172 return $this->abcc;
173 }
174
175 public function Body(string $body) : void
176 {
177 $this->body = $body;
178 }
179
180 public function getFinalBody() : string
181 {
182 return $this->finalBody;
183 }
184
185 public function getFinalBodyAlt() : string
186 {
187 return $this->finalBodyAlt;
188 }
189
190 public function getFrom() : ilMailMimeSender
191 {
192 return $this->sender;
193 }
194
202 public function Attach(
203 string $filename,
204 string $file_type = '',
205 string $disposition = 'inline',
206 ?string $display_name = null
207 ) : void {
208 if ($file_type === '') {
209 $file_type = 'application/octet-stream';
210 }
211
212 $this->aattach[] = $filename;
213 $this->actype[] = $file_type;
214 $this->adispo[] = $disposition;
215 $this->adisplay[] = $display_name;
216 }
217
221 public function getAttachments() : array
222 {
223 $attachments = [];
224
225 $i = 0;
226 foreach ($this->aattach as $attachment) {
227 $name = '';
228 if (isset($this->adisplay[$i]) && is_string($this->adisplay[$i]) && $this->adisplay[$i] !== '') {
229 $name = $this->adisplay[$i];
230 }
231
232 $attachments[] = [
233 'path' => $attachment,
234 'name' => $name
235 ];
236 ++$i;
237 }
238
239 return $attachments;
240 }
241
245 public function getImages() : array
246 {
247 return array_values($this->images);
248 }
249
253 protected function build() : void
254 {
255 global $DIC;
256
257 $this->finalBodyAlt = '';
258 $this->finalBody = '';
259 $this->images = [];
260
261 if ($DIC->settings()->get('mail_send_html', 0)) {
262 $skin = $DIC['ilClientIniFile']->readVariable('layout', 'skin');
263
264 $this->buildBodyMultiParts($skin);
265 $this->buildHtmlInlineImages($skin);
266 } else {
267 $this->finalBody = $this->removeHTMLTags($this->body);
268 }
269 }
270
271 private function removeHTMLTags(string $maybeHTML) : string
272 {
273 $maybeHTML = str_ireplace(['<br />', '<br>', '<br/>'], "\n", $maybeHTML);
274 $maybeHTML = strip_tags($maybeHTML);
275
276 return $maybeHTML;
277 }
278
279 protected function buildBodyMultiParts(string $skin) : void
280 {
281 if ($this->body === '') {
282 $this->body = ' ';
283 }
284
285 if (strip_tags($this->body, '<b><u><i><a>') === $this->body) {
286 // Let's assume(!) that there is no HTML (except certain tags, e.g. used for object title formatting, where the consumer is not aware of this), so convert "\n" to "<br>"
287 $this->finalBodyAlt = strip_tags($this->body);
288 $this->body = \ilUtil::makeClickable(nl2br($this->body));
289 } else {
290 // if there is HTML, convert "<br>" to "\n" and strip tags for plain text alternative
291 $this->finalBodyAlt = strip_tags(str_ireplace(["<br />", "<br>", "<br/>"], "\n", $this->body));
292 }
293
294 $this->finalBody = str_replace('{PLACEHOLDER}', $this->body, $this->getHtmlEnvelope($skin));
295 }
296
297 protected function getHtmlEnvelope(string $skin) : string
298 {
299 $bracket_path = './Services/Mail/templates/default/tpl.html_mail_template.html';
300
301 if ($skin !== 'default') {
302 $tplpath = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
303
304 if (file_exists($tplpath)) {
305 $bracket_path = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
306 }
307 }
308
309 return file_get_contents($bracket_path);
310 }
311
312 protected function buildHtmlInlineImages(string $skin) : void
313 {
314 $this->gatherImagesFromDirectory('./Services/Mail/templates/default/img');
315
316 if ($skin !== 'default') {
317 $skinDirectory = './Customizing/global/skin/' . $skin . '/Services/Mail/img';
318 if (is_dir($skinDirectory) && is_readable($skinDirectory)) {
319 $this->gatherImagesFromDirectory($skinDirectory, true);
320 }
321 }
322 }
323
324 protected function gatherImagesFromDirectory(string $directory, bool $clearPrevious = false) : void
325 {
326 if ($clearPrevious) {
327 $this->images = [];
328 }
329
330 foreach (new RegexIterator(new DirectoryIterator($directory), '/\.(jpg|jpeg|gif|svg|png)$/i') as $file) {
332 $cid = 'img/' . $file->getFilename();
333
334 $this->images[$cid] = [
335 'path' => $file->getPathname(),
336 'cid' => $cid,
337 'name' => $file->getFilename()
338 ];
339 }
340 }
341
346 public function Send(ilMailMimeTransport $transport = null) : bool
347 {
348 if (!($transport instanceof ilMailMimeTransport)) {
349 $transport = self::getDefaultTransport();
350 }
351
352 $this->build();
353
354 return $transport->send($this);
355 }
356}
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
Class ilMailMimeSubjectBuilder.
Class ilMimeMail.
Attach(string $filename, string $file_type='', string $disposition='inline', ?string $display_name=null)
Attach a file to the mail.
buildHtmlInlineImages(string $skin)
build()
Build the relevant email data.
Body(string $body)
static $defaultTransport
Send(ilMailMimeTransport $transport=null)
Cc($cc)
Set the cc mail recipient.
buildBodyMultiParts(string $skin)
removeHTMLTags(string $maybeHTML)
static getDefaultTransport()
static setDefaultTransport(?ilMailMimeTransport $transport)
To($to)
Set the mail recipient.
const MAIL_SUBJECT_PREFIX
Bcc($bcc)
Set the bcc mail recipient.
__construct()
ilMimeMail constructor.
From(ilMailMimeSender $sender)
getHtmlEnvelope(string $skin)
Subject(string $subject, bool $addPrefix=false, string $contextPrefix='')
static makeClickable($a_text, $detectGotoLinks=false)
makeClickable In Texten enthaltene URLs und Mail-Adressen klickbar machen
if(!file_exists(getcwd() . '/ilias.ini.php'))
registration confirmation script for ilias
Definition: confirmReg.php:12
global $DIC
Definition: goto.php:24
Interface ilMailMimeTransport.
Interface ilMailMimeTransport.
$factory
Definition: metadata.php:58
if($format !==null) $name
Definition: metadata.php:230
$i
Definition: metadata.php:24
settings()
Definition: settings.php:2