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'),
73 'Auto-Submitted' => 'auto-generated'
75 $mime = new \Mail_mime(
78 'head_charset' => 'utf-8',
79 'text_charset' => 'utf-8',
80 'html_charset' => 'utf-8',
84 $mime->setTXTBody($rc->render($arEvents));
86 if ($this->css == 'inline') {
87 $html = $this->inlineCss(
88 $rht->render($arEvents),
89 Renderer_Html::getCss()
92 $html = '<style type="text/css">'
93 . Renderer_Html::getCss()
95 . $rht->render($arEvents);
97 $mime->setHTMLBody($this->minifyHtml($html));
100 $body = $mime->get();
101 $hdrs = $mime->headers($hdrs);
103 foreach ($hdrs as $k => $v) {
104 $textHeaders .= $k . ': ' . $v . "\n";
107 if (!$this->config->get('debug', false)) {
108 foreach ((array) $this->config->get('mail_to') as $recipient) {
109 mail($recipient, $subject, $body, $textHeaders);
112 echo "Subject: " . $subject . "\n";
120 * Shorten the given string to the specified length.
121 * Adds ... when the string was too long
123 * @param string $str String to shorten
124 * @param integer $len Maximum length of the string
126 * @return string Shortened string
128 protected function shorten($str, $len)
130 if (mb_strlen($str) <= $len) {
134 return mb_substr($str, 0, $len - 1) . '…';
138 * Takes the HTML and CSS code and inlines CSS into HTML.
140 * This is important for some e-mail clients which do
141 * not interpret <style> tags but only support inline styles.
143 * Works nicely with bdrem's CSS. If you need more CSS selector
144 * support, have a look at https://github.com/jjriv/emogrifier
146 * @param string $html HTML code
147 * @param string $css CSS code
149 * @return string HTML with inlined CSS
151 protected function inlineCss($html, $css)
154 '#([^{]+) {([^}]+)}#m',
159 foreach ($parts[1] as $key => $rule) {
160 $mrules = explode(',', $rule);
161 foreach ($mrules as $rule) {
163 $style = trim($parts[2][$key]);
164 $rules[$rule] = preg_replace(
174 $sx = simplexml_load_string($html);
175 foreach ($rules as $rule => $style) {
177 $parts = explode(' ', $rule);
179 foreach ($parts as $part) {
181 if (strpos($part, ':') !== false) {
183 list($part, $mode) = explode(':', $part);
184 if ($mode == 'hover') {
188 if (strpos($part, '.') === false) {
197 list($tag, $class) = explode('.', $part);
203 . 'concat(" ", normalize-space(@class), " "), '
204 . '" ' . $class . ' "'
208 $res = $sx->xpath($xp);
209 //var_dump($res);die();
210 //var_dump($xp, $style);
211 foreach ($res as $xelem) {
212 if ($mode === null) {
213 $xelem['style'] .= $style;
214 } else if ($mode == 'before') {
215 $xelem[0] = preg_replace(
216 '#content:\s*"(.+)"#', '\1', $style
222 $html = $sx->asXML();
224 $lines = explode("\n", $html);
226 $html = implode("\n", $lines);
228 //echo $html . "\n";die();
233 * Remove whitespace between tags
235 * @param string $html HTML code
237 * @return string Smaller HTML code
239 protected function minifyHtml($html)
241 $html = trim(preg_replace("#[\n\r ]+<#", '<', $html));