From c39652345e5b1c645a22bde2df1eaa20231e6fba Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Thu, 29 Jul 2010 14:26:28 +0200 Subject: [PATCH] Send agenda emails with HTML part and convert to Horde_View. --- kronolith/docs/CHANGES | 1 + kronolith/scripts/agenda.php | 68 +++++++++++------------ kronolith/templates/agenda/notification.html.php | 19 +++++++ kronolith/templates/agenda/notification.plain.php | 11 ++++ 4 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 kronolith/templates/agenda/notification.html.php create mode 100644 kronolith/templates/agenda/notification.plain.php diff --git a/kronolith/docs/CHANGES b/kronolith/docs/CHANGES index a07e6d581..ea550c959 100644 --- a/kronolith/docs/CHANGES +++ b/kronolith/docs/CHANGES @@ -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 diff --git a/kronolith/scripts/agenda.php b/kronolith/scripts/agenda.php index d8f79b430..fd7b81d67 100755 --- a/kronolith/scripts/agenda.php +++ b/kronolith/scripts/agenda.php @@ -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 index 000000000..b80afcbce --- /dev/null +++ b/kronolith/templates/agenda/notification.html.php @@ -0,0 +1,19 @@ +

h(sprintf(_("Your daily agenda for %s"), $this->date)) ?>

+ + + events as $event): ?> + + + + + + + +
+ isAllDay()): ?> + h(_("All day")) ?> + + h($event->start->format($this->timeformat)) ?> + + + h($event->title) ?>
diff --git a/kronolith/templates/agenda/notification.plain.php b/kronolith/templates/agenda/notification.plain.php new file mode 100644 index 000000000..3970e569a --- /dev/null +++ b/kronolith/templates/agenda/notification.plain.php @@ -0,0 +1,11 @@ +date) ?> + + +events as $event): ?> +isAllDay()): ?> +pad) . $event->title ?> + +start->format($this->timeformat) . ':', $this->pad) . $event->title ?> + + + -- 2.11.0