mail sending
authorChristian Weiske <cweiske@cweiske.de>
Mon, 3 Feb 2014 19:56:17 +0000 (20:56 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 3 Feb 2014 19:56:17 +0000 (20:56 +0100)
src/bdrem/Cli.php
src/bdrem/Config.php
src/bdrem/Renderer/Mail.php [new file with mode: 0644]
src/bdrem/UserInterface.php
src/bdrem/Web.php

index a580d9a..2199161 100644 (file)
@@ -3,14 +3,14 @@ namespace bdrem;
 
 class Cli extends UserInterface
 {
 
 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) {
     {
         $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;
             }
                 $storeInto = null;
                 continue;
             }
@@ -28,7 +28,8 @@ class Cli extends UserInterface
 
     protected function render($arEvents)
     {
 
     protected function render($arEvents)
     {
-        $r = new Renderer_Console();
+        $r = new Renderer_Mail();
+        $r->config = $this->config;
         $r->ansi = true;
         echo $r->render($arEvents);
     }
         $r->ansi = true;
         echo $r->render($arEvents);
     }
index 5d34f76..7a0fd68 100644 (file)
@@ -4,6 +4,7 @@ namespace bdrem;
 class Config
 {
     public $source;
 class Config
 {
     public $source;
+    public $date;
     public $daysBefore;
     public $daysAfter;
     public $locale;
     public $daysBefore;
     public $daysAfter;
     public $locale;
@@ -21,11 +22,9 @@ class Config
     protected function loadFile($filename)
     {
         include $filename;
     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]);
     }
 
         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 (file)
index 0000000..eee8dec
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+namespace bdrem;
+
+require_once 'Mail/mime.php';
+
+class Renderer_Mail
+{
+    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'),
+            '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
index ffaa279..cd1b7cc 100644 (file)
@@ -3,22 +3,26 @@ namespace bdrem;
 
 abstract class UserInterface
 {
 
 abstract class UserInterface
 {
+    protected $config;
+
     public function run()
     {
     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(
         $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);
     }
 
         );
         usort($arEvents, '\\bdrem\\Event::compare');
         $this->render($arEvents);
     }
 
-    protected function loadParameters($cfg)
+    protected function loadParameters()
     {
     }
 
     {
     }
 
index ab3973a..b076520 100644 (file)
@@ -9,13 +9,13 @@ class Web extends UserInterface
         echo $r->render($arEvents);
     }
 
         echo $r->render($arEvents);
     }
 
-    protected function loadParameters($cfg)
+    protected function loadParameters()
     {
         if (isset($_GET['daysBefore'])) {
     {
         if (isset($_GET['daysBefore'])) {
-            $cfg->daysBefore = (int) $_GET['daysBefore'];
+            $this->config->daysBefore = (int) $_GET['daysBefore'];
         }
         if (isset($_GET['daysAfter'])) {
         }
         if (isset($_GET['daysAfter'])) {
-            $cfg->daysAfter = (int) $_GET['daysAfter'];
+            $this->config->daysAfter = (int) $_GET['daysAfter'];
         }
     }
 }
         }
     }
 }