Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magento 2.3.3 - Invalid header value detected #152

Closed
srenon opened this issue Oct 10, 2019 · 29 comments
Closed

Magento 2.3.3 - Invalid header value detected #152

srenon opened this issue Oct 10, 2019 · 29 comments

Comments

@srenon
Copy link
Contributor

srenon commented Oct 10, 2019

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
Copy link
Contributor Author

srenon commented Oct 10, 2019

Will review shortly, but this seems like a core issue

See magento/magento2#24957

@elvinristi
Copy link

see magento/magento2#24902 which has fix in PR magento/magento2#24906

@elfeffe
Copy link

elfeffe commented Oct 31, 2019

@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
Copy link
Contributor Author

srenon commented Oct 31, 2019

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

What version of Magento are you using?

@elfeffe
Copy link

elfeffe commented Oct 31, 2019

@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
Copy link
Contributor Author

srenon commented Oct 31, 2019

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

@elfeffe
Copy link

elfeffe commented Oct 31, 2019 via email

@srenon
Copy link
Contributor Author

srenon commented Nov 1, 2019

@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
Copy link

elfeffe commented Nov 3, 2019

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

@srenon
Copy link
Contributor Author

srenon commented Nov 3, 2019

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

@frvoya
Copy link

frvoya commented Nov 3, 2019

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
Copy link

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
Copy link

elfeffe commented Nov 4, 2019

@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
Copy link

grll commented Nov 12, 2019

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

@elfeffe
Copy link

elfeffe commented Nov 12, 2019

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

@fkruidhof
Copy link

fkruidhof commented Nov 26, 2019

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:

$message = Message::fromString($message->toString());

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
Copy link

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.

@DanieliMi
Copy link

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?

avoelkl added a commit to e-conomix/magento2-gmail-smtp-app that referenced this issue Jan 17, 2020
@robsoned
Copy link

@avoelkl Are you making a PR to fix this?

@robsoned
Copy link

robsoned commented Jan 21, 2020

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
Copy link

lfglopes commented Jan 22, 2020

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
Copy link
Contributor

avoelkl commented Jan 24, 2020

@robsoned i created the PR here
#156

@avstudnitz
Copy link

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
Copy link
Contributor Author

srenon commented Feb 3, 2020

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

avoelkl added a commit to e-conomix/magento2-gmail-smtp-app that referenced this issue Feb 6, 2020
avoelkl added a commit to e-conomix/magento2-gmail-smtp-app that referenced this issue Feb 6, 2020
@codedge
Copy link

codedge commented Feb 7, 2020

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
Copy link
Contributor Author

srenon commented Feb 7, 2020

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

@srenon
Copy link
Contributor Author

srenon commented Mar 27, 2020

@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 fb78046#diff-abf392b7db6e57bed52c99468ffaa5f9R17

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

@rafaelstz
Copy link

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
Copy link

lfglopes commented Jun 15, 2020

@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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests