dummy data set
[bdrem.git] / src / bdrem / Renderer / Mail.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Bdrem
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
13  */
14 namespace bdrem;
15
16 require_once 'Mail/mime.php';
17
18 /**
19  * Send out mails
20  *
21  * @category  Tools
22  * @package   Bdrem
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
28  */
29 class Renderer_Mail extends Renderer
30 {
31     /**
32      * Render the events - send out mails.
33      *
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.
37      *
38      * @param array $arEvents Array of events to display
39      *
40      * @return void
41      */
42     public function render($arEvents)
43     {
44         $todays = array();
45         foreach ($arEvents as $event) {
46             if ($event->days == 0) {
47                 $todays[] = $this->shorten($event->title, 15);
48             }
49         }
50         $subject = 'Birthday reminder';
51         if (count($todays)) {
52             $subject .= ': ' . implode(', ', $todays);
53         }
54
55         $rc = new Renderer_Console();
56         $rh = new Renderer_Html();
57
58         $hdrs = array(
59             'From'    => $this->config->get('mail_from', 'birthday@example.org'),
60             'Auto-Submitted' => 'auto-generated'
61         );
62         $mime = new \Mail_mime(
63             array(
64                 'eol' => "\n",
65                 'head_charset' => 'utf-8',
66                 'text_charset' => 'utf-8',
67                 'html_charset' => 'utf-8',
68             )
69         );
70
71         $mime->setTXTBody($rc->render($arEvents));
72         $mime->setHTMLBody($rh->render($arEvents));
73
74         $body = $mime->get();
75         $hdrs = $mime->headers($hdrs);
76         $textHeaders = '';
77         foreach ($hdrs as $k => $v) {
78             $textHeaders .= $k . ':' . $v  . "\n";
79         }
80
81         foreach ((array) $this->config->get('mail_to') as $recipient) {
82             mail($recipient, $subject, $body, $textHeaders);
83         }
84     }
85
86     /**
87      * Shorten the given string to the specified length.
88      * Adds ... when the string was too long
89      *
90      * @param string  $str String to shorten
91      * @param integer $len Maximum length of the string
92      *
93      * @return string Shortened string
94      */
95     protected function shorten($str, $len)
96     {
97         if (mb_strlen($str) <= $len) {
98             return $str;
99         }
100
101         return mb_substr($str, 0, $len - 1) . '…';
102     }
103 }
104 ?>