d7aef3e8d567b9d7442edeb092416a6ff16c7ff8
[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         if (!$this->config->get('debug', false)) {
82             foreach ((array) $this->config->get('mail_to') as $recipient) {
83                 mail($recipient, $subject, $body, $textHeaders);
84             }
85         } else {
86             echo "Subject: " . $subject . "\n";
87             echo $textHeaders;
88             echo "\n";
89             echo $body;
90         }
91     }
92
93     /**
94      * Shorten the given string to the specified length.
95      * Adds ... when the string was too long
96      *
97      * @param string  $str String to shorten
98      * @param integer $len Maximum length of the string
99      *
100      * @return string Shortened string
101      */
102     protected function shorten($str, $len)
103     {
104         if (mb_strlen($str) <= $len) {
105             return $str;
106         }
107
108         return mb_substr($str, 0, $len - 1) . '…';
109     }
110 }
111 ?>