* * @author Morris Jobke * * @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; /** * 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 \OC_Defaults */ protected $defaults; /** @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; public function __construct(\OC_Defaults $defaults) { $this->defaults = $defaults; $this->htmlBody .= $this->head; } /** * Adds a header to the email * * @param string $logoUrl */ public function addHeader($logoUrl) { if ($this->headerAdded) { return; } $this->headerAdded = true; $this->htmlBody .= vsprintf($this->header, [$this->defaults->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->defaults->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 $logoUrl * @param string $text */ public function addFooter($logoUrl, $text) { if ($this->footerAdded) { return; } $this->footerAdded = true; if ($this->bodyOpened) { $this->htmlBody .= $this->bodyEnd; $this->bodyOpened = false; } $this->htmlBody .= vsprintf($this->footer, [$logoUrl, $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; } }