ZF2-196: Multipart type respected in Zend\Mail\Message::setBody
Description
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.
Thanks!
Comments
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: 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--
Posted by Maks 3w (maks3w) on 2012-06-12T19:25:14.000+0000
As workaround you can put the following after $mail->setBody($message);
$mail->headers()->get('content-type')->setType('multipart/alternative')
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: https://github.com/zendframework/zf2/issues/2460