From: Christian Weiske Date: Mon, 3 Feb 2014 19:56:17 +0000 (+0100) Subject: mail sending X-Git-Tag: v0.5.0~46 X-Git-Url: https://git.cweiske.de/bdrem.git/commitdiff_plain/6506b1ebe1ecaa6630d6d849c39b9e9d53603699?ds=sidebyside mail sending --- diff --git a/src/bdrem/Cli.php b/src/bdrem/Cli.php index a580d9a..2199161 100644 --- a/src/bdrem/Cli.php +++ b/src/bdrem/Cli.php @@ -3,14 +3,14 @@ namespace bdrem; class Cli extends UserInterface { - protected function loadParameters($cfg) + protected function loadParameters() { $params = $GLOBALS['argv']; array_shift($params); $storeInto = null; foreach ($params as $param) { if ($storeInto !== null) { - $cfg->$storeInto = (int)$param; + $this->config->$storeInto = (int)$param; $storeInto = null; continue; } @@ -28,7 +28,8 @@ class Cli extends UserInterface protected function render($arEvents) { - $r = new Renderer_Console(); + $r = new Renderer_Mail(); + $r->config = $this->config; $r->ansi = true; echo $r->render($arEvents); } diff --git a/src/bdrem/Config.php b/src/bdrem/Config.php index 5d34f76..7a0fd68 100644 --- a/src/bdrem/Config.php +++ b/src/bdrem/Config.php @@ -4,6 +4,7 @@ namespace bdrem; class Config { public $source; + public $date; public $daysBefore; public $daysAfter; public $locale; @@ -21,11 +22,9 @@ class Config protected function loadFile($filename) { include $filename; - $this->source = $source; - $this->daysBefore = $daysBefore; - $this->daysAfter = $daysAfter; - if (isset($locale)) { - $this->locale = $locale; + $vars = get_defined_vars(); + foreach ($vars as $k => $value) { + $this->$k = $value; } } @@ -40,5 +39,13 @@ class Config return new $class($settings[0]); } + + public function get($varname, $default = '') + { + if (!isset($this->$varname) || $this->$varname == '') { + return $default; + } + return $this->$varname; + } } ?> diff --git a/src/bdrem/Renderer/Mail.php b/src/bdrem/Renderer/Mail.php new file mode 100644 index 0000000..eee8dec --- /dev/null +++ b/src/bdrem/Renderer/Mail.php @@ -0,0 +1,61 @@ +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'), + 'Subject' => $subject + ); + $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); + } + } + + protected function shorten($str, $len) + { + if (mb_strlen($str) <= $len) { + return $str; + } + + return mb_substr($str, 0, $len - 1) . '…'; + } +} +?> \ No newline at end of file diff --git a/src/bdrem/UserInterface.php b/src/bdrem/UserInterface.php index ffaa279..cd1b7cc 100644 --- a/src/bdrem/UserInterface.php +++ b/src/bdrem/UserInterface.php @@ -3,22 +3,26 @@ namespace bdrem; abstract class UserInterface { + protected $config; + public function run() { - $cfg = new Config(); - $cfg->load(); - setlocale(LC_TIME, $cfg->locale); - $source = $cfg->loadSource(); + $this->config = new Config(); + $this->config->load(); + $this->config->date = date('Y-m-d'); + setlocale(LC_TIME, $this->config->locale); + $source = $this->config->loadSource(); - $this->loadParameters($cfg); + $this->loadParameters($this->config); $arEvents = $source->getEvents( - date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter + $this->config->date, + $this->config->daysBefore, $this->config->daysAfter ); usort($arEvents, '\\bdrem\\Event::compare'); $this->render($arEvents); } - protected function loadParameters($cfg) + protected function loadParameters() { } diff --git a/src/bdrem/Web.php b/src/bdrem/Web.php index ab3973a..b076520 100644 --- a/src/bdrem/Web.php +++ b/src/bdrem/Web.php @@ -9,13 +9,13 @@ class Web extends UserInterface echo $r->render($arEvents); } - protected function loadParameters($cfg) + protected function loadParameters() { if (isset($_GET['daysBefore'])) { - $cfg->daysBefore = (int) $_GET['daysBefore']; + $this->config->daysBefore = (int) $_GET['daysBefore']; } if (isset($_GET['daysAfter'])) { - $cfg->daysAfter = (int) $_GET['daysAfter']; + $this->config->daysAfter = (int) $_GET['daysAfter']; } } }