4

Zend framework changed the Zend_Mail object so it no longer has the setBodyHtml() method to create HTML e-mails.

Does anyone know how to create an HTML e-mail with the ZF2 Mail component? So far I have tried:

$html = new \Zend\Mime\Part($htmlText);
$html->type = \Zend\Mime\Mime::TYPE_HTML;
$html->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE;
$html->encoding = \Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE;
$html->charset = 'iso-8859-1';

$body = new \Zend\Mime\Message();
$body->addPart($html);

$message = new \Zend\Mail\Message();
$message->setBody($body);
$message->addTo('myemail@com.com', 'User1');
$message->addFrom('myemail@com.com', 'User2');
$message->setSubject('Test');

The resulting email is:

MIME-Version: 1.0

Content-Type: text/html; charset=iso-8859-1

Content-Transfer-Encoding: quoted-printable

Content-Disposition: inline

From: XYZ

Report of last months log files=0A=09=09=09=0A=09=

=>09=09=0A=09=09=09Test=0A =09=09=09=0A=09=09=09

l>

Machavity
  • 30,841
  • 27
  • 92
  • 100
user521990
  • 669
  • 1
  • 5
  • 21

4 Answers4

9

It turns out there is a issue as I have found with the \Zend\Mail\Header component.

I am using a linux based machine to host my php machine which means each header line in an email should have "\r" ONLY appended to it. For Windows it is "\r\n".

The constant EOL in the \Zend\Mail\Header file is set at "\r\n" causing all the headers to appear in the e-mail if sent from a linux machine.

The best practice here is to use the PHP_EOL constant which automatically detects the platform and uses the CORRECT end of line code.

To fix this:

You need to update your \Zend\Mail\Header EOL constant to equal PHP_EOL. Also if using UTF-8 you must make sure it is the last thing that is called on your message. It must be done after the body is set.

I put in a ticket to ZF2 2.0 team to look at it

This is a working example:

    $html = '' //my html string here

    $m = new \Zend\Mail\Message();
    $m->addFrom('joe@me.com', 'Joe Schmo')
      ->addTo('ally@me.com', 'Ally Joe')
      ->setSubject('Test');

    $bodyPart = new \Zend\Mime\Message();

    $bodyMessage = new \Zend\Mime\Part($html);
    $bodyMessage->type = 'text/html';

    $bodyPart->setParts(array($bodyMessage));

    $m->setBody($bodyPart);
    $m->setEncoding('UTF-8');

             //send your message now
user521990
  • 669
  • 1
  • 5
  • 21
  • 1
    I looked for your ticket and I think this is it: https://github.com/zendframework/zf2/issues/2806 If so, it's been closed as "won't fix". The ZF folks say that this issue is caused by an improperly functioning mail transport on the server, and that using PHP_EOL will not fix the issue in all cases. If this is correct, you do *not* want to try patching ZF yourself with this fix. – Josh Mar 11 '13 at 11:20
  • this answer is WRONG! Do not use the PHP_EOL constant! – eXe Oct 08 '15 at 12:56
  • Why do you instantiate a Message type object in a ...part named variable and a Part type object in a ...message named variable ? It adds confusion. – Stephane Aug 24 '17 at 15:18
3
use Zend\Mail;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;

    $body = 'email body';
    $htmlPart = new MimePart($body);
    $htmlPart->type = "text/html";

    $textPart = new MimePart($body);
    $textPart->type = "text/plain";

    $body = new MimeMessage();
    $body->setParts(array($textPart, $htmlPart));

    $message = new Mail\Message();
    $message->setFrom($from);
    $message->addTo($to);
    $message->addReplyTo($reply);
    if ($cc)
        $message->addCc($cc);
    if ($bcc)
        $message->addBcc($bcc);
    $message->setSender($sender);
    $message->setSubject($subject);
    $message->setEncoding("UTF-8");
    $message->setBody($body);
    $message->getHeaders()->get('content-type')->setType('multipart/alternative');

    $transport = new Mail\Transport\Sendmail();
    $transport->send($message);

i hope that this help you

Mohammad Mehdi Habibi
  • 1,601
  • 1
  • 14
  • 30
0

I suggest reading this blog.

Basically you no longer have a setBodyHtml() you only have setBody(). This is due to the way modern E-Mail-Readers work. Every reader has the option to display both. Most E-Mails are sent in HTML nowadays. But you still are able to view an HTML E-Mail as PlainText, too, since the HTML Part simply get's 'translated' by the respective programs.

In ZF2, to support HTML E-Mail AND PlainText E-Mail you simply add both mime-types. And when PlainText is wanted, the E-Mail-Programm will handle this. At least this is how i understand the current thing in ZF2 and with mentioned blog.

Sam
  • 16,435
  • 6
  • 55
  • 89
  • I tried the tutorial part and used the code where the are creating the HTML e-mail, but my HTML e-mail still shows up as plain text even with the correct headers. Ideas? – user521990 Oct 17 '12 at 08:44
  • Didn't try it myself, sorry, just referenced and explained by my understanding. – Sam Oct 17 '12 at 09:16
  • 1
    sam - see my answer below. Turned out Zend_Mail component doesn't correctly create headers for Linux based machines. - At least that was my observation after looking at things in further details. – user521990 Oct 17 '12 at 19:09
0

See the following post: https://stackoverflow.com/a/34267509/5448467

WasabiMail is a mail module which is able to handle html-email templates in a convinient way.

Community
  • 1
  • 1