ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ilMimeMail Class Reference
+ Collaboration diagram for ilMimeMail:

Public Member Functions

 __construct ()
 
 Subject (string $subject, bool $add_prefix=false, string $context_prefix='')
 
 getSubject ()
 
 From (ilMailMimeSender $sender)
 
 To ($to)
 
 Cc ($cc)
 
 Bcc ($bcc)
 
 getTo ()
 
 getCc ()
 
 getBcc ()
 
 Body (string $body, ?Closure $to_html_transformation=null)
 
 getFinalBody ()
 
 getFinalBodyalt ()
 
 getFrom ()
 
 Attach (string $filename, string $file_type='', string $disposition='inline', ?string $display_name=null)
 
 getAttachments ()
 
 getImages ()
 
 Send (?ilMailMimeTransport $transport=null)
 

Static Public Member Functions

static setDefaultTransport (?ilMailMimeTransport $transport)
 
static getDefaultTransport ()
 

Data Fields

final const string MAIL_SUBJECT_PREFIX = '[ILIAS]'
 

Protected Member Functions

 build ()
 
 gatherImagesFromDirectory (string $directory, bool $clear_previous=false)
 

Protected Attributes

ilMailMimeSender $sender
 
ilMailMimeSubjectBuilder $subject_builder
 
ilSetting $settings
 
string $subject = ''
 
string $body = ''
 
string $final_body = ''
 
string $final_body_alt = ''
 
array $sendto = []
 
array $acc = []
 
array $abcc = []
 
array $images = []
 
array $aattach = []
 
array $actype = []
 
array $adispo = []
 
array $adisplay = []
 

Static Protected Attributes

static ilMailMimeTransport $default_transport = null
 

Private Member Functions

 removeHtmlTags (string $maybe_html)
 
 getPathToRootDirectory ()
 
 prepareHTMLBody ()
 
 containsHtmlBlockElementsOrLineBreaks (string $email_body)
 
 getStyleSheetPath (string $skin, string $style)
 
 getLogoCid (string $skin, string $style)
 
 addImage (SplFileInfo $file, bool $as_logo=false)
 

Private Attributes

const string SKIN_LOGO_PATH = '/public/Customizing/skin/%s/images/logo'
 
const string SKIN_CSS_PATH = '/public/Customizing/skin/%s/mail.css'
 
const string MAIL_CSS_PATH = 'assets/css/mail.css'
 
const string MAIL_LOGO_PATH = '/public/assets/images/logo/HeaderIcon.svg'
 
const string ROOT_DIR_IDENTIFICATION_FILE = '/ilias_version.php'
 
readonly Refinery $refinery
 
Closure $to_html_transformation = null
 

Detailed Description

Definition at line 24 of file class.ilMimeMail.php.

Constructor & Destructor Documentation

◆ __construct()

ilMimeMail::__construct ( )

Definition at line 62 of file class.ilMimeMail.php.

63 {
64 global $DIC;
65 $this->settings = $DIC->settings();
66
67 if (!(self::getDefaultTransport() instanceof ilMailMimeTransport)) {
68 $factory = $DIC->mail()->mime()->transportFactory();
69 self::setDefaultTransport($factory->getTransport());
70 }
71
72 $this->subject_builder = new ilMailMimeSubjectBuilder($this->settings, self::MAIL_SUBJECT_PREFIX);
73 $this->refinery = $DIC->refinery();
74 }
static setDefaultTransport(?ilMailMimeTransport $transport)
global $DIC
Definition: shib_login.php:26

References $DIC, ILIAS\Repository\refinery(), setDefaultTransport(), and ILIAS\Repository\settings().

+ Here is the call graph for this function:

Member Function Documentation

◆ addImage()

ilMimeMail::addImage ( SplFileInfo  $file,
bool  $as_logo = false 
)
private

Definition at line 414 of file class.ilMimeMail.php.

414 : string
415 {
416 $cid = 'img/' . $file->getFilename();
417
418 $this->images[$cid] = [
419 'path' => $file->getPathname(),
420 'cid' => $cid,
421 'name' => $file->getFilename(),
422 'as_logo' => $as_logo
423 ];
424
425 return $cid;
426 }

◆ Attach()

ilMimeMail::Attach ( string  $filename,
string  $file_type = '',
string  $disposition = 'inline',
?string  $display_name = null 
)
Parameters
string$filenamePath of the file to attach
string$file_typeMIME-type of the file. default to 'application/x-unknown-content-type'
string$dispositionInstruct the Mailclient to display the file if possible ("inline") or always as a link ("attachment") possible values are "inline", "attachment"
string | null$display_nameFilename to use in email (if different from source file)

Definition at line 192 of file class.ilMimeMail.php.

197 : void {
198 if ($file_type === '') {
199 $file_type = 'application/octet-stream';
200 }
201
202 $this->aattach[] = $filename;
203 $this->actype[] = $file_type;
204 $this->adispo[] = $disposition;
205 $this->adisplay[] = $display_name;
206 }
$filename
Definition: buildRTE.php:78

◆ Bcc()

ilMimeMail::Bcc (   $bcc)
Parameters
string | string[]$bccBCC email address, accept both a single address or an array of addresses

Definition at line 128 of file class.ilMimeMail.php.

128 : void
129 {
130 if (is_array($bcc)) {
131 $this->abcc = $bcc;
132 } else {
133 $this->abcc[] = $bcc;
134 }
135 }

◆ Body()

ilMimeMail::Body ( string  $body,
?Closure  $to_html_transformation = null 
)
Parameters
Closure(string)string|null $to_html_transformation

Definition at line 164 of file class.ilMimeMail.php.

164 : void
165 {
166 $this->body = $body;
167 $this->to_html_transformation = $to_html_transformation;
168 }
Closure $to_html_transformation

References $body, and $to_html_transformation.

◆ build()

ilMimeMail::build ( )
protected

Definition at line 241 of file class.ilMimeMail.php.

241 : void
242 {
243 global $DIC;
244
245 $this->final_body_alt = '';
246 $this->final_body = '';
247 $this->images = [];
248
249 if ($DIC->settings()->get('mail_send_html', '0')) {
250 $skin = $DIC['ilClientIniFile']->readVariable('layout', 'skin');
251 $style = $DIC['ilClientIniFile']->readVariable('layout', 'style');
252
253 $data_factory = new Factory();
254 $factory = $DIC->ui()->factory();
255 $renderer = $DIC->ui()->renderer();
256
257 $this->prepareHTMLBody();
258
259 $page = $factory->layout()->page()->mail(
260 $this->getStyleSheetPath($skin, $style),
261 "cid:{$this->getLogoCid($skin, $style)}",
263 $factory->legacy()->content($this->body),
264 $data_factory->link(ilUtil::_getHttpPath(), $data_factory->uri(ilUtil::_getHttpPath())),
265 );
266
267 $this->final_body = $renderer->render($page);
268 $this->final_body_alt = $this->removeHtmlTags($this->body);
269 } else {
270 $this->final_body = $this->removeHtmlTags($this->body);
271 }
272 }
$renderer
Builds data types.
Definition: Factory.php:36
removeHtmlTags(string $maybe_html)
getStyleSheetPath(string $skin, string $style)
static _getHttpPath()

References $DIC, $renderer, ilObjSystemFolder\_getHeaderTitle(), and ilUtil\_getHttpPath().

+ Here is the call graph for this function:

◆ Cc()

ilMimeMail::Cc (   $cc)
Parameters
string | string[]$ccCC email address, accept both a single address or an array of addresses

Definition at line 116 of file class.ilMimeMail.php.

116 : void
117 {
118 if (is_array($cc)) {
119 $this->acc = $cc;
120 } else {
121 $this->acc[] = $cc;
122 }
123 }

◆ containsHtmlBlockElementsOrLineBreaks()

ilMimeMail::containsHtmlBlockElementsOrLineBreaks ( string  $email_body)
private

Definition at line 316 of file class.ilMimeMail.php.

316 : bool
317 {
318 if (str_contains($email_body, '<') === false || str_contains($email_body, '>') === false) {
319 return false;
320 }
321
322 // Detect common HTML tags produced by Markdown rendering.
323 $pattern = '~</?(p|br|div|ul|ol|li|code|pre|h[1-6])\b~i';
324 if (preg_match($pattern, $email_body) === 1) {
325 return true;
326 }
327
328 return strip_tags($email_body, '<b><u><i><a>') !== $email_body;
329 }

◆ From()

ilMimeMail::From ( ilMailMimeSender  $sender)

Definition at line 96 of file class.ilMimeMail.php.

96 : void
97 {
98 $this->sender = $sender;
99 }
ilMailMimeSender $sender

References $sender.

◆ gatherImagesFromDirectory()

ilMimeMail::gatherImagesFromDirectory ( string  $directory,
bool  $clear_previous = false 
)
protected

Definition at line 400 of file class.ilMimeMail.php.

400 : void
401 {
402 if ($clear_previous) {
403 $this->images = [];
404 }
405
406 foreach (new RegexIterator(
407 new DirectoryIterator($directory),
408 '/\.(jpg|jpeg|gif|svg|png)$/i'
409 ) as $file) {
410 $this->addImage($file);
411 }
412 }
addImage(SplFileInfo $file, bool $as_logo=false)

◆ getAttachments()

ilMimeMail::getAttachments ( )
Returns
array{path: string, name: string}[]

Definition at line 211 of file class.ilMimeMail.php.

211 : array
212 {
213 $attachments = [];
214
215 $i = 0;
216 foreach ($this->aattach as $attachment) {
217 $name = '';
218 if (isset($this->adisplay[$i]) && is_string($this->adisplay[$i]) && $this->adisplay[$i] !== '') {
219 $name = $this->adisplay[$i];
220 }
221
222 $attachments[] = [
223 'path' => $attachment,
224 'name' => $name
225 ];
226 ++$i;
227 }
228
229 return $attachments;
230 }

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ getBcc()

ilMimeMail::getBcc ( )
Returns
string[]

Definition at line 156 of file class.ilMimeMail.php.

156 : array
157 {
158 return $this->abcc;
159 }

References $abcc.

Referenced by ilMailMimeTransportBase\send(), and ilMailTest\testExternalMailDeliveryWorksAsExpected().

+ Here is the caller graph for this function:

◆ getCc()

ilMimeMail::getCc ( )
Returns
string[]

Definition at line 148 of file class.ilMimeMail.php.

148 : array
149 {
150 return $this->acc;
151 }

References $acc.

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ getDefaultTransport()

static ilMimeMail::getDefaultTransport ( )
static

Definition at line 81 of file class.ilMimeMail.php.

82 {
84 }
static ilMailMimeTransport $default_transport

References $default_transport.

Referenced by ilMailTest\testExternalMailDeliveryWorksAsExpected().

+ Here is the caller graph for this function:

◆ getFinalBody()

ilMimeMail::getFinalBody ( )

Definition at line 170 of file class.ilMimeMail.php.

170 : string
171 {
172 return $this->final_body;
173 }
string $final_body

References $final_body.

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ getFinalBodyalt()

ilMimeMail::getFinalBodyalt ( )

Definition at line 175 of file class.ilMimeMail.php.

175 : string
176 {
178 }
string $final_body_alt

References $final_body_alt.

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ getFrom()

ilMimeMail::getFrom ( )

Definition at line 180 of file class.ilMimeMail.php.

References $sender.

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ getImages()

ilMimeMail::getImages ( )
Returns
array{path: string, cid: string, name: string}[] An array of images. Each element must container to associative keys, 'path', 'cid' and 'name'

Definition at line 236 of file class.ilMimeMail.php.

236 : array
237 {
238 return array_values($this->images);
239 }

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ getLogoCid()

ilMimeMail::getLogoCid ( string  $skin,
string  $style 
)
private

Definition at line 350 of file class.ilMimeMail.php.

350 : string
351 {
352 if ($skin !== 'default') {
353 $locations = [
354 $skin,
355 "$skin/$style"
356 ];
357
358 foreach ($locations as $location) {
359 $custom_directory = $this->getPathToRootDirectory() . sprintf(self::SKIN_LOGO_PATH, $location);
360 if (is_dir($custom_directory) && is_readable($custom_directory)) {
361 $this->gatherImagesFromDirectory($custom_directory);
362 }
363 }
364 } else {
366 if (is_file($path) && is_readable($path)) {
367 return $this->addImage(new SplFileInfo($path), true);
368 }
369 }
370
371 foreach ($this->images as $image) {
372 if ($image['as_logo']) {
373 return $image['cid'];
374 }
375 }
376
377 $logo_cid = count($this->images) > 1 ? current($this->images)['cid'] : null;
378
379 foreach ($this->images as $cid => $image) {
380 $file_name = basename($image['path'], '.' . pathinfo($image['path'], PATHINFO_EXTENSION));
381 if (in_array(strtolower($file_name), ['logo', 'headericon'], true)) {
382 $logo_cid = $cid;
383 break;
384 }
385 }
386
387 if (is_string($logo_cid)) {
388 $this->images[$logo_cid]['as_logo'] = true;
389 return $logo_cid;
390 }
391
393 if (is_file($path) && is_readable($path)) {
394 return $this->addImage(new SplFileInfo($path), true);
395 }
396
397 return '';
398 }
$location
Definition: buildRTE.php:22
gatherImagesFromDirectory(string $directory, bool $clear_previous=false)
const string MAIL_LOGO_PATH
$path
Definition: ltiservices.php:30

References $location, and $path.

◆ getPathToRootDirectory()

ilMimeMail::getPathToRootDirectory ( )
private

Definition at line 281 of file class.ilMimeMail.php.

281 : string
282 {
283 $current_dir = realpath(__DIR__);
284
285 while ($current_dir !== '.') {
286 if (file_exists($current_dir . self::ROOT_DIR_IDENTIFICATION_FILE)) {
287 break;
288 }
289
290 $current_dir = dirname($current_dir);
291 }
292
293 return $current_dir;
294 }

◆ getStyleSheetPath()

ilMimeMail::getStyleSheetPath ( string  $skin,
string  $style 
)
private

Definition at line 331 of file class.ilMimeMail.php.

331 : string
332 {
333 if ($skin !== 'default') {
334 $locations = [
335 $skin,
336 "$skin/$style"
337 ];
338
339 foreach ($locations as $location) {
340 $custom_path = $this->getPathToRootDirectory() . sprintf(self::SKIN_CSS_PATH, $location);
341 if (is_file($custom_path)) {
342 return $custom_path;
343 }
344 }
345 }
346
347 return self::MAIL_CSS_PATH;
348 }
const string MAIL_CSS_PATH

References $location.

◆ getSubject()

ilMimeMail::getSubject ( )

Definition at line 91 of file class.ilMimeMail.php.

91 : string
92 {
93 return $this->subject;
94 }

References $subject.

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ getTo()

ilMimeMail::getTo ( )
Returns
string[]

Definition at line 140 of file class.ilMimeMail.php.

140 : array
141 {
142 return $this->sendto;
143 }

References $sendto.

Referenced by ilMailMimeTransportBase\send().

+ Here is the caller graph for this function:

◆ prepareHTMLBody()

ilMimeMail::prepareHTMLBody ( )
private

Definition at line 296 of file class.ilMimeMail.php.

296 : void
297 {
298 if ($this->body === '') {
299 $this->body = ' ';
300 }
301
302 $transformed_body = $this->to_html_transformation ? ($this->to_html_transformation)($this->body) : $this->body;
303
304 $contains_html = $this->containsHtmlBlockElementsOrLineBreaks($transformed_body);
305 if ($contains_html) {
306 $this->final_body_alt = strip_tags(str_ireplace(['<br />', '<br>', '<br/>'], "\n", $this->body));
307 $this->body = $transformed_body;
308 } else {
309 $this->final_body_alt = strip_tags($this->body);
310 $this->body = nl2br($transformed_body);
311 }
312
313 $this->body = $this->refinery->string()->makeClickable()->transform($this->body);
314 }
containsHtmlBlockElementsOrLineBreaks(string $email_body)

References ILIAS\Repository\refinery().

+ Here is the call graph for this function:

◆ removeHtmlTags()

ilMimeMail::removeHtmlTags ( string  $maybe_html)
private

Definition at line 274 of file class.ilMimeMail.php.

274 : string
275 {
276 $maybe_html = str_ireplace(['<br />', '<br>', '<br/>'], "\n", $maybe_html);
277
278 return html_entity_decode(strip_tags($maybe_html), ENT_QUOTES);
279 }

◆ Send()

ilMimeMail::Send ( ?ilMailMimeTransport  $transport = null)

Definition at line 428 of file class.ilMimeMail.php.

428 : bool
429 {
430 if (!($transport instanceof ilMailMimeTransport)) {
431 $transport = self::getDefaultTransport();
432 }
433
434 $this->build();
435
436 return $transport->send($this);
437 }
static getDefaultTransport()

◆ setDefaultTransport()

static ilMimeMail::setDefaultTransport ( ?ilMailMimeTransport  $transport)
static

Definition at line 76 of file class.ilMimeMail.php.

76 : void
77 {
78 self::$default_transport = $transport;
79 }

Referenced by __construct(), ilMailMimeTest\setUp(), and ilMailTest\testExternalMailDeliveryWorksAsExpected().

+ Here is the caller graph for this function:

◆ Subject()

ilMimeMail::Subject ( string  $subject,
bool  $add_prefix = false,
string  $context_prefix = '' 
)

Definition at line 86 of file class.ilMimeMail.php.

86 : void
87 {
88 $this->subject = $this->subject_builder->subject($subject, $add_prefix, $context_prefix);
89 }

References $subject.

◆ To()

ilMimeMail::To (   $to)
Parameters
string | string[]$toTo email address, accept both a single address or an array of addresses

Definition at line 104 of file class.ilMimeMail.php.

104 : void
105 {
106 if (is_array($to)) {
107 $this->sendto = $to;
108 } else {
109 $this->sendto[] = $to;
110 }
111 }

Field Documentation

◆ $aattach

array ilMimeMail::$aattach = []
protected

Definition at line 51 of file class.ilMimeMail.php.

◆ $abcc

array ilMimeMail::$abcc = []
protected

Definition at line 47 of file class.ilMimeMail.php.

Referenced by getBcc().

◆ $acc

array ilMimeMail::$acc = []
protected

Definition at line 45 of file class.ilMimeMail.php.

Referenced by getCc().

◆ $actype

array ilMimeMail::$actype = []
protected

Definition at line 53 of file class.ilMimeMail.php.

◆ $adisplay

array ilMimeMail::$adisplay = []
protected

Definition at line 57 of file class.ilMimeMail.php.

◆ $adispo

array ilMimeMail::$adispo = []
protected

Definition at line 55 of file class.ilMimeMail.php.

◆ $body

string ilMimeMail::$body = ''
protected

Definition at line 39 of file class.ilMimeMail.php.

Referenced by Body().

◆ $default_transport

ilMailMimeTransport ilMimeMail::$default_transport = null
staticprotected

Definition at line 33 of file class.ilMimeMail.php.

Referenced by getDefaultTransport().

◆ $final_body

string ilMimeMail::$final_body = ''
protected

Definition at line 40 of file class.ilMimeMail.php.

Referenced by getFinalBody().

◆ $final_body_alt

string ilMimeMail::$final_body_alt = ''
protected

Definition at line 41 of file class.ilMimeMail.php.

Referenced by getFinalBodyalt().

◆ $images

array ilMimeMail::$images = []
protected

Definition at line 49 of file class.ilMimeMail.php.

◆ $refinery

readonly Refinery ilMimeMail::$refinery
private

Definition at line 58 of file class.ilMimeMail.php.

◆ $sender

ilMailMimeSender ilMimeMail::$sender
protected

Definition at line 35 of file class.ilMimeMail.php.

Referenced by From(), and getFrom().

◆ $sendto

array ilMimeMail::$sendto = []
protected

Definition at line 43 of file class.ilMimeMail.php.

Referenced by getTo().

◆ $settings

ilSetting ilMimeMail::$settings
protected

Definition at line 37 of file class.ilMimeMail.php.

◆ $subject

string ilMimeMail::$subject = ''
protected

Definition at line 38 of file class.ilMimeMail.php.

Referenced by getSubject(), and Subject().

◆ $subject_builder

ilMailMimeSubjectBuilder ilMimeMail::$subject_builder
protected

Definition at line 36 of file class.ilMimeMail.php.

◆ $to_html_transformation

Closure ilMimeMail::$to_html_transformation = null
private

Definition at line 60 of file class.ilMimeMail.php.

Referenced by Body().

◆ MAIL_CSS_PATH

const string ilMimeMail::MAIL_CSS_PATH = 'assets/css/mail.css'
private

Definition at line 29 of file class.ilMimeMail.php.

◆ MAIL_LOGO_PATH

const string ilMimeMail::MAIL_LOGO_PATH = '/public/assets/images/logo/HeaderIcon.svg'
private

Definition at line 30 of file class.ilMimeMail.php.

◆ MAIL_SUBJECT_PREFIX

final const string ilMimeMail::MAIL_SUBJECT_PREFIX = '[ILIAS]'

Definition at line 26 of file class.ilMimeMail.php.

Referenced by ilObjMailGUI\populateExternalSettingsForm().

◆ ROOT_DIR_IDENTIFICATION_FILE

const string ilMimeMail::ROOT_DIR_IDENTIFICATION_FILE = '/ilias_version.php'
private

Definition at line 31 of file class.ilMimeMail.php.

◆ SKIN_CSS_PATH

const string ilMimeMail::SKIN_CSS_PATH = '/public/Customizing/skin/%s/mail.css'
private

Definition at line 28 of file class.ilMimeMail.php.

◆ SKIN_LOGO_PATH

const string ilMimeMail::SKIN_LOGO_PATH = '/public/Customizing/skin/%s/images/logo'
private

Definition at line 27 of file class.ilMimeMail.php.


The documentation for this class was generated from the following file: