In ZF2 (2.2), there is no easy way to combine a multi-page / alternative body (html with alternative text for clients who cannot / do not want to use html) with attachments. If you add a header like "multipart / alternative" for the whole message, the attachment (link) will not be displayed on some mail clients.
The solution is to split the message into two parts: body (text and html) and attachment:
http://jw-dev.blogspot.com.es/2013/01/zf2-zend-mail-multipartalternative-and.html
example:
$content = new MimeMessage(); $htmlPart = new MimePart("<html><body><p>Sorry,</p><p>I'm going to be late today!</p></body></html>"); $htmlPart->type = 'text/html'; $textPart = new MimePart("Sorry, I'm going to be late today!"); $textPart->type = 'text/plain'; $content->setParts(array($textPart, $htmlPart)); $contentPart = new MimePart($content->generateMessage()); $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"'; $attachment = new MimePart(fopen('/path/to/test.pdf', 'r')); $attachment->type = 'application/pdf'; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $body = new MimeMessage(); $body->setParts(array($contentPart, $attachment)); $message = new Message(); $message->setEncoding('utf-8') ->addTo(' mywife@home.com ') ->addFrom(' myself@office.com ') ->setSubject('will be late') ->setBody($body); $transport = new SmtpTransport(); $options = new SmtpOptions($transportConfig), )); $transport->setOptions($options); $transport->send($message);
For the above, you will need the following usage statements:
use Zend\Mail\Message; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions; use Zend\Mime\Mime; use Zend\Mime\Part as MimePart; use Zend\Mime\Message as MimeMessage;
ZF1 had a _buildBody() method in Zend_Mail_Transport_Abstract that did this automatically.
source share