ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMimeMail.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  final public const MAIL_SUBJECT_PREFIX = '[ILIAS]';
26 
27  protected static ?ilMailMimeTransport $defaultTransport = null;
28 
31  protected ilSetting $settings;
32  protected string $subject = '';
33  protected string $body = '';
34  protected string $finalBody = '';
35  protected string $finalBodyAlt = '';
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->subjectBuilder = 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::$defaultTransport = $transport;
71  }
72 
73  public static function getDefaultTransport(): ?ilMailMimeTransport
74  {
75  return self::$defaultTransport;
76  }
77 
78  public function Subject(string $subject, bool $addPrefix = false, string $contextPrefix = ''): void
79  {
80  $this->subject = $this->subjectBuilder->subject($subject, $addPrefix, $contextPrefix);
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->finalBody;
161  }
162 
163  public function getFinalBodyAlt(): string
164  {
165  return $this->finalBodyAlt;
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->finalBodyAlt = '';
234  $this->finalBody = '';
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->finalBody = $this->removeHTMLTags($this->body);
245  }
246  }
247 
248  private function removeHTMLTags(string $maybeHTML): string
249  {
250  $maybeHTML = str_ireplace(['<br />', '<br>', '<br/>'], "\n", $maybeHTML);
251 
252  return strip_tags($maybeHTML);
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->finalBodyAlt = 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->finalBodyAlt = strip_tags(str_ireplace(["<br />", "<br>", "<br/>"], "\n", $this->body));
270  }
271 
272  $this->finalBody = str_replace('{PLACEHOLDER}', $this->body, $this->getHtmlEnvelope($skin, $style));
273  }
274 
275  protected function getHtmlEnvelope(string $skin, string $style): string
276  {
277  $bracket_path = './Services/Mail/templates/default/tpl.html_mail_template.html';
278 
279  if ($skin !== 'default') {
280  $locations = [
281  $skin,
282  $skin . '/' . $style
283  ];
284 
285  foreach ($locations as $location) {
286  $tplpath = './Customizing/global/skin/' . $location . '/Services/Mail/tpl.html_mail_template.html';
287 
288  if (is_file($tplpath)) {
289  $bracket_path = $tplpath;
290  break;
291  }
292  }
293  }
294 
295  return file_get_contents($bracket_path);
296  }
297 
298  protected function buildHtmlInlineImages(string $skin, string $style): void
299  {
300  $this->gatherImagesFromDirectory('./Services/Mail/templates/default/img');
301 
302  if ($skin !== 'default') {
303  $locations = [
304  $skin,
305  $skin . '/' . $style
306  ];
307 
308  foreach ($locations as $location) {
309  $skin_directory = './Customizing/global/skin/' . $location . '/Services/Mail/img';
310  if (is_dir($skin_directory) && is_readable($skin_directory)) {
311  $this->gatherImagesFromDirectory($skin_directory, true);
312  break;
313  }
314  }
315  }
316  }
317 
318  protected function gatherImagesFromDirectory(string $directory, bool $clearPrevious = false): void
319  {
320  if ($clearPrevious) {
321  $this->images = [];
322  }
323 
324  foreach (new RegexIterator(
325  new DirectoryIterator($directory),
326  '/\.(jpg|jpeg|gif|svg|png)$/i'
327  ) as $file) {
329  $cid = 'img/' . $file->getFilename();
330 
331  $this->images[$cid] = [
332  'path' => $file->getPathname(),
333  'cid' => $cid,
334  'name' => $file->getFilename()
335  ];
336  }
337  }
338 
339  public function Send(ilMailMimeTransport $transport = null): bool
340  {
341  if (!($transport instanceof ilMailMimeTransport)) {
342  $transport = self::getDefaultTransport();
343  }
344 
345  $this->build();
346 
347  return $transport->send($this);
348  }
349 }
buildBodyMultiParts(string $skin, string $style)
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
removeHTMLTags(string $maybeHTML)
ilSetting $settings
Class ilMailMimeSubjectBuilder.
Send(ilMailMimeTransport $transport=null)
Subject(string $subject, bool $addPrefix=false, string $contextPrefix='')
static setDefaultTransport(?ilMailMimeTransport $transport)
global $DIC
Definition: feed.php:28
static getDefaultTransport()
getHtmlEnvelope(string $skin, string $style)
buildHtmlInlineImages(string $skin, string $style)
Interface ilMailMimeTransport.
readonly Refinery $refinery
Interface ilMailMimeTransport.
ilMailMimeSubjectBuilder $subjectBuilder
ilMailMimeSender $sender
$filename
Definition: buildRTE.php:78
static ilMailMimeTransport $defaultTransport
From(ilMailMimeSender $sender)
string $finalBodyAlt
final const MAIL_SUBJECT_PREFIX
Attach(string $filename, string $file_type='', string $disposition='inline', ?string $display_name=null)
Body(string $body)