add Auto-Submitted: auto-generated mail header (RFC #3834, sect 5)
[bdrem.git] / src / bdrem / Renderer / Mail.php
1 <?php
2 namespace bdrem;
3
4 require_once 'Mail/mime.php';
5
6 class Renderer_Mail extends Renderer
7 {
8     public function render($arEvents)
9     {
10         $todays = array();
11         foreach ($arEvents as $event) {
12             if ($event->days == 0) {
13                 $todays[] = $this->shorten($event->title, 15);
14             }
15         }
16         $subject = 'Birthday reminder';
17         if (count($todays)) {
18             $subject .= ': ' . implode(', ', $todays);
19         }
20
21         $rc = new Renderer_Console();
22         $rh = new Renderer_Html();
23
24         $hdrs = array(
25             'From'    => $this->config->get('mail_from', 'birthday@example.org'),
26             'Auto-Submitted' => 'auto-generated'
27         );
28         $mime = new \Mail_mime(
29             array(
30                 'eol' => "\n",
31                 'head_charset' => 'utf-8',
32                 'text_charset' => 'utf-8',
33                 'html_charset' => 'utf-8',
34             )
35         );
36
37         $mime->setTXTBody($rc->render($arEvents));
38         $mime->setHTMLBody($rh->render($arEvents));
39
40         $body = $mime->get();
41         $hdrs = $mime->headers($hdrs);
42         $textHeaders = '';
43         foreach ($hdrs as $k => $v) {
44             $textHeaders .= $k . ':' . $v  . "\n";
45         }
46
47         foreach ((array) $this->config->get('mail_to') as $recipient) {
48             mail($recipient, $subject, $body, $textHeaders);
49         }
50     }
51
52     protected function shorten($str, $len)
53     {
54         if (mb_strlen($str) <= $len) {
55             return $str;
56         }
57
58         return mb_substr($str, 0, $len - 1) . '…';
59     }
60 }
61 ?>