* @copyright 2017, Lukas Reschke * * @author Bjoern Schiessle * @author brad2014 * @author Brad Rubenstein * @author Christoph Wurst * @author Jan-Christoph Borchardt * @author Joas Schilling * @author Julius Härtl * @author Liam JACK * @author Lukas Reschke * @author medcloud <42641918+medcloud@users.noreply.github.com> * @author Morris Jobke * @author Roeland Jago Douma * @author Tomasz Paluszkiewicz * * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ namespace OC\Mail; use OCP\Defaults; use OCP\IURLGenerator; use OCP\L10N\IFactory; use OCP\Mail\IEMailTemplate; /** * Class EMailTemplate * * addBodyText and addBodyButtonGroup automatically opens the body * addFooter, renderHtml, renderText automatically closes the body and the HTML if opened * * @package OC\Mail */ class EMailTemplate implements IEMailTemplate { protected string $subject = ''; protected string $htmlBody = ''; protected string $plainBody = ''; /** indicated if the header is added */ protected bool $headerAdded = false; /** indicated if the body is already opened */ protected bool $bodyOpened = false; /** indicated if there is a list open in the body */ protected bool $bodyListOpened = false; /** indicated if the footer is added */ protected bool $footerAdded = false; protected string $head = <<
EOF; protected string $tail = <<
                                                           
EOF; protected string $header = <<
 
EOF; protected string $heading = <<

%s

 
EOF; protected string $bodyBegin = <<
EOF; protected string $bodyText = <<

%s

EOF; // note: listBegin (like bodyBegin) is not processed through sprintf, so "%" is not escaped as "%%". (bug #12151) protected string $listBegin = << EOF; protected string $listItem = << EOF; protected string $listEnd = <<

%s

%s

EOF; protected string $buttonGroup = <<  
%7\$s
%9\$s
EOF; protected string $button = <<  
%7\$s
EOF; protected string $bodyEnd = << EOF; protected string $footer = <<   EOF; public function __construct( protected Defaults $themingDefaults, protected IURLGenerator $urlGenerator, protected IFactory $l10nFactory, protected string $emailId, protected array $data, ) { $this->htmlBody .= $this->head; } /** * Sets the subject of the email */ public function setSubject(string $subject): void { $this->subject = $subject; } /** * Adds a header to the email */ public function addHeader(): void { if ($this->headerAdded) { return; } $this->headerAdded = true; $logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo(false)); $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getDefaultColorPrimary(), $logoUrl, $this->themingDefaults->getName()]); } /** * Adds a heading to the email * * @param string|bool $plainTitle Title that is used in the plain text email * if empty the $title is used, if false none will be used */ public function addHeading(string $title, $plainTitle = ''): void { if ($this->footerAdded) { return; } if ($plainTitle === '') { $plainTitle = $title; } $this->htmlBody .= vsprintf($this->heading, [htmlspecialchars($title)]); if ($plainTitle !== false) { $this->plainBody .= $plainTitle . PHP_EOL . PHP_EOL; } } /** * Open the HTML body when it is not already */ protected function ensureBodyIsOpened(): void { if ($this->bodyOpened) { return; } $this->htmlBody .= $this->bodyBegin; $this->bodyOpened = true; } /** * Adds a paragraph to the body of the email * * @param string $text Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email * @param string|bool $plainText Text that is used in the plain text email * if empty the $text is used, if false none will be used */ public function addBodyText(string $text, $plainText = ''): void { if ($this->footerAdded) { return; } if ($plainText === '') { $plainText = $text; $text = htmlspecialchars($text); } $this->ensureBodyListClosed(); $this->ensureBodyIsOpened(); $this->htmlBody .= vsprintf($this->bodyText, [$text]); if ($plainText !== false) { $this->plainBody .= $plainText . PHP_EOL . PHP_EOL; } } /** * Adds a list item to the body of the email * * @param string $text Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email * @param string $metaInfo Note: When $plainMetaInfo falls back to this, HTML is automatically escaped in the HTML email * @param string $icon Absolute path, must be 16*16 pixels * @param string|bool $plainText Text that is used in the plain text email * if empty or true the $text is used, if false none will be used * @param string|bool $plainMetaInfo Meta info that is used in the plain text email * if empty or true the $metaInfo is used, if false none will be used * @param integer $plainIndent plainIndent If > 0, Indent plainText by this amount. * @since 12.0.0 */ public function addBodyListItem( string $text, string $metaInfo = '', string $icon = '', $plainText = '', $plainMetaInfo = '', $plainIndent = 0, ): void { $this->ensureBodyListOpened(); if ($plainText === '' || $plainText === true) { $plainText = $text; $text = htmlspecialchars($text); $text = str_replace("\n", "
", $text); // convert newlines to HTML breaks } if ($plainMetaInfo === '' || $plainMetaInfo === true) { $plainMetaInfo = $metaInfo; $metaInfo = htmlspecialchars($metaInfo); } $htmlText = $text; if ($metaInfo) { $htmlText = '' . $metaInfo . '
' . $htmlText; } if ($icon !== '') { $icon = '•'; } else { $icon = '•'; } $this->htmlBody .= vsprintf($this->listItem, [$icon, $htmlText]); if ($plainText !== false) { if ($plainIndent === 0) { /* * If plainIndent is not set by caller, this is the old NC17 layout code. */ $this->plainBody .= ' * ' . $plainText; if ($plainMetaInfo !== false) { $this->plainBody .= ' (' . $plainMetaInfo . ')'; } $this->plainBody .= PHP_EOL; } else { /* * Caller can set plainIndent > 0 to format plainText in tabular fashion. * with plainMetaInfo in column 1, and plainText in column 2. * The plainMetaInfo label is right justified in a field of width * "plainIndent". Multilines after the first are indented plainIndent+1 * (to account for space after label). Fixes: #12391 */ /** @var string $label */ $label = ($plainMetaInfo !== false)? $plainMetaInfo : ''; $this->plainBody .= sprintf("%{$plainIndent}s %s\n", $label, str_replace("\n", "\n" . str_repeat(' ', $plainIndent + 1), $plainText)); } } } protected function ensureBodyListOpened(): void { if ($this->bodyListOpened) { return; } $this->ensureBodyIsOpened(); $this->bodyListOpened = true; $this->htmlBody .= $this->listBegin; } protected function ensureBodyListClosed(): void { if (!$this->bodyListOpened) { return; } $this->bodyListOpened = false; $this->htmlBody .= $this->listEnd; } /** * Adds a button group of two buttons to the body of the email * * @param string $textLeft Text of left button; Note: When $plainTextLeft falls back to this, HTML is automatically escaped in the HTML email * @param string $urlLeft URL of left button * @param string $textRight Text of right button; Note: When $plainTextRight falls back to this, HTML is automatically escaped in the HTML email * @param string $urlRight URL of right button * @param string $plainTextLeft Text of left button that is used in the plain text version - if unset the $textLeft is used * @param string $plainTextRight Text of right button that is used in the plain text version - if unset the $textRight is used */ public function addBodyButtonGroup( string $textLeft, string $urlLeft, string $textRight, string $urlRight, string $plainTextLeft = '', string $plainTextRight = '', ): void { if ($this->footerAdded) { return; } if ($plainTextLeft === '') { $plainTextLeft = $textLeft; $textLeft = htmlspecialchars($textLeft); } if ($plainTextRight === '') { $plainTextRight = $textRight; $textRight = htmlspecialchars($textRight); } $this->ensureBodyIsOpened(); $this->ensureBodyListClosed(); $color = $this->themingDefaults->getDefaultColorPrimary(); $textColor = $this->themingDefaults->getTextColorPrimary(); $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, $textColor, $textColor, $textLeft, $urlRight, $textRight]); $this->plainBody .= PHP_EOL . $plainTextLeft . ': ' . $urlLeft . PHP_EOL; $this->plainBody .= $plainTextRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; } /** * Adds a button to the body of the email * * @param string $text Text of button; Note: When $plainText falls back to this, HTML is automatically escaped in the HTML email * @param string $url URL of button * @param string|false $plainText Text of button in plain text version * if empty the $text is used, if false none will be used * * @since 12.0.0 */ public function addBodyButton(string $text, string $url, $plainText = ''): void { if ($this->footerAdded) { return; } $this->ensureBodyIsOpened(); $this->ensureBodyListClosed(); if ($plainText === '') { $plainText = $text; $text = htmlspecialchars($text); } $color = $this->themingDefaults->getDefaultColorPrimary(); $textColor = $this->themingDefaults->getTextColorPrimary(); $this->htmlBody .= vsprintf($this->button, [$color, $color, $url, $color, $textColor, $textColor, $text]); if ($plainText !== false) { $this->plainBody .= $plainText . ': '; } $this->plainBody .= $url . PHP_EOL; } /** * Close the HTML body when it is open */ protected function ensureBodyIsClosed(): void { if (!$this->bodyOpened) { return; } $this->ensureBodyListClosed(); $this->htmlBody .= $this->bodyEnd; $this->bodyOpened = false; } /** * Adds a logo and a text to the footer.
in the text will be replaced by new lines in the plain text email * * @param string $text If the text is empty the default "Name - Slogan
This is an automatically sent email" will be used */ public function addFooter(string $text = '', ?string $lang = null): void { if ($text === '') { $l10n = $this->l10nFactory->get('lib', $lang); $slogan = $this->themingDefaults->getSlogan($lang); if ($slogan !== '') { $slogan = ' - ' . $slogan; } $text = $this->themingDefaults->getName() . $slogan . '
' . $l10n->t('This is an automatically sent email, please do not reply.'); } if ($this->footerAdded) { return; } $this->footerAdded = true; $this->ensureBodyIsClosed(); $this->htmlBody .= vsprintf($this->footer, [$text]); $this->htmlBody .= $this->tail; $this->plainBody .= PHP_EOL . '-- ' . PHP_EOL; $this->plainBody .= str_replace('
', PHP_EOL, $text); } /** * Returns the rendered email subject as string */ public function renderSubject(): string { return $this->subject; } /** * Returns the rendered HTML email as string */ public function renderHtml(): string { if (!$this->footerAdded) { $this->footerAdded = true; $this->ensureBodyIsClosed(); $this->htmlBody .= $this->tail; } return $this->htmlBody; } /** * Returns the rendered plain text email as string */ public function renderText(): string { if (!$this->footerAdded) { $this->footerAdded = true; $this->ensureBodyIsClosed(); $this->htmlBody .= $this->tail; } return $this->plainBody; } }