* @copyright 2017, Lukas Reschke * * @author Morris Jobke * @author Lukas Reschke * * @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\IL10N; use OCP\IURLGenerator; /** * 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 { /** @var Defaults */ protected $themingDefaults; /** @var IURLGenerator */ protected $urlGenerator; /** @var IL10N */ protected $l10n; /** @var string */ protected $htmlBody = ''; /** @var string */ protected $plainBody = ''; /** @var bool indicated if the footer is added */ protected $headerAdded = false; /** @var bool indicated if the body is already opened */ protected $bodyOpened = false; /** @var bool indicated if the footer is added */ protected $footerAdded = false; protected $head = <<
EOF; protected $tail = <<
                                                           
EOF; protected $header = <<
 
EOF; protected $heading = <<

%s

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

%s

EOF; protected $buttonGroup = <<  
%s
%s
EOF; protected $bodyEnd = << EOF; protected $footer = <<   EOF; /** * @param Defaults $themingDefaults * @param IURLGenerator $urlGenerator * @param IL10N $l10n */ public function __construct(Defaults $themingDefaults, IURLGenerator $urlGenerator, IL10N $l10n) { $this->themingDefaults = $themingDefaults; $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; $this->htmlBody .= $this->head; } /** * Adds a header to the email */ public function addHeader() { if ($this->headerAdded) { return; } $this->headerAdded = true; $logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo()); $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getColorPrimary(), $logoUrl]); } /** * Adds a heading to the email * * @param string $title */ public function addHeading($title) { if ($this->footerAdded) { return; } $this->htmlBody .= vsprintf($this->heading, [$title]); $this->plainBody .= $title . PHP_EOL . PHP_EOL; } /** * Adds a paragraph to the body of the email * * @param string $text */ public function addBodyText($text) { if ($this->footerAdded) { return; } if (!$this->bodyOpened) { $this->htmlBody .= $this->bodyBegin; $this->bodyOpened = true; } $this->htmlBody .= vsprintf($this->bodyText, [$text]); $this->plainBody .= $text . PHP_EOL . PHP_EOL; } /** * Adds a button group of two buttons to the body of the email * * @param string $textLeft Text of left button * @param string $urlLeft URL of left button * @param string $textRight Text of right button * @param string $urlRight URL of right button */ public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight) { if ($this->footerAdded) { return; } if (!$this->bodyOpened) { $this->htmlBody .= $this->bodyBegin; $this->bodyOpened = true; } $color = $this->themingDefaults->getColorPrimary(); $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, $textLeft, $urlRight, $textRight]); $this->plainBody .= $textLeft . ': ' . $urlLeft . PHP_EOL; $this->plainBody .= $textRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; } /** * 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 */ public function addFooter($text = '') { if($text === '') { $text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan() . '
' . $this->l10n->t('This is an automatically generated email, please do not reply.'); } if ($this->footerAdded) { return; } $this->footerAdded = true; if ($this->bodyOpened) { $this->htmlBody .= $this->bodyEnd; $this->bodyOpened = false; } $this->htmlBody .= vsprintf($this->footer, [$text]); $this->htmlBody .= $this->tail; $this->plainBody .= '--' . PHP_EOL; $this->plainBody .= str_replace('
', PHP_EOL, $text); } /** * Returns the rendered HTML email as string * * @return string */ public function renderHTML() { if (!$this->footerAdded) { $this->footerAdded = true; if ($this->bodyOpened) { $this->htmlBody .= $this->bodyEnd; } $this->htmlBody .= $this->tail; } return $this->htmlBody; } /** * Returns the rendered plain text email as string * * @return string */ public function renderText() { if (!$this->footerAdded) { $this->footerAdded = true; if ($this->bodyOpened) { $this->htmlBody .= $this->bodyEnd; } $this->htmlBody .= $this->tail; } return $this->plainBody; } }