From 3d96c9fe6bd5879addf825ec492c0f33cb64c23f Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Thu, 2 Dec 2010 19:25:32 +0100 Subject: [PATCH] Centralize generation of MIME messages. --- kronolith/lib/Event.php | 28 ++-------------- kronolith/lib/Kronolith.php | 80 ++++++++++++++++++++++++++++++-------------- kronolith/scripts/agenda.php | 28 ++-------------- 3 files changed, 59 insertions(+), 77 deletions(-) diff --git a/kronolith/lib/Event.php b/kronolith/lib/Event.php index 2c5234188..142a9d6ca 100644 --- a/kronolith/lib/Event.php +++ b/kronolith/lib/Event.php @@ -1573,12 +1573,7 @@ abstract class Kronolith_Event } } if (isset($methods['mail'])) { - $background = Horde_Themes::img('big_alarm.png'); - $image = new Horde_Mime_Part(); - $image->setType('image/png'); - $image->setContents(file_get_contents($background->fs)); - $image->setContentId(); - $image->setDisposition('attachment'); + $image = Kronolith::getImagePart('big_alarm.png'); $view = new Horde_View(array('templatePath' => KRONOLITH_TEMPLATES . '/alarm', 'encoding' => 'UTF-8')); new Horde_View_Helper_Text($view); @@ -1598,26 +1593,7 @@ abstract class Kronolith_Event $view->attendees = $attendees; } - $multipart = new Horde_Mime_Part(); - $multipart->setType('multipart/alternative'); - $bodyText = new Horde_Mime_Part(); - $bodyText->setType('text/plain'); - $bodyText->setCharset('UTF-8'); - $bodyText->setContents($view->render('mail.plain.php')); - $bodyText->setDisposition('inline'); - $multipart->addPart($bodyText); - $bodyHtml = new Horde_Mime_Part(); - $bodyHtml->setType('text/html'); - $bodyHtml->setCharset('UTF-8'); - $bodyHtml->setContents($view->render('mail.html.php')); - $bodyHtml->setDisposition('inline'); - $related = new Horde_Mime_Part(); - $related->setType('multipart/related'); - $related->setContentTypeParameter('start', $bodyHtml->setContentId()); - $related->addPart($bodyHtml); - $related->addPart($image); - $multipart->addPart($related); - $methods['mail']['mimepart'] = $multipart; + $methods['mail']['mimepart'] = Kronolith::buildMimeMessage($view, 'mail', $image); } return array( diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 4fe339a9e..227599a7e 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -2257,12 +2257,7 @@ class Kronolith // Generate image mime part first and only once, because we // need the Content-ID. - $background = Horde_Themes::img('big_invitation.png'); - $image = new Horde_Mime_Part(); - $image->setType('image/png'); - $image->setContents(file_get_contents($background->fs)); - $image->setContentId(); - $image->setDisposition('attachment'); + $image = self::getImagePart('big_invitation.png'); $share = $GLOBALS['kronolith_shares']->getShare($event->calendar); $view = new Horde_View(array('templatePath' => KRONOLITH_TEMPLATES . '/itip')); @@ -2352,25 +2347,7 @@ class Kronolith $ics->setContentTypeParameter('METHOD', $method); $ics->setCharset('UTF-8'); - $multipart = new Horde_Mime_Part(); - $multipart->setType('multipart/alternative'); - $bodyText = new Horde_Mime_Part(); - $bodyText->setType('text/plain'); - $bodyText->setCharset('UTF-8'); - $bodyText->setContents($view->render('notification.plain.php')); - $bodyText->setDisposition('inline'); - $multipart->addPart($bodyText); - $bodyHtml = new Horde_Mime_Part(); - $bodyHtml->setType('text/html'); - $bodyHtml->setCharset('UTF-8'); - $bodyHtml->setContents($view->render('notification.html.php')); - $bodyHtml->setDisposition('inline'); - $related = new Horde_Mime_Part(); - $related->setType('multipart/related'); - $related->setContentTypeParameter('start', $bodyHtml->setContentId()); - $related->addPart($bodyHtml); - $related->addPart($image); - $multipart->addPart($related); + $multipart = Kronolith::buildMimeMessage($view, 'notification', $image); $multipart->addPart($ics); $recipient = empty($status['name']) ? $email : Horde_Mime_Address::trimAddress($status['name'] . ' <' . $email . '>'); $mail = new Horde_Mime_Mail(array('subject' => $view->subject, @@ -2608,6 +2585,59 @@ class Kronolith } /** + * Builds the body MIME part of a multipart message. + * + * @param Horde_View $view A view to render the HTML and plain text + * templates for the messate. + * @param string $template The template base name for the view. + * @param Horde_Mime_Part $image The MIME part of a related image. + * + * @return Horde_Mime_Part A multipart/alternative MIME part. + */ + public static function buildMimeMessage(Horde_View $view, $template, + Horde_Mime_Part $image) + { + $multipart = new Horde_Mime_Part(); + $multipart->setType('multipart/alternative'); + $bodyText = new Horde_Mime_Part(); + $bodyText->setType('text/plain'); + $bodyText->setCharset('UTF-8'); + $bodyText->setContents($view->render($template . '.plain.php')); + $bodyText->setDisposition('inline'); + $multipart->addPart($bodyText); + $bodyHtml = new Horde_Mime_Part(); + $bodyHtml->setType('text/html'); + $bodyHtml->setCharset('UTF-8'); + $bodyHtml->setContents($view->render($template . '.html.php')); + $bodyHtml->setDisposition('inline'); + $related = new Horde_Mime_Part(); + $related->setType('multipart/related'); + $related->setContentTypeParameter('start', $bodyHtml->setContentId()); + $related->addPart($bodyHtml); + $related->addPart($image); + $multipart->addPart($related); + return $multipart; + } + + /** + * Returns a MIME part for an image to be embedded into a HTML document. + * + * @param string $file An image file name. + * + * @return Horde_Mime_Part A MIME part representing the image. + */ + public static function getImagePart($file) + { + $background = Horde_Themes::img($file); + $image = new Horde_Mime_Part(); + $image->setType('image/png'); + $image->setContents(file_get_contents($background->fs)); + $image->setContentId(); + $image->setDisposition('attachment'); + return $image; + } + + /** * @return Horde_Date */ public static function currentDate() diff --git a/kronolith/scripts/agenda.php b/kronolith/scripts/agenda.php index b00ce3e8b..992dc81ff 100755 --- a/kronolith/scripts/agenda.php +++ b/kronolith/scripts/agenda.php @@ -53,12 +53,7 @@ function send_agendas() // Generate image mime part first and only once, because we need // the Content-ID. - $background = new Horde_Themes_Image('big_agenda.png'); - $image = new Horde_Mime_Part(); - $image->setType('image/png'); - $image->setContents(file_get_contents($background->fs)); - $image->setContentId(); - $image->setDisposition('attachment'); + $image = Kronolith::getImagePart('big_agenda.png'); $runtime = new Horde_Date($runtime); $default_timezone = date_default_timezone_get(); @@ -162,26 +157,7 @@ function send_agendas() $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('UTF-8'); - $bodyText->setContents($view->render('notification.plain.php')); - $bodyText->setDisposition('inline'); - $multipart->addPart($bodyText); - $bodyHtml = new Horde_Mime_Part(); - $bodyHtml->setType('text/html'); - $bodyHtml->setCharset('UTF-8'); - $bodyHtml->setContents($view->render('notification.html.php')); - $bodyHtml->setDisposition('inline'); - $related = new Horde_Mime_Part(); - $related->setType('multipart/related'); - $related->setContentTypeParameter('start', $bodyHtml->setContentId()); - $related->addPart($bodyHtml); - $related->addPart($image); - $multipart->addPart($related); - $mime_mail->setBasePart($multipart); + $mime_mail->setBasePart(Kronolith::buildMimeMessage($view, 'notification', $image)); Horde::logMessage(sprintf('Sending daily agenda to %s', $email), 'DEBUG'); try { -- 2.11.0