Send agenda emails with HTML part and convert to Horde_View.
authorJan Schneider <jan@horde.org>
Thu, 29 Jul 2010 12:26:28 +0000 (14:26 +0200)
committerJan Schneider <jan@horde.org>
Thu, 29 Jul 2010 12:26:28 +0000 (14:26 +0200)
kronolith/docs/CHANGES
kronolith/scripts/agenda.php
kronolith/templates/agenda/notification.html.php [new file with mode: 0644]
kronolith/templates/agenda/notification.plain.php [new file with mode: 0644]

index a07e6d5..ea550c9 100644 (file)
@@ -2,6 +2,7 @@
 v3.0-git
 --------
 
+[jan] Send agenda emails with HTML part and convert to Horde_View.
 [mjr] More complete handling of recurring event exceptions when dealing with the
       iCalendar format (Request #9091).
 [jan] Add remote calendars to the possible list of calendars for free/busy
index d8f79b4..fd7b81d 100755 (executable)
@@ -54,6 +54,8 @@ function send_agendas()
     $runtime = new Horde_Date($runtime);
     $default_timezone = date_default_timezone_get();
     $kronolith_driver = Kronolith::getDriver();
+    $view = new Horde_View(array('templatePath' => KRONOLITH_TEMPLATES . '/agenda', 'encoding' => $GLOBALS['registry']->getCharset()));
+    new Horde_View_Helper_Text($view);
 
     // Loop through the users and generate an agenda for them
     foreach ($users as $user) {
@@ -62,26 +64,18 @@ function send_agendas()
         $prefs->retrieve();
         $agenda_calendars = $prefs->getValue('daily_agenda');
 
-        // Check if user has a timezone pref, and set it. Otherwise, make
-        // sure to use the server's default timezone.
-        $tz = $prefs->getValue('timezone');
-        date_default_timezone_set(empty($tz) ? $default_timezone : $tz);
-
         if (!$agenda_calendars) {
             continue;
         }
 
-        // try to find an email address for the user
+        // Try to find an email address for the user.
         $identity = $GLOBALS['injector']->getInstance('Horde_Prefs_Identity')->getIdentity($user);
-        $email = $identity->getValue('from_addr');
-        if (strstr($email, '@')) {
-            list($mailbox, $host) = explode('@', $email);
-            $email = Horde_Mime_Address::writeAddress($mailbox, $host, $identity->getValue('fullname'));
-        }
+        $email = $identity->getDefaultFromAddress(true);
 
-        if (empty($email)) {
-            continue;
-        }
+        // Check if user has a timezone pref, and set it. Otherwise, make
+        // sure to use the server's default timezone.
+        $tz = $prefs->getValue('timezone');
+        date_default_timezone_set(empty($tz) ? $default_timezone : $tz);
 
         // If we found an email address, generate the agenda.
         switch ($agenda_calendars) {
@@ -130,31 +124,37 @@ function send_agendas()
         $lang = $prefs->getValue('language');
         $twentyFour = $prefs->getValue('twentyFour');
         $dateFormat = $prefs->getValue('date_format');
-        $GLOBALS['registry']->setLanguageEnvironment($lang);
-        $mime_mail = new Horde_Mime_Mail(array('subject' => sprintf(_("Your daily agenda for %s"), $runtime->strftime($dateFormat)),
-                                               'to' => $email,
-                                               'from' => $GLOBALS['conf']['reminder']['from_addr'],
-                                               'charset' => $GLOBALS['registry']->getCharset()));
-        $mime_mail->addHeader('User-Agent', 'Kronolith ' . $GLOBALS['registry']->getVersion());
 
-        $pad = max(Horde_String::length(_("All day")) + 2, $twentyFour ? 6 : 8);
-
-        $message = sprintf(_("Your daily agenda for %s"),
-                           $runtime->strftime($dateFormat))
-            . "\n\n";
-        foreach ($eventlist as $event) {
-            if ($event->isAllDay()) {
-                $message .= str_pad(_("All day") . ':', $pad);
-            } else {
-                $message .= str_pad($event->start->format($twentyFour  ? 'H:i' : 'h:ia'), $pad);
-            }
-            $message .= $event->title . "\n";
-        }
+        $view->pad = max(Horde_String::length(_("All day")) + 2, $twentyFour ? 6 : 8);
+        $view->date = $runtime->strftime($dateFormat);
+        $view->timeformat = $twentyFour  ? 'H:i' : 'h:ia';
+        $view->events = $eventlist;
 
-        $mime_mail->setBody($message, $GLOBALS['registry']->getCharset(), true);
+        $GLOBALS['registry']->setLanguageEnvironment($lang);
+        $mime_mail = new Horde_Mime_Mail(
+            array('subject' => sprintf(_("Your daily agenda for %s"), $view->date),
+                  'to' => $email,
+                  'from' => $GLOBALS['conf']['reminder']['from_addr'],
+                  'charset' => $GLOBALS['registry']->getCharset()));
+        $mime_mail->addHeader('User-Agent', 'Kronolith ' . $GLOBALS['registry']->getVersion());
         try {
             $mime_mail->addRecipients($email);
         } catch (Horde_Mime_Exception $e) {}
+
+        $multipart = new Horde_Mime_Part();
+        $multipart->setType('multipart/alternative');
+        $bodyText = new Horde_Mime_Part();
+        $bodyText->setType('text/plain');
+        $bodyText->setCharset($GLOBALS['registry']->getCharset());
+        $bodyText->setContents($view->render('notification.plain.php'));
+        $multipart->addPart($bodyText);
+        $bodyHtml = new Horde_Mime_Part();
+        $bodyHtml->setType('text/html');
+        $bodyHtml->setCharset($GLOBALS['registry']->getCharset());
+        $bodyHtml->setContents($view->render('notification.html.php'));
+        $multipart->addPart($bodyHtml);
+        $mime_mail->setBasePart($multipart);
+
         Horde::logMessage(sprintf('Sending daily agenda to %s', $email), 'DEBUG');
         try {
             $mime_mail->send($GLOBALS['injector']->getInstance('Horde_Mail'), false, false);
diff --git a/kronolith/templates/agenda/notification.html.php b/kronolith/templates/agenda/notification.html.php
new file mode 100644 (file)
index 0000000..b80afcb
--- /dev/null
@@ -0,0 +1,19 @@
+<h3><?php echo $this->h(sprintf(_("Your daily agenda for %s"), $this->date)) ?></h3>
+
+<table style="border-collapse:collapse;border:1px solid #000" border="1">
+  <?php foreach ($this->events as $event): ?>
+
+  <tr>
+    <td>
+      <?php if ($event->isAllDay()): ?>
+      <?php echo $this->h(_("All day")) ?>
+      <?php else: ?>
+      <?php echo $this->h($event->start->format($this->timeformat)) ?>
+      <?php endif ?>
+
+    </td>
+    <td><a href="<?php echo $event->getViewUrl(array(), true) ?>"><?php echo $this->h($event->title) ?></a></td>
+  </tr>
+  <?php endforeach ?>
+
+</table>
diff --git a/kronolith/templates/agenda/notification.plain.php b/kronolith/templates/agenda/notification.plain.php
new file mode 100644 (file)
index 0000000..3970e56
--- /dev/null
@@ -0,0 +1,11 @@
+<?php printf(_("Your daily agenda for %s"), $this->date) ?>
+
+
+<?php foreach ($this->events as $event): ?>
+<?php if ($event->isAllDay()): ?>
+<?php echo str_pad(_("All day") . ':', $this->pad) . $event->title ?>
+<?php else: ?>
+<?php echo str_pad($event->start->format($this->timeformat) . ':', $this->pad) . $event->title ?>
+<?php endif ?>
+
+<?php endforeach ?>