ILIAS  trunk Revision v11.0_alpha-1843-g9e1fad99175
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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 
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  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 $clearPrevious = false): void
326  {
327  if ($clearPrevious) {
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 }
buildBodyMultiParts(string $skin, string $style)
$location
Definition: buildRTE.php:22
Send(?ilMailMimeTransport $transport=null)
removeHTMLTags(string $maybeHTML)
ilSetting $settings
Class ilMailMimeSubjectBuilder.
Subject(string $subject, bool $addPrefix=false, string $contextPrefix='')
static setDefaultTransport(?ilMailMimeTransport $transport)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getDefaultTransport()
getHtmlEnvelope(string $skin, string $style)
buildHtmlInlineImages(string $skin, string $style)
Interface ilMailMimeTransport.
readonly Refinery $refinery
Interface ilMailMimeTransport.
ilMailMimeSubjectBuilder $subjectBuilder
global $DIC
Definition: shib_login.php:22
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)