ILIAS  release_8 Revision v8.24
class.ilMimeMail.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Refinery\Factory as Refinery;
22
24{
25 public const MAIL_SUBJECT_PREFIX = '[ILIAS]';
26 protected static ?ilMailMimeTransport $defaultTransport = null;
30 protected string $subject = '';
31 protected string $body = '';
32 protected string $finalBody = '';
33 protected string $finalBodyAlt = '';
35 protected array $sendto = [];
37 protected array $acc = [];
39 protected array $abcc = [];
41 protected array $images = [];
43 protected array $aattach = [];
45 protected array $actype = [];
47 protected array $adispo = [];
49 protected array $adisplay = [];
50 private Refinery $refinery;
51
52 public function __construct()
53 {
54 global $DIC;
55 $this->settings = $DIC->settings();
56
57 if (!(self::getDefaultTransport() instanceof ilMailMimeTransport)) {
58 $factory = $DIC["mail.mime.transport.factory"];
59 self::setDefaultTransport($factory->getTransport());
60 }
61
62 $this->subjectBuilder = new ilMailMimeSubjectBuilder($this->settings, self::MAIL_SUBJECT_PREFIX);
63 $this->refinery = $DIC->refinery();
64 }
65
66 public static function setDefaultTransport(?ilMailMimeTransport $transport): void
67 {
68 self::$defaultTransport = $transport;
69 }
70
71 public static function getDefaultTransport(): ?ilMailMimeTransport
72 {
74 }
75
76 public function Subject(string $subject, bool $addPrefix = false, string $contextPrefix = ''): void
77 {
78 $this->subject = $this->subjectBuilder->subject($subject, $addPrefix, $contextPrefix);
79 }
80
81 public function getSubject(): string
82 {
83 return $this->subject;
84 }
85
86 public function From(ilMailMimeSender $sender): void
87 {
88 $this->sender = $sender;
89 }
90
94 public function To($to): void
95 {
96 if (is_array($to)) {
97 $this->sendto = $to;
98 } else {
99 $this->sendto[] = $to;
100 }
101 }
102
106 public function Cc($cc): void
107 {
108 if (is_array($cc)) {
109 $this->acc = $cc;
110 } else {
111 $this->acc[] = $cc;
112 }
113 }
114
118 public function Bcc($bcc): void
119 {
120 if (is_array($bcc)) {
121 $this->abcc = $bcc;
122 } else {
123 $this->abcc[] = $bcc;
124 }
125 }
126
130 public function getTo(): array
131 {
132 return $this->sendto;
133 }
134
138 public function getCc(): array
139 {
140 return $this->acc;
141 }
142
146 public function getBcc(): array
147 {
148 return $this->abcc;
149 }
150
151 public function Body(string $body): void
152 {
153 $this->body = $body;
154 }
155
156 public function getFinalBody(): string
157 {
158 return $this->finalBody;
159 }
160
161 public function getFinalBodyAlt(): string
162 {
163 return $this->finalBodyAlt;
164 }
165
166 public function getFrom(): ilMailMimeSender
167 {
168 return $this->sender;
169 }
170
178 public function Attach(
179 string $filename,
180 string $file_type = '',
181 string $disposition = 'inline',
182 ?string $display_name = null
183 ): void {
184 if ($file_type === '') {
185 $file_type = 'application/octet-stream';
186 }
187
188 $this->aattach[] = $filename;
189 $this->actype[] = $file_type;
190 $this->adispo[] = $disposition;
191 $this->adisplay[] = $display_name;
192 }
193
197 public function getAttachments(): array
198 {
199 $attachments = [];
200
201 $i = 0;
202 foreach ($this->aattach as $attachment) {
203 $name = '';
204 if (isset($this->adisplay[$i]) && is_string($this->adisplay[$i]) && $this->adisplay[$i] !== '') {
205 $name = $this->adisplay[$i];
206 }
207
208 $attachments[] = [
209 'path' => $attachment,
210 'name' => $name
211 ];
212 ++$i;
213 }
214
215 return $attachments;
216 }
217
222 public function getImages(): array
223 {
224 return array_values($this->images);
225 }
226
227 protected function build(): void
228 {
229 global $DIC;
230
231 $this->finalBodyAlt = '';
232 $this->finalBody = '';
233 $this->images = [];
234
235 if ($DIC->settings()->get('mail_send_html', '0')) {
236 $skin = $DIC['ilClientIniFile']->readVariable('layout', 'skin');
237
238 $this->buildBodyMultiParts($skin);
239 $this->buildHtmlInlineImages($skin);
240 } else {
241 $this->finalBody = $this->removeHTMLTags($this->body);
242 }
243 }
244
245 private function removeHTMLTags(string $maybeHTML): string
246 {
247 $maybeHTML = str_ireplace(['<br />', '<br>', '<br/>'], "\n", $maybeHTML);
248
249 return strip_tags($maybeHTML);
250 }
251
252 protected function buildBodyMultiParts(string $skin): void
253 {
254 if ($this->body === '') {
255 $this->body = ' ';
256 }
257
258 if (strip_tags($this->body, '<b><u><i><a>') === $this->body) {
259 // Let's assume(!) that there is no HTML
260 // (except certain tags, e.g. used for object title formatting, where the consumer is not aware of this),
261 // so convert "\n" to "<br>"
262 $this->finalBodyAlt = strip_tags($this->body);
263 $this->body = $this->refinery->string()->makeClickable()->transform(nl2br($this->body));
264 } else {
265 // if there is HTML, convert "<br>" to "\n" and strip tags for plain text alternative
266 $this->finalBodyAlt = strip_tags(str_ireplace(["<br />", "<br>", "<br/>"], "\n", $this->body));
267 }
268
269 $this->finalBody = str_replace('{PLACEHOLDER}', $this->body, $this->getHtmlEnvelope($skin));
270 }
271
272 protected function getHtmlEnvelope(string $skin): string
273 {
274 $bracket_path = './Services/Mail/templates/default/tpl.html_mail_template.html';
275
276 if ($skin !== 'default') {
277 $tplpath = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
278
279 if (is_file($tplpath)) {
280 $bracket_path = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
281 }
282 }
283
284 return file_get_contents($bracket_path);
285 }
286
287 protected function buildHtmlInlineImages(string $skin): void
288 {
289 $this->gatherImagesFromDirectory('./Services/Mail/templates/default/img');
290
291 if ($skin !== 'default') {
292 $skinDirectory = './Customizing/global/skin/' . $skin . '/Services/Mail/img';
293 if (is_dir($skinDirectory) && is_readable($skinDirectory)) {
294 $this->gatherImagesFromDirectory($skinDirectory, true);
295 }
296 }
297 }
298
299 protected function gatherImagesFromDirectory(string $directory, bool $clearPrevious = false): void
300 {
301 if ($clearPrevious) {
302 $this->images = [];
303 }
304
305 foreach (new RegexIterator(
306 new DirectoryIterator($directory),
307 '/\.(jpg|jpeg|gif|svg|png)$/i'
308 ) as $file) {
310 $cid = 'img/' . $file->getFilename();
311
312 $this->images[$cid] = [
313 'path' => $file->getPathname(),
314 'cid' => $cid,
315 'name' => $file->getFilename()
316 ];
317 }
318 }
319
320 public function Send(ilMailMimeTransport $transport = null): bool
321 {
322 if (!($transport instanceof ilMailMimeTransport)) {
323 $transport = self::getDefaultTransport();
324 }
325
326 $this->build();
327
328 return $transport->send($this);
329 }
330}
$filename
Definition: buildRTE.php:78
Builds data types.
Definition: Factory.php:21
Class ilMailMimeSubjectBuilder.
Refinery $refinery
Attach(string $filename, string $file_type='', string $disposition='inline', ?string $display_name=null)
buildHtmlInlineImages(string $skin)
Body(string $body)
ilMailMimeSender $sender
Send(ilMailMimeTransport $transport=null)
ilMailMimeSubjectBuilder $subjectBuilder
buildBodyMultiParts(string $skin)
removeHTMLTags(string $maybeHTML)
static getDefaultTransport()
string $finalBodyAlt
static setDefaultTransport(?ilMailMimeTransport $transport)
const MAIL_SUBJECT_PREFIX
From(ilMailMimeSender $sender)
getHtmlEnvelope(string $skin)
ilSetting $settings
Subject(string $subject, bool $addPrefix=false, string $contextPrefix='')
static ilMailMimeTransport $defaultTransport
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
Interface ilMailMimeTransport.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$factory
Definition: metadata.php:75
if($format !==null) $name
Definition: metadata.php:247
$i
Definition: metadata.php:41