Skip to content

Commit

Permalink
Merged PR 12965: set encoding 'utf-8' on Message
Browse files Browse the repository at this point in the history
**This is a workaround for encoding issue until Magento resolves bug**
magento/magento2#22065
magento/magento2#22103

**Change will effect Magento 2.2.8 and 2.3.+**
Because of ZF v2 usage in these versions setEncoding on message should fix encoding overall (setEncoding fixes encoding issue in subject) but because of ZF v2 bug have to fix encoding separately on body.
zendframework/zendframework#6841
zendframework/zendframework#5708
zendframework/zendframework#4917

**To test:**
- Test both email types text and html
- Test in Magento 2.3.*
- Test in Magento 2.2.8
- Test in Magento < 2.2.8
- Test using our Email Template mapped and our SMTP enabled
- Test using Magento template and our SMTP enabled
- Test using our SMTP disabled

Related work items: #86368
  • Loading branch information
adeelq committed Apr 16, 2019
1 parent a3ff553 commit 1b08554
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
8 changes: 7 additions & 1 deletion Model/Mail/SmtpTransportZend2.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class SmtpTransportZend2
*/
private $zendMailTransportSmtp2Factory;

/**
* Default encoding
*/
const ENCODING = 'utf-8';

/**
* @param Transactional $transactionalEmailSettings
* @param ZendMailTransportSmtp2Factory $zendMailTransportSmtp2Factory
Expand All @@ -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);
}
}
38 changes: 35 additions & 3 deletions Plugin/MessagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()) {
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 1b08554

Please sign in to comment.