ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 final public const string MAIL_SUBJECT_PREFIX = '[ILIAS]';
26
27 protected static ?ilMailMimeTransport $default_transport = null;
28
32 protected string $subject = '';
33 protected string $body = '';
34 protected string $final_body = '';
35 protected string $final_body_alt = '';
37 protected array $sendto = [];
39 protected array $acc = [];
41 protected array $abcc = [];
43 protected array $images = [];
45 protected array $aattach = [];
47 protected array $actype = [];
49 protected array $adispo = [];
51 protected array $adisplay = [];
52 private readonly Refinery $refinery;
53
54 public function __construct()
55 {
56 global $DIC;
57 $this->settings = $DIC->settings();
58
59 if (!(self::getDefaultTransport() instanceof ilMailMimeTransport)) {
60 $factory = $DIC->mail()->mime()->transportFactory();
61 self::setDefaultTransport($factory->getTransport());
62 }
63
64 $this->subject_builder = new ilMailMimeSubjectBuilder($this->settings, self::MAIL_SUBJECT_PREFIX);
65 $this->refinery = $DIC->refinery();
66 }
67
68 public static function setDefaultTransport(?ilMailMimeTransport $transport): void
69 {
70 self::$default_transport = $transport;
71 }
72
73 public static function getDefaultTransport(): ?ilMailMimeTransport
74 {
76 }
77
78 public function Subject(string $subject, bool $add_prefix = false, string $context_prefix = ''): void
79 {
80 $this->subject = $this->subject_builder->subject($subject, $add_prefix, $context_prefix);
81 }
82
83 public function getSubject(): string
84 {
85 return $this->subject;
86 }
87
88 public function From(ilMailMimeSender $sender): void
89 {
90 $this->sender = $sender;
91 }
92
96 public function To($to): void
97 {
98 if (is_array($to)) {
99 $this->sendto = $to;
100 } else {
101 $this->sendto[] = $to;
102 }
103 }
104
108 public function Cc($cc): void
109 {
110 if (is_array($cc)) {
111 $this->acc = $cc;
112 } else {
113 $this->acc[] = $cc;
114 }
115 }
116
120 public function Bcc($bcc): void
121 {
122 if (is_array($bcc)) {
123 $this->abcc = $bcc;
124 } else {
125 $this->abcc[] = $bcc;
126 }
127 }
128
132 public function getTo(): array
133 {
134 return $this->sendto;
135 }
136
140 public function getCc(): array
141 {
142 return $this->acc;
143 }
144
148 public function getBcc(): array
149 {
150 return $this->abcc;
151 }
152
153 public function Body(string $body): void
154 {
155 $this->body = $body;
156 }
157
158 public function getFinalBody(): string
159 {
160 return $this->final_body;
161 }
162
163 public function getFinalBodyalt(): string
164 {
166 }
167
168 public function getFrom(): ilMailMimeSender
169 {
170 return $this->sender;
171 }
172
180 public function Attach(
181 string $filename,
182 string $file_type = '',
183 string $disposition = 'inline',
184 ?string $display_name = null
185 ): void {
186 if ($file_type === '') {
187 $file_type = 'application/octet-stream';
188 }
189
190 $this->aattach[] = $filename;
191 $this->actype[] = $file_type;
192 $this->adispo[] = $disposition;
193 $this->adisplay[] = $display_name;
194 }
195
199 public function getAttachments(): array
200 {
201 $attachments = [];
202
203 $i = 0;
204 foreach ($this->aattach as $attachment) {
205 $name = '';
206 if (isset($this->adisplay[$i]) && is_string($this->adisplay[$i]) && $this->adisplay[$i] !== '') {
207 $name = $this->adisplay[$i];
208 }
209
210 $attachments[] = [
211 'path' => $attachment,
212 'name' => $name
213 ];
214 ++$i;
215 }
216
217 return $attachments;
218 }
219
224 public function getImages(): array
225 {
226 return array_values($this->images);
227 }
228
229 protected function build(): void
230 {
231 global $DIC;
232
233 $this->final_body_alt = '';
234 $this->final_body = '';
235 $this->images = [];
236
237 if ($DIC->settings()->get('mail_send_html', '0')) {
238 $skin = $DIC['ilClientIniFile']->readVariable('layout', 'skin');
239 $style = $DIC['ilClientIniFile']->readVariable('layout', 'style');
240
241 $this->buildBodyMultiParts($skin, $style);
242 $this->buildHtmlInlineImages($skin, $style);
243 } else {
244 $this->final_body = $this->removeHTMLTags($this->body);
245 }
246 }
247
248 private function removeHTMLTags(string $maybe_html): string
249 {
250 $maybe_html = str_ireplace(['<br />', '<br>', '<br/>'], "\n", $maybe_html);
251
252 return strip_tags($maybe_html);
253 }
254
255 protected function buildBodyMultiParts(string $skin, string $style): void
256 {
257 if ($this->body === '') {
258 $this->body = ' ';
259 }
260
261 if (strip_tags($this->body, '<b><u><i><a>') === $this->body) {
262 // Let's assume(!) that there is no HTML
263 // (except certain tags, e.g. used for object title formatting, where the consumer is not aware of this),
264 // so convert "\n" to "<br>"
265 $this->final_body_alt = strip_tags($this->body);
266 $this->body = $this->refinery->string()->makeClickable()->transform(nl2br($this->body));
267 } else {
268 // if there is HTML, convert "<br>" to "\n" and strip tags for plain text alternative
269 $this->final_body_alt = strip_tags(str_ireplace(["<br />", "<br>", "<br/>"], "\n", $this->body));
270 }
271
272 $this->final_body = str_replace('{PLACEHOLDER}', $this->body, $this->getHtmlEnvelope($skin, $style));
273 }
274
275 private function getPathToRootDirectory(): string
276 {
277 return realpath(dirname(__DIR__, 4) . '/');
278 }
279
280 protected function getHtmlEnvelope(string $skin, string $style): string
281 {
282 $bracket_path = $this->getPathToRootDirectory() . '/components/ILIAS/Mail/templates/default/tpl.html_mail_template.html';
283 if ($skin !== 'default') {
284 $locations = [
285 $skin,
286 $skin . '/' . $style
287 ];
288
289 foreach ($locations as $location) {
290 $custom_path = $this->getPathToRootDirectory(
291 ) . '/public/Customizing/skin/' . $location . '/components/ILIAS/Mail/tpl.html_mail_template.html';
292 if (is_file($custom_path)) {
293 $bracket_path = $custom_path;
294 break;
295 }
296 }
297 }
298
299 return file_get_contents($bracket_path);
300 }
301
302 protected function buildHtmlInlineImages(string $skin, string $style): void
303 {
304 $this->gatherImagesFromDirectory(
305 $this->getPathToRootDirectory() . '/components/ILIAS/Mail/templates/default/img'
306 );
307
308 if ($skin !== 'default') {
309 $locations = [
310 $skin,
311 $skin . '/' . $style
312 ];
313
314 foreach ($locations as $location) {
315 $custom_directory = $this->getPathToRootDirectory(
316 ) . '/public/Customizing/skin/' . $location . '/components/ILIAS/Mail/img';
317 if (is_dir($custom_directory) && is_readable($custom_directory)) {
318 $this->gatherImagesFromDirectory($custom_directory, true);
319 break;
320 }
321 }
322 }
323 }
324
325 protected function gatherImagesFromDirectory(string $directory, bool $clear_previous = false): void
326 {
327 if ($clear_previous) {
328 $this->images = [];
329 }
330
331 foreach (new RegexIterator(
332 new DirectoryIterator($directory),
333 '/\.(jpg|jpeg|gif|svg|png)$/i'
334 ) as $file) {
336 $cid = 'img/' . $file->getFilename();
337
338 $this->images[$cid] = [
339 'path' => $file->getPathname(),
340 'cid' => $cid,
341 'name' => $file->getFilename()
342 ];
343 }
344 }
345
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:78
$location
Definition: buildRTE.php:22
Builds data types.
Definition: Factory.php:36
Attach(string $filename, string $file_type='', string $disposition='inline', ?string $display_name=null)
final const string MAIL_SUBJECT_PREFIX
Body(string $body)
removeHTMLTags(string $maybe_html)
ilMailMimeSender $sender
string $final_body_alt
Send(?ilMailMimeTransport $transport=null)
buildBodyMultiParts(string $skin, string $style)
static ilMailMimeTransport $default_transport
static getDefaultTransport()
static setDefaultTransport(?ilMailMimeTransport $transport)
Subject(string $subject, bool $add_prefix=false, string $context_prefix='')
ilMailMimeSubjectBuilder $subject_builder
string $final_body
readonly Refinery $refinery
From(ilMailMimeSender $sender)
ilSetting $settings
getHtmlEnvelope(string $skin, string $style)
buildHtmlInlineImages(string $skin, string $style)
ILIAS Setting Class.
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26