ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
HtmlMailBodyComposer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data\Factory as DataFactory;
25use ILIAS\Refinery\Factory as Refinery;
26use ILIAS\UI\Factory as UIFactory;
28
36{
37 private const string BLOCK_ELEMENT_PATTERN = '~</?(p|br|div|ul|ol|li|code|pre|h[1-6])\b~i';
38 private const string INLINE_TAGS = '<b><u><i><a>';
39
40 public function __construct(
41 private readonly MailAssets $assets,
42 private readonly PlainTextMailBodyComposer $plain_text,
43 private readonly UIFactory $ui,
44 private readonly Renderer $renderer,
45 private readonly Refinery $refinery,
46 private readonly DataFactory $data,
47 private readonly string $installation_title,
48 private readonly string $installation_url
49 ) {
50 }
51
52 public function compose(MailBodySource $source): ComposedMailBody
53 {
54 $html = $source->transformedToHtml();
55 $html = $this->containsHtml($html) ? $html : nl2br($html);
56 $html = $this->refinery->string()->makeClickable()->transform($html);
57
58 $images = $this->assets->inlineImages();
59 $logo = $images->logo();
60 $page = $this->ui->layout()->page()->mail(
61 $this->assets->styleSheet()->path(),
62 'cid:' . ($logo?->cid() ?? ''),
63 $this->installation_title,
64 $this->ui->legacy()->content($html),
65 $this->data->link($this->installation_url, $this->data->uri($this->installation_url))
66 );
67 $plain_text = $this->plain_text->compose(new MailBodySource($html));
68
69 return new ComposedMailBody(
70 $this->renderer->render($page),
71 $plain_text->body(),
72 $images
73 );
74 }
75
76 private function containsHtml(string $email_body): bool
77 {
78 if (!str_contains($email_body, '<') || !str_contains($email_body, '>')) {
79 return false;
80 }
81
82 if (preg_match(self::BLOCK_ELEMENT_PATTERN, $email_body) === 1) {
83 return true;
84 }
85
86 return strip_tags($email_body, self::INLINE_TAGS) !== $email_body;
87 }
88}
renderer()
$renderer
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Composes the HTML representation of a mail body, and the plain text alternative that goes with it.
__construct(private readonly MailAssets $assets, private readonly PlainTextMailBodyComposer $plain_text, private readonly UIFactory $ui, private readonly Renderer $renderer, private readonly Refinery $refinery, private readonly DataFactory $data, private readonly string $installation_title, private readonly string $installation_url)
Turns a mail body into plain text, for installations with HTML mails disabled and for the alternative...
An entity that renders components to a string output.
Definition: Renderer.php:31
$data
Definition: storeScorm.php:45