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


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.



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

Could you provide a example to reproduce the issue?

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

Return-Path: example@example.org Date: Tue, 12 Jun 2012 19:42:56 +0200 From: example@example.org Subject: ZF2 Multipart Mail Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=_4ed23c87057835f27922e2478cd8eeac" To: example@example.org 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--

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


Thank you for the workaround!

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

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2460