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 * Render the events - send out mails.
34 * Uses the config's "mail_to" array as recipients.
35 * Sends out a single mail for each recipient.
36 * Config "mail_from" can also be used.
38 * @param array $arEvents Array of events to display
42 public function render($arEvents)
45 foreach ($arEvents as $event) {
46 if ($event->days == 0) {
47 $todays[] = $this->shorten($event->title, 15);
50 $subject = 'Birthday reminder';
52 $subject .= ': ' . implode(', ', $todays);
55 $rc = new Renderer_Console();
56 $rh = new Renderer_Html();
59 'From' => $this->config->get('mail_from', 'birthday@example.org'),
60 'Auto-Submitted' => 'auto-generated'
62 $mime = new \Mail_mime(
65 'head_charset' => 'utf-8',
66 'text_charset' => 'utf-8',
67 'html_charset' => 'utf-8',
71 $mime->setTXTBody($rc->render($arEvents));
72 $mime->setHTMLBody($rh->render($arEvents));
75 $hdrs = $mime->headers($hdrs);
77 foreach ($hdrs as $k => $v) {
78 $textHeaders .= $k . ':' . $v . "\n";
81 foreach ((array) $this->config->get('mail_to') as $recipient) {
82 mail($recipient, $subject, $body, $textHeaders);
87 * Shorten the given string to the specified length.
88 * Adds ... when the string was too long
90 * @param string $str String to shorten
91 * @param integer $len Maximum length of the string
93 * @return string Shortened string
95 protected function shorten($str, $len)
97 if (mb_strlen($str) <= $len) {
101 return mb_substr($str, 0, $len - 1) . '…';