2

I am showing these errors in my system log while requesting a password reset, and the customer don't receive the email.

[2020-01-09 05:16:28] main.CRITICAL: Exception message: Invalid header value detected
Trace: <pre>#1 Zend\Mail\Header\AbstractAddressList->getFieldValue(true) called at [vendor/zendframework/zend-mail/src/Header/AbstractAddressList.php:209]
#2 Zend\Mail\Header\AbstractAddressList->toString() called at [vendor/zendframework/zend-mail/src/Headers.php:426]
#3 Zend\Mail\Headers->toString() called at [vendor/zendframework/zend-mail/src/Message.php:546]
#4 Zend\Mail\Message->toString() called at [vendor/magento/framework/Mail/EmailMessage.php:217]
#5 Magento\Framework\Mail\EmailMessage->toString() called at [vendor/magento/framework/Mail/EmailMessage.php:209]

3 Answers 3

7

Yes, it is a bug with encoding ...

In the EmailMessage class (vendor/magento/framework/Mail/EmailMessage.php) the encoding can be set if passed in an argument in the constructor, but it is null by default.

if ($encoding) {
   $this->message->setEncoding($encoding);
}

So, I think the best practice use type in di.xml in your module without rewrite and plugins

<type name="Magento\Framework\Mail\EmailMessage">
   <arguments>
       <argument name="encoding" xsi:type="string">utf-8</argument>
   </arguments>
</type>
0
6

This is a bug of Magento version 2.3.3. It does not allow the use of special characters such as ã, ơ in-store email address or sender mail name or etc...

This is a changed path in Magento

vendor/magento/framework/Mail/EmailMessage.php

Please replace function with this code in above file :

public function toString(): string {
    $this->message->setEncoding('utf-8');
    return $this->message->toString();
}
3
  • Thank you, works like a charm in Magento 2.3.3. Commented Sep 26, 2020 at 7:54
  • It seems that in magento 2.4 this changed, do you know what the solution for 2.4 looks like? Commented Jun 21, 2021 at 1:45
  • Yeah! also worked for me. EmailMessage.php file, line 215 in my case. Thanks all. Commented Aug 4, 2021 at 15:23
0

Here is the class in case some one wonders what is going on:

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    /**
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\MailException
     */
    public function send()
    {
        $this->configureEmailTemplate();
        //Removed Customer name because of diacritics
        $this->transportBuilder->addTo(
            $this->identityContainer->getCustomerEmail()
        );

        $copyTo = $this->identityContainer->getEmailCopyTo();
        if (!empty($copyTo) && $this->identityContainer->getCopyMethod() == 'bcc') {
            foreach ($copyTo as $email) {
                $this->transportBuilder->addBcc($email);
            }
        }

        $transport = $this->transportBuilder->getTransport();
        $transport->sendMessage();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.