Patch: Zend_Mime, Zend_Mime_Part, Zend_Mail

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Patch: Zend_Mime, Zend_Mime_Part, Zend_Mail

Simon Mundy
I've put together a patch for the problem encountered with Mail  
messages that have both Text and HTML content as well as attachments.

The changes I made were:-

Index: Zend/Mime.php
===================================================================
--- Zend/Mime.php       (revision 412)
+++ Zend/Mime.php       (working copy)
@@ -34,6 +34,7 @@
      const ENCODING_8BIT = '8bit';
      const ENCODING_QUOTEDPRINTABLE = 'quoted-printable';
      const ENCODING_BASE64 = 'base64';
+    const MULTIPART_ALTERNATIVE = 'multipart/alternative';
      const DISPOSITION_ATTACHMENT = 'attachment';
      const DISPOSITION_INLINE = 'inline';
      const LINELENGTH = 74;

...added a new constant MULTIPART_ALTERNATIVE to use when creating a  
new Mime_Part


Index: Zend/Mime/Part.php
===================================================================
--- Zend/Mime/Part.php  (revision 412)
+++ Zend/Mime/Part.php  (working copy)
@@ -40,6 +40,7 @@
      public $filename;
      public $description;
      public $charset;
+    public $boundary;
      protected $_content;
@@ -79,6 +80,10 @@
              $res.= '; charset="'.$this->charset.'"';
          }
+        if ($this->boundary) {
+            $res.= '; boundary="'.$this->boundary.'"';
+        }
+
          $res .= Zend_Mime::LINEEND
                . 'Content-Transfer-Encoding: ' . $this->encoding
                . Zend_Mime::LINEEND;

...added a new public property 'boundary' to allow parts to specify a  
boundary (as they currently do in the http://www.ietf.org/rfc/ 
rfc2045.txt specification)


Index: Zend/Mail.php
===================================================================
--- Zend/Mail.php       (revision 412)
+++ Zend/Mail.php       (working copy)
@@ -421,7 +421,48 @@
      {
          return $this->_from;
      }
+
+    /**
+     * Generate Mime Compliant Message from the current configuration
+     *
+     * Extends the parent class to ensure that multipart/alternative is
+     * added to the mail parts if both text and html are present in a
+     * message that contains an attachment.
+     *
+     * @return String
+     */
+    public function generateMessage()
+    {
+        if ($this->_hasAttachments && $this->_hasTextBody && $this-
 >_hasHtmlBody) {
+            $txt = '';
+            $parts = array_splice($this->_parts, 0, count($this-
 >_parts));
+            // Generate unique boundary for multipart/alternative
+            $mime = new Zend_Mime(null);
+            $boundaryLine = $mime->boundaryLine();
+
+            foreach($parts as $mp) {
+                // Include only known text types
+                if (in_array($mp->type, array(Zend_Mime::TYPE_TEXT,  
Zend_Mime::TYPE_HTML))) {
+                    $txt .= $boundaryLine
+                          . $mp->getHeaders()
+                          . Zend_Mime::LINEEND
+                          . $mp->getContent()
+                          . Zend_Mime::LINEEND;
+                } else {
+                    $this->addPart($mp);
+                }
+            }
+            $mp = new Zend_Mime_Part($txt . $boundaryLine);
+            $mp->type = Zend_Mime::MULTIPART_ALTERNATIVE;
+            $mp->boundary = $mime->boundary();
+
+            // Ensure first part contains text alternatives
+            array_unshift($this->_parts, $mp);
+        }
+        return parent::generateMessage();
+    }
+
      /**
       * Sends a Multipart eMail using the given Transport
       *

...the 'generateMessage' method overrides the parent method and  
performs a quick check to see if the parts need rewriting. If it  
does, it strips out the existing parts, creates a new 'multipart/
alternative' (with a new unique boundary) and then re-positions it to  
become the first part - I don't know if that affects Mail apps much,  
but a quick glance at the source of most of my inbox emails shows  
that it seems to be the standard way of doing things.

I have run the existing unit tests and it passes with flying colours.  
Do I need to pass this on to anyone in particular, or is someone able  
to review these changes and let me know if it requires further tweaking?

To be honest, it seems to be more of a patch and I think this mail  
module could do with a more robust way of handling nested content but  
I think this will do nicely for now.

Look forward to any feedback

--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com


Reply | Threaded
Open this post in threaded view
|

Re: Patch: Zend_Mime, Zend_Mime_Part, Zend_Mail

Mislav Marohnić
Simon,

the correct way to do this is to create a new ticket in ZF Trac and post your patch as an attachment.

On 5/16/06, Simon Mundy <[hidden email]> wrote:
I've put together a patch for the problem encountered with Mail
messages that have both Text and HTML content as well as attachments.

Reply | Threaded
Open this post in threaded view
|

Re: Patch: Zend_Mime, Zend_Mime_Part, Zend_Mail

Simon Mundy
Thank you Mislav - will do now

Cheers

Simon,

the correct way to do this is to create a new ticket in ZF Trac and post your patch as an attachment.

On 5/16/06, Simon Mundy <[hidden email]> wrote:
I've put together a patch for the problem encountered with Mail
messages that have both Text and HTML content as well as attachments.

--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124