Centralize generation of MIME messages.
authorJan Schneider <jan@horde.org>
Thu, 2 Dec 2010 18:25:32 +0000 (19:25 +0100)
committerJan Schneider <jan@horde.org>
Thu, 2 Dec 2010 18:27:57 +0000 (19:27 +0100)
kronolith/lib/Event.php
kronolith/lib/Kronolith.php
kronolith/scripts/agenda.php

index 2c52341..142a9d6 100644 (file)
@@ -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(
index 4fe339a..227599a 100644 (file)
@@ -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()
index b00ce3e..992dc81 100755 (executable)
@@ -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 {