ZF2-196: Multipart type respected in Zend\Mail\Message::setBody

Issue Type: Improvement Created: 2012-03-07T10:41:49.000+0000 Last Updated: 2012-10-08T20:15:25.000+0000 Status: Closed Fix version(s): Reporter: Piotrek RybaƂtowski (piotrekr) Assignee: Maks 3w (maks3w) Tags: - Zend\Mail

  • mail

Related issues: Attachments: - zf2-multipart-mail.php


When calling Zend\Mail\Message::setBody providing a Mime Message object with multipart, in line 414 any multipart settings in Mime Message are ignored and multipart/mixed is put in statically. I guess multipart/alternative would be used more often to not display HTML and plain text version at one time, but the most ideal situation would be to have it set as an option.



Posted by Steffen Weber (steffen) on 2012-06-10T08:35:29.000+0000

I have run into the same issue. It is currently impossible to send an email that contains HTML and a text fallback.

Posted by Maks 3w (maks3w) on 2012-06-12T16:16:11.000+0000

Could you provide a example to reproduce the issue?

Posted by Steffen Weber (steffen) on 2012-06-12T17:45:09.000+0000

The attached demo script creates the following output with "Content-Type: multipart/mixed" although "Content-Type: multipart/alternative" would be desired:

Return-Path: Date: Tue, 12 Jun 2012 19:42:56 +0200 From: Subject: ZF2 Multipart Mail Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=_4ed23c87057835f27922e2478cd8eeac" To: Sender: Cc:

This is a message in Mime Format. If you see this, your mail reader does not support this format.

--=_4ed23c87057835f27922e2478cd8eeac Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Plain Text --=_4ed23c87057835f27922e2478cd8eeac Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

HTML --=_4ed23c87057835f27922e2478cd8eeac--

Posted by Maks 3w (maks3w) on 2012-06-12T19:25:14.000+0000

As workaround you can put the following after $mail->setBody($message);


Posted by Steffen Weber (steffen) on 2012-06-12T20:49:49.000+0000

Thank you for the workaround!

Posted by Steve Talbot (stevetalbot) on 2012-09-06T09:11:43.000+0000

Another workaround that I'm using successfully is to embed a multipart/alternative object inside a multipart/mixed message. This allows you to have two alternative message bodies (HTML and plain text) and add attachments. Hope this helps someone:

use Zend\Mail\Message as EmailMessage; use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart;

// HTML message $htmlPart = new MimePart($bodyHtml); $htmlPart->type = 'text/html';

// Plain text message $textPart = new MimePart($bodyText); $textPart->type = 'text/plain';

// Assemble them into a multipart/alternative object $alternatives = new MimeMessage(); $alternatives->setParts(array($textPart, $htmlPart)); $alternativesPart = new MimePart($alternatives->generateMessage()); $alternativesPart->type = "multipart/alternative;\n boundary=\"".$alternatives->getMime()->boundary()."\"";

// Now add the multipart/alternative object to a multipart/mixed object $body = new MimeMessage(); $body->addPart($alternativesPart);

// Add other parts (attachments) here

// Create the email message $email = new EmailMessage(); $email->setBody($body);

// Then set your to address, from address and send it

Posted by Ralph Schindler (ralph) on 2012-10-08T20:15:25.000+0000

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit:

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.