@@ -50,33 +50,33 @@ discard block |
||
50 | 50 | * @package OC\Mail |
51 | 51 | */ |
52 | 52 | class EMailTemplate implements IEMailTemplate { |
53 | - /** @var Defaults */ |
|
54 | - protected $themingDefaults; |
|
55 | - /** @var IURLGenerator */ |
|
56 | - protected $urlGenerator; |
|
57 | - /** @var IFactory */ |
|
58 | - protected $l10nFactory; |
|
59 | - /** @var string */ |
|
60 | - protected $emailId; |
|
61 | - /** @var array */ |
|
62 | - protected $data; |
|
63 | - |
|
64 | - /** @var string */ |
|
65 | - protected $subject = ''; |
|
66 | - /** @var string */ |
|
67 | - protected $htmlBody = ''; |
|
68 | - /** @var string */ |
|
69 | - protected $plainBody = ''; |
|
70 | - /** @var bool indicated if the footer is added */ |
|
71 | - protected $headerAdded = false; |
|
72 | - /** @var bool indicated if the body is already opened */ |
|
73 | - protected $bodyOpened = false; |
|
74 | - /** @var bool indicated if there is a list open in the body */ |
|
75 | - protected $bodyListOpened = false; |
|
76 | - /** @var bool indicated if the footer is added */ |
|
77 | - protected $footerAdded = false; |
|
78 | - |
|
79 | - protected $head = <<<EOF |
|
53 | + /** @var Defaults */ |
|
54 | + protected $themingDefaults; |
|
55 | + /** @var IURLGenerator */ |
|
56 | + protected $urlGenerator; |
|
57 | + /** @var IFactory */ |
|
58 | + protected $l10nFactory; |
|
59 | + /** @var string */ |
|
60 | + protected $emailId; |
|
61 | + /** @var array */ |
|
62 | + protected $data; |
|
63 | + |
|
64 | + /** @var string */ |
|
65 | + protected $subject = ''; |
|
66 | + /** @var string */ |
|
67 | + protected $htmlBody = ''; |
|
68 | + /** @var string */ |
|
69 | + protected $plainBody = ''; |
|
70 | + /** @var bool indicated if the footer is added */ |
|
71 | + protected $headerAdded = false; |
|
72 | + /** @var bool indicated if the body is already opened */ |
|
73 | + protected $bodyOpened = false; |
|
74 | + /** @var bool indicated if there is a list open in the body */ |
|
75 | + protected $bodyListOpened = false; |
|
76 | + /** @var bool indicated if the footer is added */ |
|
77 | + protected $footerAdded = false; |
|
78 | + |
|
79 | + protected $head = <<<EOF |
|
80 | 80 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
81 | 81 | <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" style="-webkit-font-smoothing:antialiased;background:#fff!important"> |
82 | 82 | <head> |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | <center data-parsed="" style="min-width:580px;width:100%"> |
95 | 95 | EOF; |
96 | 96 | |
97 | - protected $tail = <<<EOF |
|
97 | + protected $tail = <<<EOF |
|
98 | 98 | </center> |
99 | 99 | </td> |
100 | 100 | </tr> |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | |
107 | 107 | EOF; |
108 | 108 | |
109 | - protected $header = <<<EOF |
|
109 | + protected $header = <<<EOF |
|
110 | 110 | <table align="center" class="wrapper header float-center" style="Margin:0 auto;background:#fff;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%%"> |
111 | 111 | <tr style="padding:0;text-align:left;vertical-align:top"> |
112 | 112 | <td class="wrapper-inner" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:20px;text-align:left;vertical-align:top;word-wrap:break-word"> |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | </table> |
140 | 140 | EOF; |
141 | 141 | |
142 | - protected $heading = <<<EOF |
|
142 | + protected $heading = <<<EOF |
|
143 | 143 | <table align="center" class="container main-heading float-center" style="Margin:0 auto;background:0 0!important;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:580px"> |
144 | 144 | <tbody> |
145 | 145 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | </table> |
159 | 159 | EOF; |
160 | 160 | |
161 | - protected $bodyBegin = <<<EOF |
|
161 | + protected $bodyBegin = <<<EOF |
|
162 | 162 | <table align="center" class="wrapper content float-center" style="Margin:0 auto;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%"> |
163 | 163 | <tr style="padding:0;text-align:left;vertical-align:top"> |
164 | 164 | <td class="wrapper-inner" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:0;text-align:left;vertical-align:top;word-wrap:break-word"> |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | <td style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:0;text-align:left;vertical-align:top;word-wrap:break-word"> |
169 | 169 | EOF; |
170 | 170 | |
171 | - protected $bodyText = <<<EOF |
|
171 | + protected $bodyText = <<<EOF |
|
172 | 172 | <table class="row description" style="border-collapse:collapse;border-spacing:0;display:table;padding:0;position:relative;text-align:left;vertical-align:top;width:100%%"> |
173 | 173 | <tbody> |
174 | 174 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -187,8 +187,8 @@ discard block |
||
187 | 187 | </table> |
188 | 188 | EOF; |
189 | 189 | |
190 | - // note: listBegin (like bodyBegin) is not processed through sprintf, so "%" is not escaped as "%%". (bug #12151) |
|
191 | - protected $listBegin = <<<EOF |
|
190 | + // note: listBegin (like bodyBegin) is not processed through sprintf, so "%" is not escaped as "%%". (bug #12151) |
|
191 | + protected $listBegin = <<<EOF |
|
192 | 192 | <table class="row description" style="border-collapse:collapse;border-spacing:0;display:table;padding:0;position:relative;text-align:left;vertical-align:top;width:100%"> |
193 | 193 | <tbody> |
194 | 194 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | <table style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%"> |
197 | 197 | EOF; |
198 | 198 | |
199 | - protected $listItem = <<<EOF |
|
199 | + protected $listItem = <<<EOF |
|
200 | 200 | <tr style="padding:0;text-align:left;vertical-align:top"> |
201 | 201 | <td style="Margin:0;color:#0a0a0a;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;line-height:1.3;margin:0;padding:0;text-align:left;width:15px;"> |
202 | 202 | <p class="text-left" style="Margin:0;Margin-bottom:10px;color:#777;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',Arial,sans-serif;font-size:16px;font-weight:400;line-height:1.3;margin:0;margin-bottom:10px;padding:0;padding-left:10px;text-align:left">%s</p> |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | </tr> |
209 | 209 | EOF; |
210 | 210 | |
211 | - protected $listEnd = <<<EOF |
|
211 | + protected $listEnd = <<<EOF |
|
212 | 212 | </table> |
213 | 213 | </th> |
214 | 214 | </tr> |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | </table> |
217 | 217 | EOF; |
218 | 218 | |
219 | - protected $buttonGroup = <<<EOF |
|
219 | + protected $buttonGroup = <<<EOF |
|
220 | 220 | <table class="spacer" style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%%"> |
221 | 221 | <tbody> |
222 | 222 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | </table> |
270 | 270 | EOF; |
271 | 271 | |
272 | - protected $button = <<<EOF |
|
272 | + protected $button = <<<EOF |
|
273 | 273 | <table class="spacer" style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%%"> |
274 | 274 | <tbody> |
275 | 275 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -309,7 +309,7 @@ discard block |
||
309 | 309 | </table> |
310 | 310 | EOF; |
311 | 311 | |
312 | - protected $bodyEnd = <<<EOF |
|
312 | + protected $bodyEnd = <<<EOF |
|
313 | 313 | |
314 | 314 | </td> |
315 | 315 | </tr> |
@@ -320,7 +320,7 @@ discard block |
||
320 | 320 | </table> |
321 | 321 | EOF; |
322 | 322 | |
323 | - protected $footer = <<<EOF |
|
323 | + protected $footer = <<<EOF |
|
324 | 324 | <table class="spacer float-center" style="Margin:0 auto;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%%"> |
325 | 325 | <tbody> |
326 | 326 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -346,307 +346,307 @@ discard block |
||
346 | 346 | </table> |
347 | 347 | EOF; |
348 | 348 | |
349 | - public function __construct(Defaults $themingDefaults, |
|
350 | - IURLGenerator $urlGenerator, |
|
351 | - IFactory $l10nFactory, |
|
352 | - $emailId, |
|
353 | - array $data) { |
|
354 | - $this->themingDefaults = $themingDefaults; |
|
355 | - $this->urlGenerator = $urlGenerator; |
|
356 | - $this->l10nFactory = $l10nFactory; |
|
357 | - $this->htmlBody .= $this->head; |
|
358 | - $this->emailId = $emailId; |
|
359 | - $this->data = $data; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * Sets the subject of the email |
|
364 | - * |
|
365 | - * @param string $subject |
|
366 | - */ |
|
367 | - public function setSubject(string $subject) { |
|
368 | - $this->subject = $subject; |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Adds a header to the email |
|
373 | - */ |
|
374 | - public function addHeader() { |
|
375 | - if ($this->headerAdded) { |
|
376 | - return; |
|
377 | - } |
|
378 | - $this->headerAdded = true; |
|
379 | - |
|
380 | - $logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo(false)); |
|
381 | - $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getColorPrimary(), $logoUrl, $this->themingDefaults->getName()]); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Adds a heading to the email |
|
386 | - * |
|
387 | - * @param string $title |
|
388 | - * @param string|bool $plainTitle Title that is used in the plain text email |
|
389 | - * if empty the $title is used, if false none will be used |
|
390 | - */ |
|
391 | - public function addHeading(string $title, $plainTitle = '') { |
|
392 | - if ($this->footerAdded) { |
|
393 | - return; |
|
394 | - } |
|
395 | - if ($plainTitle === '') { |
|
396 | - $plainTitle = $title; |
|
397 | - } |
|
398 | - |
|
399 | - $this->htmlBody .= vsprintf($this->heading, [htmlspecialchars($title)]); |
|
400 | - if ($plainTitle !== false) { |
|
401 | - $this->plainBody .= $plainTitle . PHP_EOL . PHP_EOL; |
|
402 | - } |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Open the HTML body when it is not already |
|
407 | - */ |
|
408 | - protected function ensureBodyIsOpened() { |
|
409 | - if ($this->bodyOpened) { |
|
410 | - return; |
|
411 | - } |
|
412 | - |
|
413 | - $this->htmlBody .= $this->bodyBegin; |
|
414 | - $this->bodyOpened = true; |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Adds a paragraph to the body of the email |
|
419 | - * |
|
420 | - * @param string $text Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email |
|
421 | - * @param string|bool $plainText Text that is used in the plain text email |
|
422 | - * if empty the $text is used, if false none will be used |
|
423 | - */ |
|
424 | - public function addBodyText(string $text, $plainText = '') { |
|
425 | - if ($this->footerAdded) { |
|
426 | - return; |
|
427 | - } |
|
428 | - if ($plainText === '') { |
|
429 | - $plainText = $text; |
|
430 | - $text = htmlspecialchars($text); |
|
431 | - } |
|
432 | - |
|
433 | - $this->ensureBodyListClosed(); |
|
434 | - $this->ensureBodyIsOpened(); |
|
435 | - |
|
436 | - $this->htmlBody .= vsprintf($this->bodyText, [$text]); |
|
437 | - if ($plainText !== false) { |
|
438 | - $this->plainBody .= $plainText . PHP_EOL . PHP_EOL; |
|
439 | - } |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Adds a list item to the body of the email |
|
444 | - * |
|
445 | - * @param string $text Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email |
|
446 | - * @param string $metaInfo Note: When $plainMetaInfo falls back to this, HTML is automatically escaped in the HTML email |
|
447 | - * @param string $icon Absolute path, must be 16*16 pixels |
|
448 | - * @param string|bool $plainText Text that is used in the plain text email |
|
449 | - * if empty the $text is used, if false none will be used |
|
450 | - * @param string|bool $plainMetaInfo Meta info that is used in the plain text email |
|
451 | - * if empty the $metaInfo is used, if false none will be used |
|
452 | - * @since 12.0.0 |
|
453 | - */ |
|
454 | - public function addBodyListItem(string $text, string $metaInfo = '', string $icon = '', $plainText = '', $plainMetaInfo = '') { |
|
455 | - $this->ensureBodyListOpened(); |
|
456 | - |
|
457 | - if ($plainText === '') { |
|
458 | - $plainText = $text; |
|
459 | - $text = htmlspecialchars($text); |
|
460 | - } |
|
461 | - if ($plainMetaInfo === '') { |
|
462 | - $plainMetaInfo = $metaInfo; |
|
463 | - $metaInfo = htmlspecialchars($metaInfo); |
|
464 | - } |
|
465 | - |
|
466 | - $htmlText = $text; |
|
467 | - if ($metaInfo) { |
|
468 | - $htmlText = '<em style="color:#777;">' . $metaInfo . '</em><br>' . $htmlText; |
|
469 | - } |
|
470 | - if ($icon !== '') { |
|
471 | - $icon = '<img src="' . htmlspecialchars($icon) . '" alt="•">'; |
|
472 | - } else { |
|
473 | - $icon = '•'; |
|
474 | - } |
|
475 | - $this->htmlBody .= vsprintf($this->listItem, [$icon, $htmlText]); |
|
476 | - if ($plainText !== false) { |
|
477 | - $this->plainBody .= ' * ' . $plainText; |
|
478 | - if ($plainMetaInfo !== false) { |
|
479 | - $this->plainBody .= ' (' . $plainMetaInfo . ')'; |
|
480 | - } |
|
481 | - $this->plainBody .= PHP_EOL; |
|
482 | - } |
|
483 | - } |
|
484 | - |
|
485 | - protected function ensureBodyListOpened() { |
|
486 | - if ($this->bodyListOpened) { |
|
487 | - return; |
|
488 | - } |
|
489 | - |
|
490 | - $this->ensureBodyIsOpened(); |
|
491 | - $this->bodyListOpened = true; |
|
492 | - $this->htmlBody .= $this->listBegin; |
|
493 | - } |
|
494 | - |
|
495 | - protected function ensureBodyListClosed() { |
|
496 | - if (!$this->bodyListOpened) { |
|
497 | - return; |
|
498 | - } |
|
499 | - |
|
500 | - $this->bodyListOpened = false; |
|
501 | - $this->htmlBody .= $this->listEnd; |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Adds a button group of two buttons to the body of the email |
|
506 | - * |
|
507 | - * @param string $textLeft Text of left button; Note: When $plainTextLeft falls back to this, HTML is automatically escaped in the HTML email |
|
508 | - * @param string $urlLeft URL of left button |
|
509 | - * @param string $textRight Text of right button; Note: When $plainTextRight falls back to this, HTML is automatically escaped in the HTML email |
|
510 | - * @param string $urlRight URL of right button |
|
511 | - * @param string $plainTextLeft Text of left button that is used in the plain text version - if unset the $textLeft is used |
|
512 | - * @param string $plainTextRight Text of right button that is used in the plain text version - if unset the $textRight is used |
|
513 | - */ |
|
514 | - public function addBodyButtonGroup(string $textLeft, |
|
515 | - string $urlLeft, |
|
516 | - string $textRight, |
|
517 | - string $urlRight, |
|
518 | - string $plainTextLeft = '', |
|
519 | - string $plainTextRight = '') { |
|
520 | - if ($this->footerAdded) { |
|
521 | - return; |
|
522 | - } |
|
523 | - if ($plainTextLeft === '') { |
|
524 | - $plainTextLeft = $textLeft; |
|
525 | - $textLeft = htmlspecialchars($textLeft); |
|
526 | - } |
|
527 | - |
|
528 | - if ($plainTextRight === '') { |
|
529 | - $plainTextRight = $textRight; |
|
530 | - $textRight = htmlspecialchars($textRight); |
|
531 | - } |
|
532 | - |
|
533 | - $this->ensureBodyIsOpened(); |
|
534 | - $this->ensureBodyListClosed(); |
|
535 | - |
|
536 | - $color = $this->themingDefaults->getColorPrimary(); |
|
537 | - $textColor = $this->themingDefaults->getTextColorPrimary(); |
|
538 | - |
|
539 | - $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, $textColor, $textColor, $textLeft, $urlRight, $textRight]); |
|
540 | - $this->plainBody .= $plainTextLeft . ': ' . $urlLeft . PHP_EOL; |
|
541 | - $this->plainBody .= $plainTextRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Adds a button to the body of the email |
|
546 | - * |
|
547 | - * @param string $text Text of button; Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email |
|
548 | - * @param string $url URL of button |
|
549 | - * @param string $plainText Text of button in plain text version |
|
550 | - * if empty the $text is used, if false none will be used |
|
551 | - * |
|
552 | - * @since 12.0.0 |
|
553 | - */ |
|
554 | - public function addBodyButton(string $text, string $url, $plainText = '') { |
|
555 | - if ($this->footerAdded) { |
|
556 | - return; |
|
557 | - } |
|
558 | - |
|
559 | - $this->ensureBodyIsOpened(); |
|
560 | - $this->ensureBodyListClosed(); |
|
561 | - |
|
562 | - if ($plainText === '') { |
|
563 | - $plainText = $text; |
|
564 | - $text = htmlspecialchars($text); |
|
565 | - } |
|
566 | - |
|
567 | - $color = $this->themingDefaults->getColorPrimary(); |
|
568 | - $textColor = $this->themingDefaults->getTextColorPrimary(); |
|
569 | - $this->htmlBody .= vsprintf($this->button, [$color, $color, $url, $color, $textColor, $textColor, $text]); |
|
570 | - |
|
571 | - if ($plainText !== false) { |
|
572 | - $this->plainBody .= $plainText . ': '; |
|
573 | - } |
|
574 | - |
|
575 | - $this->plainBody .= $url . PHP_EOL; |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * Close the HTML body when it is open |
|
580 | - */ |
|
581 | - protected function ensureBodyIsClosed() { |
|
582 | - if (!$this->bodyOpened) { |
|
583 | - return; |
|
584 | - } |
|
585 | - |
|
586 | - $this->ensureBodyListClosed(); |
|
587 | - |
|
588 | - $this->htmlBody .= $this->bodyEnd; |
|
589 | - $this->bodyOpened = false; |
|
590 | - } |
|
591 | - |
|
592 | - /** |
|
593 | - * Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email |
|
594 | - * |
|
595 | - * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically sent email" will be used |
|
596 | - */ |
|
597 | - public function addFooter(string $text = '', ?string $lang = null) { |
|
598 | - if ($text === '') { |
|
599 | - $l10n = $this->l10nFactory->get('lib', $lang); |
|
600 | - $text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan($lang) . '<br>' . $l10n->t('This is an automatically sent email, please do not reply.'); |
|
601 | - } |
|
602 | - |
|
603 | - if ($this->footerAdded) { |
|
604 | - return; |
|
605 | - } |
|
606 | - $this->footerAdded = true; |
|
607 | - |
|
608 | - $this->ensureBodyIsClosed(); |
|
609 | - |
|
610 | - $this->htmlBody .= vsprintf($this->footer, [$text]); |
|
611 | - $this->htmlBody .= $this->tail; |
|
612 | - $this->plainBody .= PHP_EOL . '-- ' . PHP_EOL; |
|
613 | - $this->plainBody .= str_replace('<br>', PHP_EOL, $text); |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Returns the rendered email subject as string |
|
618 | - * |
|
619 | - * @return string |
|
620 | - */ |
|
621 | - public function renderSubject(): string { |
|
622 | - return $this->subject; |
|
623 | - } |
|
624 | - |
|
625 | - /** |
|
626 | - * Returns the rendered HTML email as string |
|
627 | - * |
|
628 | - * @return string |
|
629 | - */ |
|
630 | - public function renderHtml(): string { |
|
631 | - if (!$this->footerAdded) { |
|
632 | - $this->footerAdded = true; |
|
633 | - $this->ensureBodyIsClosed(); |
|
634 | - $this->htmlBody .= $this->tail; |
|
635 | - } |
|
636 | - return $this->htmlBody; |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * Returns the rendered plain text email as string |
|
641 | - * |
|
642 | - * @return string |
|
643 | - */ |
|
644 | - public function renderText(): string { |
|
645 | - if (!$this->footerAdded) { |
|
646 | - $this->footerAdded = true; |
|
647 | - $this->ensureBodyIsClosed(); |
|
648 | - $this->htmlBody .= $this->tail; |
|
649 | - } |
|
650 | - return $this->plainBody; |
|
651 | - } |
|
349 | + public function __construct(Defaults $themingDefaults, |
|
350 | + IURLGenerator $urlGenerator, |
|
351 | + IFactory $l10nFactory, |
|
352 | + $emailId, |
|
353 | + array $data) { |
|
354 | + $this->themingDefaults = $themingDefaults; |
|
355 | + $this->urlGenerator = $urlGenerator; |
|
356 | + $this->l10nFactory = $l10nFactory; |
|
357 | + $this->htmlBody .= $this->head; |
|
358 | + $this->emailId = $emailId; |
|
359 | + $this->data = $data; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * Sets the subject of the email |
|
364 | + * |
|
365 | + * @param string $subject |
|
366 | + */ |
|
367 | + public function setSubject(string $subject) { |
|
368 | + $this->subject = $subject; |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Adds a header to the email |
|
373 | + */ |
|
374 | + public function addHeader() { |
|
375 | + if ($this->headerAdded) { |
|
376 | + return; |
|
377 | + } |
|
378 | + $this->headerAdded = true; |
|
379 | + |
|
380 | + $logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo(false)); |
|
381 | + $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getColorPrimary(), $logoUrl, $this->themingDefaults->getName()]); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Adds a heading to the email |
|
386 | + * |
|
387 | + * @param string $title |
|
388 | + * @param string|bool $plainTitle Title that is used in the plain text email |
|
389 | + * if empty the $title is used, if false none will be used |
|
390 | + */ |
|
391 | + public function addHeading(string $title, $plainTitle = '') { |
|
392 | + if ($this->footerAdded) { |
|
393 | + return; |
|
394 | + } |
|
395 | + if ($plainTitle === '') { |
|
396 | + $plainTitle = $title; |
|
397 | + } |
|
398 | + |
|
399 | + $this->htmlBody .= vsprintf($this->heading, [htmlspecialchars($title)]); |
|
400 | + if ($plainTitle !== false) { |
|
401 | + $this->plainBody .= $plainTitle . PHP_EOL . PHP_EOL; |
|
402 | + } |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Open the HTML body when it is not already |
|
407 | + */ |
|
408 | + protected function ensureBodyIsOpened() { |
|
409 | + if ($this->bodyOpened) { |
|
410 | + return; |
|
411 | + } |
|
412 | + |
|
413 | + $this->htmlBody .= $this->bodyBegin; |
|
414 | + $this->bodyOpened = true; |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Adds a paragraph to the body of the email |
|
419 | + * |
|
420 | + * @param string $text Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email |
|
421 | + * @param string|bool $plainText Text that is used in the plain text email |
|
422 | + * if empty the $text is used, if false none will be used |
|
423 | + */ |
|
424 | + public function addBodyText(string $text, $plainText = '') { |
|
425 | + if ($this->footerAdded) { |
|
426 | + return; |
|
427 | + } |
|
428 | + if ($plainText === '') { |
|
429 | + $plainText = $text; |
|
430 | + $text = htmlspecialchars($text); |
|
431 | + } |
|
432 | + |
|
433 | + $this->ensureBodyListClosed(); |
|
434 | + $this->ensureBodyIsOpened(); |
|
435 | + |
|
436 | + $this->htmlBody .= vsprintf($this->bodyText, [$text]); |
|
437 | + if ($plainText !== false) { |
|
438 | + $this->plainBody .= $plainText . PHP_EOL . PHP_EOL; |
|
439 | + } |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Adds a list item to the body of the email |
|
444 | + * |
|
445 | + * @param string $text Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email |
|
446 | + * @param string $metaInfo Note: When $plainMetaInfo falls back to this, HTML is automatically escaped in the HTML email |
|
447 | + * @param string $icon Absolute path, must be 16*16 pixels |
|
448 | + * @param string|bool $plainText Text that is used in the plain text email |
|
449 | + * if empty the $text is used, if false none will be used |
|
450 | + * @param string|bool $plainMetaInfo Meta info that is used in the plain text email |
|
451 | + * if empty the $metaInfo is used, if false none will be used |
|
452 | + * @since 12.0.0 |
|
453 | + */ |
|
454 | + public function addBodyListItem(string $text, string $metaInfo = '', string $icon = '', $plainText = '', $plainMetaInfo = '') { |
|
455 | + $this->ensureBodyListOpened(); |
|
456 | + |
|
457 | + if ($plainText === '') { |
|
458 | + $plainText = $text; |
|
459 | + $text = htmlspecialchars($text); |
|
460 | + } |
|
461 | + if ($plainMetaInfo === '') { |
|
462 | + $plainMetaInfo = $metaInfo; |
|
463 | + $metaInfo = htmlspecialchars($metaInfo); |
|
464 | + } |
|
465 | + |
|
466 | + $htmlText = $text; |
|
467 | + if ($metaInfo) { |
|
468 | + $htmlText = '<em style="color:#777;">' . $metaInfo . '</em><br>' . $htmlText; |
|
469 | + } |
|
470 | + if ($icon !== '') { |
|
471 | + $icon = '<img src="' . htmlspecialchars($icon) . '" alt="•">'; |
|
472 | + } else { |
|
473 | + $icon = '•'; |
|
474 | + } |
|
475 | + $this->htmlBody .= vsprintf($this->listItem, [$icon, $htmlText]); |
|
476 | + if ($plainText !== false) { |
|
477 | + $this->plainBody .= ' * ' . $plainText; |
|
478 | + if ($plainMetaInfo !== false) { |
|
479 | + $this->plainBody .= ' (' . $plainMetaInfo . ')'; |
|
480 | + } |
|
481 | + $this->plainBody .= PHP_EOL; |
|
482 | + } |
|
483 | + } |
|
484 | + |
|
485 | + protected function ensureBodyListOpened() { |
|
486 | + if ($this->bodyListOpened) { |
|
487 | + return; |
|
488 | + } |
|
489 | + |
|
490 | + $this->ensureBodyIsOpened(); |
|
491 | + $this->bodyListOpened = true; |
|
492 | + $this->htmlBody .= $this->listBegin; |
|
493 | + } |
|
494 | + |
|
495 | + protected function ensureBodyListClosed() { |
|
496 | + if (!$this->bodyListOpened) { |
|
497 | + return; |
|
498 | + } |
|
499 | + |
|
500 | + $this->bodyListOpened = false; |
|
501 | + $this->htmlBody .= $this->listEnd; |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Adds a button group of two buttons to the body of the email |
|
506 | + * |
|
507 | + * @param string $textLeft Text of left button; Note: When $plainTextLeft falls back to this, HTML is automatically escaped in the HTML email |
|
508 | + * @param string $urlLeft URL of left button |
|
509 | + * @param string $textRight Text of right button; Note: When $plainTextRight falls back to this, HTML is automatically escaped in the HTML email |
|
510 | + * @param string $urlRight URL of right button |
|
511 | + * @param string $plainTextLeft Text of left button that is used in the plain text version - if unset the $textLeft is used |
|
512 | + * @param string $plainTextRight Text of right button that is used in the plain text version - if unset the $textRight is used |
|
513 | + */ |
|
514 | + public function addBodyButtonGroup(string $textLeft, |
|
515 | + string $urlLeft, |
|
516 | + string $textRight, |
|
517 | + string $urlRight, |
|
518 | + string $plainTextLeft = '', |
|
519 | + string $plainTextRight = '') { |
|
520 | + if ($this->footerAdded) { |
|
521 | + return; |
|
522 | + } |
|
523 | + if ($plainTextLeft === '') { |
|
524 | + $plainTextLeft = $textLeft; |
|
525 | + $textLeft = htmlspecialchars($textLeft); |
|
526 | + } |
|
527 | + |
|
528 | + if ($plainTextRight === '') { |
|
529 | + $plainTextRight = $textRight; |
|
530 | + $textRight = htmlspecialchars($textRight); |
|
531 | + } |
|
532 | + |
|
533 | + $this->ensureBodyIsOpened(); |
|
534 | + $this->ensureBodyListClosed(); |
|
535 | + |
|
536 | + $color = $this->themingDefaults->getColorPrimary(); |
|
537 | + $textColor = $this->themingDefaults->getTextColorPrimary(); |
|
538 | + |
|
539 | + $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, $textColor, $textColor, $textLeft, $urlRight, $textRight]); |
|
540 | + $this->plainBody .= $plainTextLeft . ': ' . $urlLeft . PHP_EOL; |
|
541 | + $this->plainBody .= $plainTextRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Adds a button to the body of the email |
|
546 | + * |
|
547 | + * @param string $text Text of button; Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email |
|
548 | + * @param string $url URL of button |
|
549 | + * @param string $plainText Text of button in plain text version |
|
550 | + * if empty the $text is used, if false none will be used |
|
551 | + * |
|
552 | + * @since 12.0.0 |
|
553 | + */ |
|
554 | + public function addBodyButton(string $text, string $url, $plainText = '') { |
|
555 | + if ($this->footerAdded) { |
|
556 | + return; |
|
557 | + } |
|
558 | + |
|
559 | + $this->ensureBodyIsOpened(); |
|
560 | + $this->ensureBodyListClosed(); |
|
561 | + |
|
562 | + if ($plainText === '') { |
|
563 | + $plainText = $text; |
|
564 | + $text = htmlspecialchars($text); |
|
565 | + } |
|
566 | + |
|
567 | + $color = $this->themingDefaults->getColorPrimary(); |
|
568 | + $textColor = $this->themingDefaults->getTextColorPrimary(); |
|
569 | + $this->htmlBody .= vsprintf($this->button, [$color, $color, $url, $color, $textColor, $textColor, $text]); |
|
570 | + |
|
571 | + if ($plainText !== false) { |
|
572 | + $this->plainBody .= $plainText . ': '; |
|
573 | + } |
|
574 | + |
|
575 | + $this->plainBody .= $url . PHP_EOL; |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * Close the HTML body when it is open |
|
580 | + */ |
|
581 | + protected function ensureBodyIsClosed() { |
|
582 | + if (!$this->bodyOpened) { |
|
583 | + return; |
|
584 | + } |
|
585 | + |
|
586 | + $this->ensureBodyListClosed(); |
|
587 | + |
|
588 | + $this->htmlBody .= $this->bodyEnd; |
|
589 | + $this->bodyOpened = false; |
|
590 | + } |
|
591 | + |
|
592 | + /** |
|
593 | + * Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email |
|
594 | + * |
|
595 | + * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically sent email" will be used |
|
596 | + */ |
|
597 | + public function addFooter(string $text = '', ?string $lang = null) { |
|
598 | + if ($text === '') { |
|
599 | + $l10n = $this->l10nFactory->get('lib', $lang); |
|
600 | + $text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan($lang) . '<br>' . $l10n->t('This is an automatically sent email, please do not reply.'); |
|
601 | + } |
|
602 | + |
|
603 | + if ($this->footerAdded) { |
|
604 | + return; |
|
605 | + } |
|
606 | + $this->footerAdded = true; |
|
607 | + |
|
608 | + $this->ensureBodyIsClosed(); |
|
609 | + |
|
610 | + $this->htmlBody .= vsprintf($this->footer, [$text]); |
|
611 | + $this->htmlBody .= $this->tail; |
|
612 | + $this->plainBody .= PHP_EOL . '-- ' . PHP_EOL; |
|
613 | + $this->plainBody .= str_replace('<br>', PHP_EOL, $text); |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Returns the rendered email subject as string |
|
618 | + * |
|
619 | + * @return string |
|
620 | + */ |
|
621 | + public function renderSubject(): string { |
|
622 | + return $this->subject; |
|
623 | + } |
|
624 | + |
|
625 | + /** |
|
626 | + * Returns the rendered HTML email as string |
|
627 | + * |
|
628 | + * @return string |
|
629 | + */ |
|
630 | + public function renderHtml(): string { |
|
631 | + if (!$this->footerAdded) { |
|
632 | + $this->footerAdded = true; |
|
633 | + $this->ensureBodyIsClosed(); |
|
634 | + $this->htmlBody .= $this->tail; |
|
635 | + } |
|
636 | + return $this->htmlBody; |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * Returns the rendered plain text email as string |
|
641 | + * |
|
642 | + * @return string |
|
643 | + */ |
|
644 | + public function renderText(): string { |
|
645 | + if (!$this->footerAdded) { |
|
646 | + $this->footerAdded = true; |
|
647 | + $this->ensureBodyIsClosed(); |
|
648 | + $this->htmlBody .= $this->tail; |
|
649 | + } |
|
650 | + return $this->plainBody; |
|
651 | + } |
|
652 | 652 | } |