9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/bdrem.htm
16 require_once 'Mail/mime.php';
23 * @author Christian Weiske <cweiske@cweiske.de>
24 * @copyright 2014 Christian Weiske
25 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
26 * @version Release: @package_version@
27 * @link http://cweiske.de/bdrem.htm
29 class Renderer_Mail extends Renderer
32 * Add HTML part to email
38 * CSS "inline" in tags, or "separate" in a style block
41 public $css = 'inline';
44 * Render the events - send out mails.
46 * Uses the config's "mail_to" array as recipients.
47 * Sends out a single mail for each recipient.
48 * Config "mail_from" can also be used.
50 * @param array $arEvents Array of events to display
54 public function render($arEvents)
57 foreach ($arEvents as $event) {
58 if ($event->days == 0) {
59 $todays[] = $this->shorten($event->title, 15);
62 $subject = 'Birthday reminder';
64 $subject .= ': ' . implode(', ', $todays);
67 $rc = new Renderer_Console();
68 $rht = new Renderer_HtmlTable();
71 'From' => $this->config->get('mail_from', 'birthday@example.org'),
72 'Auto-Submitted' => 'auto-generated'
74 $mime = new \Mail_mime(
77 'head_charset' => 'utf-8',
78 'text_charset' => 'utf-8',
79 'html_charset' => 'utf-8',
83 $mime->setTXTBody($rc->render($arEvents));
85 if ($this->css == 'inline') {
86 $html = $this->inlineCss(
87 $rht->render($arEvents),
88 Renderer_Html::getCss()
91 $html = '<style type="text/css">'
92 . Renderer_Html::getCss()
94 . $rht->render($arEvents);
96 $mime->setHTMLBody($this->minifyHtml($html));
100 $hdrs = $mime->headers($hdrs);
102 foreach ($hdrs as $k => $v) {
103 $textHeaders .= $k . ': ' . $v . "\n";
106 if (!$this->config->get('debug', false)) {
107 foreach ((array) $this->config->get('mail_to') as $recipient) {
108 mail($recipient, $subject, $body, $textHeaders);
111 echo "Subject: " . $subject . "\n";
119 * Shorten the given string to the specified length.
120 * Adds ... when the string was too long
122 * @param string $str String to shorten
123 * @param integer $len Maximum length of the string
125 * @return string Shortened string
127 protected function shorten($str, $len)
129 if (mb_strlen($str) <= $len) {
133 return mb_substr($str, 0, $len - 1) . '…';
137 * Takes the HTML and CSS code and inlines CSS into HTML.
139 * This is important for some e-mail clients which do
140 * not interpret <style> tags but only support inline styles.
142 * Works nicely with bdrem's CSS. If you need more CSS selector
143 * support, have a look at https://github.com/jjriv/emogrifier
145 * @param string $html HTML code
146 * @param string $css CSS code
148 * @return string HTML with inlined CSS
150 protected function inlineCss($html, $css)
153 '#([^{]+) {([^}]+)}#m',
158 foreach ($parts[1] as $key => $rule) {
159 $mrules = explode(',', $rule);
160 foreach ($mrules as $rule) {
162 $style = trim($parts[2][$key]);
163 $rules[$rule] = preg_replace(
173 $sx = simplexml_load_string($html);
174 foreach ($rules as $rule => $style) {
176 $parts = explode(' ', $rule);
178 foreach ($parts as $part) {
180 if (strpos($part, ':') !== false) {
182 list($part, $mode) = explode(':', $part);
183 if ($mode == 'hover') {
187 if (strpos($part, '.') === false) {
196 list($tag, $class) = explode('.', $part);
202 . 'concat(" ", normalize-space(@class), " "), '
203 . '" ' . $class . ' "'
207 $res = $sx->xpath($xp);
208 //var_dump($res);die();
209 //var_dump($xp, $style);
210 foreach ($res as $xelem) {
211 if ($mode === null) {
212 $xelem['style'] .= $style;
213 } else if ($mode == 'before') {
214 $xelem[0] = preg_replace(
215 '#content:\s*"(.+)"#', '\1', $style
221 $html = $sx->asXML();
223 $lines = explode("\n", $html);
225 $html = implode("\n", $lines);
227 //echo $html . "\n";die();
232 * Remove whitespace between tags
234 * @param string $html HTML code
236 * @return string Smaller HTML code
238 protected function minifyHtml($html)
240 $html = trim(preg_replace("#[\n\r ]+<#", '<', $html));