diff --git a/Model/Mail/SmtpTransportZend2.php b/Model/Mail/SmtpTransportZend2.php index 66ebc3e43..e91505870 100644 --- a/Model/Mail/SmtpTransportZend2.php +++ b/Model/Mail/SmtpTransportZend2.php @@ -16,6 +16,11 @@ class SmtpTransportZend2 */ private $zendMailTransportSmtp2Factory; + /** + * Default encoding + */ + const ENCODING = 'utf-8'; + /** * @param Transactional $transactionalEmailSettings * @param ZendMailTransportSmtp2Factory $zendMailTransportSmtp2Factory @@ -29,13 +34,14 @@ public function __construct( } /** - * @param \Magento\Framework\Mail\Message $message + * @param \Zend\Mail\Message $message * @param int $storeId */ public function send($message, $storeId) { $smtpOptions = $this->transactionalEmailSettings->getSmtpOptions($storeId); $smtp = $this->zendMailTransportSmtp2Factory->create($smtpOptions); + $message->setEncoding(self::ENCODING); $smtp->send($message); } } diff --git a/Plugin/MessagePlugin.php b/Plugin/MessagePlugin.php index c50510e52..ea7fa15b3 100644 --- a/Plugin/MessagePlugin.php +++ b/Plugin/MessagePlugin.php @@ -7,6 +7,9 @@ use Magento\Email\Model\TemplateFactory; use Magento\Framework\Mail\MessageInterface; use Magento\Framework\Registry; +use Zend\Mime\Mime; +use Zend\Mime\Part; +use Dotdigitalgroup\Email\Model\Mail\SmtpTransportZend2; class MessagePlugin { @@ -51,11 +54,11 @@ public function __construct( /** * @param MessageInterface $message - * @param string $body + * @param mixed $body * * @return mixed */ - public function afterSetBody(MessageInterface $message, $body) + public function beforeSetBody(MessageInterface $message, $body) { $templateId = $this->isTemplate(); if ($templateId && $this->shouldIntercept()) { @@ -64,8 +67,37 @@ public function afterSetBody(MessageInterface $message, $body) $this->handleZendMailMessage($message); $this->setMessageFromAddressFromTemplate($message, $template); } + if (is_string($body) && ! $message instanceof \Zend_Mail) { + return [self::createMimeFromString($body)]; + } } - return $body; + return null; + } + + /** + * @param $string + * @return bool + */ + private function isHTML($string) + { + return $string != strip_tags($string); + } + + /** + * Create HTML mime message from the string. + * + * @param string $body + * @return \Zend\Mime\Message + */ + private function createMimeFromString($body) + { + $bodyPart = new Part($body); + $bodyPart->setEncoding(Mime::ENCODING_QUOTEDPRINTABLE); + $bodyPart->setCharset(SmtpTransportZend2::ENCODING); + ($this->isHTML($body)) ? $bodyPart->setType(Mime::TYPE_HTML) : $bodyPart->setType(Mime::TYPE_TEXT); + $mimeMessage = new \Zend\Mime\Message(); + $mimeMessage->addPart($bodyPart); + return $mimeMessage; } /**