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