Fix setting the charset in the ctor.
authorJan Schneider <jan@horde.org>
Mon, 15 Jun 2009 17:10:47 +0000 (19:10 +0200)
committerJan Schneider <jan@horde.org>
Tue, 16 Jun 2009 12:59:10 +0000 (14:59 +0200)
Add setBasePart().

framework/Mime/lib/Horde/Mime/Mail.php

index d290a49..66c7d9d 100644 (file)
@@ -21,6 +21,13 @@ class Horde_Mime_Mail
     protected $_headers;
 
     /**
+     * The base MIME part.
+     *
+     * @var Horde_Mime_Part
+     */
+    protected $_base;
+
+    /**
      * The main body part.
      *
      * @var Horde_Mime_Part
@@ -83,24 +90,27 @@ class Horde_Mime_Mail
      * @throws Horde_Mime_Exception
      */
     function __construct($subject = null, $body = null, $to = null,
-                         $from = null, $charset = 'iso-8859-1')
+                         $from = null, $charset = null)
     {
         /* Set SERVER_NAME. */
         if (!isset($_SERVER['SERVER_NAME'])) {
             $_SERVER['SERVER_NAME'] = php_uname('n');
         }
+        if (!$charset) {
+            $charset = 'iso-8859-1';
+        }
 
         $this->_headers = new Horde_Mime_Headers();
         $this->_charset = $charset;
 
         if ($subject) {
-            $this->addHeader('Subject', $subject);
+            $this->addHeader('Subject', $subject, $charset);
         }
         if ($to) {
-            $this->addHeader('To', $to);
+            $this->addHeader('To', $to, $charset);
         }
         if ($from) {
-            $this->addHeader('From', $from);
+            $this->addHeader('From', $from, $charset);
         }
         if ($body) {
             $this->setBody($body, $charset);
@@ -266,6 +276,19 @@ class Horde_Mime_Mail
     }
 
     /**
+     * Sets the base MIME part.
+     *
+     * If the base part is set, any text bodies will be ignored when building
+     * the message.
+     *
+     * @param Horde_Mime_Part $part  A Horde_Mime_Part object.
+     */
+    public function setBasePart($part)
+    {
+        $this->_base = $part;
+    }
+
+    /**
      * Adds an attachment.
      *
      * @param string $file     The path to the file.
@@ -407,40 +430,44 @@ class Horde_Mime_Mail
             $this->_headers->addHeader('Date', date('r'));
         }
 
-        /* Send in flowed format. */
-        if ($flowed && !empty($this->_body)) {
-            $flowed = new Horde_Text_Flowed($this->_body->getContents(), $this->_body->getCharset());
-            $flowed->setDelSp(true);
-            $this->_body->setContentTypeParameter('format', 'flowed');
-            $this->_body->setContentTypeParameter('DelSp', 'Yes');
-            $this->_body->setContents($flowed->toFlowed());
-        }
+        if (isset($this->_base)) {
+            $basepart = $this->_base;
+        } else {
+            /* Send in flowed format. */
+            if ($flowed && !empty($this->_body)) {
+                $flowed = new Horde_Text_Flowed($this->_body->getContents(), $this->_body->getCharset());
+                $flowed->setDelSp(true);
+                $this->_body->setContentTypeParameter('format', 'flowed');
+                $this->_body->setContentTypeParameter('DelSp', 'Yes');
+                $this->_body->setContents($flowed->toFlowed());
+            }
 
-        /* Build mime message. */
-        $body = null;
-        if (!empty($this->_body) && !empty($this->_htmlBody)) {
-            $body = new Horde_Mime_Part();
-            $body->setType('multipart/alternative');
-            $this->_body->setDescription(_("Plaintext Version of Message"));
-            $body->addPart($this->_body);
-            $this->_htmlBody->setDescription(_("HTML Version of Message"));
-            $body->addPart($this->_htmlBody);
-        } elseif (!empty($this->_htmlBody)) {
-            $body = $this->_htmlBody;
-        } elseif (!empty($this->_body)) {
-            $body = $this->_body;
-        }
-        if (count($this->_parts)) {
-            $basepart = new Horde_Mime_Part();
-            $basepart->setType('multipart/mixed');
-            if ($body) {
-                $basepart->addPart($body);
+            /* Build mime message. */
+            $body = null;
+            if (!empty($this->_body) && !empty($this->_htmlBody)) {
+                $body = new Horde_Mime_Part();
+                $body->setType('multipart/alternative');
+                $this->_body->setDescription(_("Plaintext Version of Message"));
+                $body->addPart($this->_body);
+                $this->_htmlBody->setDescription(_("HTML Version of Message"));
+                $body->addPart($this->_htmlBody);
+            } elseif (!empty($this->_htmlBody)) {
+                $body = $this->_htmlBody;
+            } elseif (!empty($this->_body)) {
+                $body = $this->_body;
             }
-            foreach ($this->_parts as $mime_part) {
-                $basepart->addPart($mime_part);
+            if (count($this->_parts)) {
+                $basepart = new Horde_Mime_Part();
+                $basepart->setType('multipart/mixed');
+                if ($body) {
+                    $basepart->addPart($body);
+                }
+                foreach ($this->_parts as $mime_part) {
+                    $basepart->addPart($mime_part);
+                }
+            } else {
+                $basepart = $body;
             }
-        } else {
-            $basepart = $body;
         }
 
         /* Check mailer configuration. */