Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating HTML E-mails in Zend Framework 2 with Zend_Mail + Zend_Mime

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('[email protected]', 'User1');
$message->addFrom('[email protected]', '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>

like image 659
user521990 Avatar asked Dec 16 '22 18:12

user521990


2 Answers

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('[email protected]', 'Joe Schmo')
      ->addTo('[email protected]', '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
like image 150
user521990 Avatar answered Dec 18 '22 06:12

user521990


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

like image 34
Mohammad Mehdi Habibi Avatar answered Dec 18 '22 06:12

Mohammad Mehdi Habibi