magepal / magento2-gmail-smtp-app

Configure Magento 2 to send email using Google App, Gmail, Amazon Simple Email Service (SES), Microsoft Office365 and many other SMTP (Simple Mail Transfer Protocol) servers
https://www.magepal.com
319 stars 148 forks source link

Magento 2.3.3 - Invalid header value detected #152

Closed srenon closed 4 years ago

srenon commented 4 years ago

Magento version #:

2.3.3

Edition (EE, CE, OS, etc):

CE

Expected behavior:

Actual behavior:

Steps to reproduce:

Preconditions

SMTP Configuration version 2.6.6 - it's not possible to send emails to receipients containing special characters in their names.

main.CRITICAL: Invalid header value detected {"exception":"[object] (Zend\Mail\Header\Exception\RuntimeException(code: 0): Invalid header value detected at /var/www/magento2/vendor/zendframework/zend-mail/src/Header/HeaderValue.php:112)"} []

srenon commented 4 years ago

Will review shortly, but this seems like a core issue

See https://github.com/magento/magento2/issues/24957

elvinristi commented 4 years ago

see https://github.com/magento/magento2/issues/24902 which has fix in PR https://github.com/magento/magento2/pull/24906

elfeffe commented 4 years ago

@elvinristi No, I have applied the changes here https://github.com/magento/magento2/pull/24906/files#diff-dfb3ee5a7f863458afea75f5524cd2e5R398

And I still have that Invalid Header error (and email not being sent) even with normal gmail addresses with normal characters.

srenon commented 4 years ago

@elfeffe ... what version of our extension are you using 2.6.8?

What version of Magento are you using?

elfeffe commented 4 years ago

@srenon Latest just upgraded today, in both cases. 2.6.8 and 2.3.3 I even applied the patch https://community.magento.com/t5/Magento-DevBlog/Backward-incompatible-Changes-in-the-Mail-Library-for-Magento-2/ba-p/144787

I applied the patch after I pened the issue here.

Nothing, it always give the same error.

srenon commented 4 years ago

which SMTP are you using (gmail, aws, etc)?

elfeffe commented 4 years ago

Amazon SES and Mailgun, both do the same thing

srenon commented 4 years ago

@elfeffe ... I have zero issues on SES with Magento 2.3.3 and PHP 7.3, where are you seeing this issue so that I can check?

elfeffe commented 4 years ago

I just noted that only happens with some addresses, very strange. Because is happening with a very normal address.

srenon commented 4 years ago

Does the email/name contain special characters? Can you share a sample email address for me to test with?

frvoya commented 4 years ago

Hello, i have the same issue with Gmail. 2.6.8 and 2.3.3 with the patch https://community.magento.com/t5/Magento-DevBlog/Backward-incompatible-Changes-in-the-Mail-Library-for-Magento-2/ba-p/144787 Same error and I have Invalid Header error (and email not being sent)

elvinristi commented 4 years ago

see also issue with Content-Disposition being as Content-Disposition: =?utf-8?Q?inline?= while must be Content-Disposition: inline

two possible hotfixes at the moment:

elfeffe commented 4 years ago

@srenon The name of the customer has special characters, not the address. it is an "é". I have tried every single method to fix it, but I still get the beautiful Invalid headers error.

grll commented 4 years ago

@elfeffe I am experimenting the exact same issue. Did you find any solution / hotfix yet?

elfeffe commented 4 years ago

@grll Not yet :( I'm having problems with special characters now, but that's Magento.

fkruidhof commented 4 years ago

Using Magento 2.3.3 and gmail-smtp-app 2.6.8. I patched Magento with the "Backward-incompatible Changes in the Mail Library" patch and those mentioned by elvinristi, but still no luck sending mails with specials characters in the "from" header.

problem seems to be in your module: https://github.com/magepal/magento2-gmail-smtp-app/blob/d247c7452e8c7dc64b73020d21ad67b44451369b/Model/ZendMailTwo/Smtp.php#L88

fromString() seems to convert all headers back to ASCII encoding.

Removing this line worked for me and i'm trying to patch that in via composer. I do not know the effect on Amazon SES, but my customer is not using that.

Since the comment mentions that that line is a temp fix, maybe it could be removed?

JeroenVanLeusden commented 4 years ago

I've added $message->getHeaders()->setEncoding('utf-8'); again which is removed in https://github.com/magepal/magento2-gmail-smtp-app/commit/52e2136585bd72d69e83c0c44fa37c9107905b16. Works fine so far but we're not using Amazon SES.

DanieliMi commented 4 years ago

I've added $message->getHeaders()->setEncoding('utf-8'); again which is removed in 52e2136. Works fine so far but we're not using Amazon SES.

Instead of setting the encoding fixed to utf-8 the following patch can be used:

--- vendor/magepal/magento2-gmailsmtpapp/Model/TwoDotThree/Smtp.php
+++ vendor/magepal/magento2-gmailsmtpapp/Model/TwoDotThree/Smtp.php
@@ -78,7 +78,9 @@
         $dataHelper = $this->dataHelper;
         $dataHelper->setStoreId($this->storeModel->getStoreId());

+        $encoding = $message->getEncoding();
         $message = Message::fromString($message->getRawMessage());
+        $message->setEncoding($encoding);

         //Set reply-to path
         switch ($dataHelper->getConfigSetReturnPath()) {

@srenon what's the status of the issue? Is a solution/fix in sight?

robsoned commented 4 years ago

@avoelkl Are you making a PR to fix this?

robsoned commented 4 years ago

In my case here the error was because the customer name had 'ô'. the same thing as @elfeffe has described. Did you find any fix to this? The only thing we are doing now is to replace the special chars of the customers until we find a solution for this.

lfglopes commented 4 years ago

Based on this thread and the one on the magento2 repo I assembled this patch

https://gist.github.com/lfglopes/a4d4eed2e8c0432fd8e60f0540a401fb

it fixed it for me :) [no backport needed]

avoelkl commented 4 years ago

@robsoned i created the PR here https://github.com/magepal/magento2-gmail-smtp-app/pull/156

avstudnitz commented 4 years ago

We also had problems with Umlauts in recipient names. The PR didn't help for me. The following patch did:

diff --git a/magento2-gmailsmtpapp/Model/TwoDotThree/Smtp.php b/magento2-gmailsmtpapp/Model/TwoDotThree/Smtp.php
index 198da24..197f6ea 100644
--- a/magento2-gmailsmtpapp/Model/TwoDotThree/Smtp.php
+++ b/magento2-gmailsmtpapp/Model/TwoDotThree/Smtp.php
@@ -79,6 +79,7 @@ class Smtp
         $dataHelper->setStoreId($this->storeModel->getStoreId());

         $message = Message::fromString($message->getRawMessage());
+        $message->getHeaders()->get('to')->setEncoding('utf-8');

         //Set reply-to path
         switch ($dataHelper->getConfigSetReturnPath()) {
diff --git a/magento2-gmailsmtpapp/Model/ZendMailTwo/Smtp.php b/magento2-gmailsmtpapp/Model/ZendMailTwo/Smtp.php
index 8827e50..352816d 100644
--- a/magento2-gmailsmtpapp/Model/ZendMailTwo/Smtp.php
+++ b/magento2-gmailsmtpapp/Model/ZendMailTwo/Smtp.php
@@ -173,6 +173,7 @@ class Smtp
         try {
             $transport = new SmtpTransport();
             $transport->setOptions($options);
+            $message->getHeaders()->get('to')->setEncoding('utf-8');
             $transport->send($message);
         } catch (Exception $e) {
             throw new MailException(
srenon commented 4 years ago

@avoelkl @avstudnitz ... sorry for the delay I will review and accept this shortly

codedge commented 4 years ago

Any timeline here? Would be nice if this is going to be merged in the next 1-2 days. It's just that the email sending does not work with this bug(?) 😒

srenon commented 4 years ago

@codedge ... done... please update to 2.7.0.

srenon commented 4 years ago

@avoelkl, @avstudnitz @robsoned @lfglopes @DanieliMi @elfeffe I rewrite the way we are setting headers because most of the changes listed above only fix it for a particular version of Magento or language and cause issues in other environments. see https://github.com/magepal/magento2-gmail-smtp-app/commit/fb78046126238e13e0112936e6be2178926ecb5d#diff-abf392b7db6e57bed52c99468ffaa5f9R17

Please download 2.8.0 and let me know if you are still having issues

rafaelstz commented 4 years ago

This solution worked for me 🎉 ! 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>

Reference: https://magento.stackexchange.com/a/306638/16416

lfglopes commented 4 years ago

@srenon

Using magento 2.3.3 and version 2.8.1 of this plugin didn't fix it for me.

edit: the solution above works like a charm though. thanks @rafaelstz !