Question

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>

Was it helpful?

Solution

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

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top