zend-mail icon indicating copy to clipboard operation
zend-mail copied to clipboard

Port back setBodyHtml() and setBodyText()

Open djozsef opened this issue 9 years ago • 1 comments

I get the concept of removing the mentioned methods in ZF2 derives from the fact that a message may have several MIME parts, not only HTML and plain text. But still, most of the time nowadays we primarily compose HTML messages, secondarily with text/plain fallback. With the current concept we have to roll over and over something like this:

use Zend\Mail;
use Zend\Mime;

[...]

$body = new Mime\Message();     

$bodyHtml = new Mime\Part($view->render("html-template.phtml", $viewData));
$bodyHtml->setEncoding(Mime\Mime::ENCODING_QUOTEDPRINTABLE);
$bodyHtml->setType(\Zend\Mime\Mime::TYPE_HTML);
$bodyHtml->setCharset("UTF-8");     

$bodyText = new Mime\Part($view->render("html-template.phtml", $viewData));
$bodyText->setEncoding(Mime\Mime::ENCODING_8BIT);
$bodyText->setType(\Zend\Mime\Mime::TYPE_TEXT);
$bodyText->setCharset("UTF-8");

$body->addPart($bodyText);
$body->addPart($bodyHtml);

$message = new Mail\Message();
$message->setEncoding("UTF-8");
$message->setBody($body);
$message->setFrom($senderEmail, $senderName);
$message->addTo($recipientEmail, $recipientName);
$message->setSubject($subject);

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

Compared to ZF1 this is way too tedious. I am sure we could find a golden midway between keeping the current clean concept and being able to compose common messages easily. What I am thinking of is a Zend\Mail\Message::setBodyHtml() and Zend\Mail\Message::setBodyText() backport as convenience methods that would add the appropriate MIME parts pushing it to the end of the parts' stack. In case the message had already have a non-multipart body, the method would make the message multipart pushing the existing body as the first part.

I am posting this to ask for opinions and your thought on this. I am doing it anyways but if the community accepts the idea I will code it in a mergeable way.

Thoughts out there?

djozsef avatar Feb 13 '16 00:02 djozsef

This repository has been closed and moved to laminas/laminas-mail; a new issue has been opened at https://github.com/laminas/laminas-mail/issues/65.

michalbundyra avatar Jan 15 '20 19:01 michalbundyra