-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
"From Header set twice" when sending an email #7739
Comments
Any update on this. |
Same issue here. Trying to send two emails. One for order confirmation and the other is for a specific type of product from the same order. I get this error: Magento CE 2.1.5 |
@toshz if you need to fix in your custom module then you can read this blog for the solution. |
@RishabhRkRai, thank you for your report. |
Had the same issue sending only 1 mail after a new customer registered. |
I found out how to reproduce this. The main cause is that a singleton is being used in the following files:
There is a reset function that is called on the TransportBuilder but a simple mistake is made here. This fix works for me at least. |
Hi @stephan-cream as I said before - this issue duplicating #14952. I just updated labels and I'll keet this issue open |
Duplicate of #14952 |
I have the same problem but the solution from @stephan-cream did not work for me. To make magento send my invoice mails again, until the official fix is published, I changed vendor\magento\framework\Mail\Template\TransportBuilderByStore public function setFromByStore($from, $store)
{
$result = $this->senderResolver->resolve($from, $store);
$this->message->setFrom($result['email'], $result['name']);
return $this;
} to public function setFromByStore($from, $store)
{
$result = $this->senderResolver->resolve($from, $store);
if ($this->message->getFrom()) {
$this->message->clearFrom();
}
$this->message->setFrom($result['email'], $result['name']);
return $this;
} |
It is easier to define the injected transport object as non singleton. You can do this by defining your injected object in di.xml and setting the attribute 'shared' to 'false'. For instance, I created a script where I needed to send multiple order mails. Adding the following resolve my problem:
|
@LuckyLoek unfortunately it will not be enough because they both contains "message" object that should be shared. |
Fixed in 2.2-develop |
@gwharton: just ran into this problem on Magento 2.2.5 shop (without amazon modules), and can not confirm that 9a7c9e5 fixes the "From Header set twice" problem. In fact, it makes the email system even more unstable, by causing the From email address to be incorrect. By then applying your fix #18472 as well on top of this, we get a much more stable email system, no more "From Header set twice" problems, also no incorrect From email addresses and async sending now marks emails as being sent. Just wanted to add this extra info in here in case anybody else ran against this. Big thanks for the fix! |
Hi @RishabhRkRai. Thank you for your report.
The fix will be available with the upcoming 2.3.1 release. |
@magento-engcom-team give me 2.2.7-develop instance |
Hi @stephan-cream. Thank you for your request. I'm working on Magento 2.2.7-develop instance for you |
@stephan-cream I think you will find this is still broken in 2.2.7. I'm hoping that #18472 will be merged soon in time for 2.2.8 which resolves this issue. |
I know, hence i was trying to get a clean install to make a screencapture. |
Yeah, If you apply #18472, it gets rid of the use of TransportBuilderByStore altogether. |
Yes. Im also confused. The source code for 2.2.7 on github does NOT have 18472 merged. I just checked. |
@gwharton, that is indeed very strange. I also went back to browse the files in the 2.2.7 Github release, and I can see that the changes weren't applied like you said. Yet they are in the 2.2.7 version I installed via composer. At any rate, after verifying that those changes are a part of my code, I still get the "From Header set twice" error, so it looks like I'll have to dig into Magento core some more to see what's up. Like others, this bug seems to occur for me when I try to send more than one email in the same process. EDIT: Some digging reveals that the issue I'm having might be due to a bug in my own module's code. That being said, @skymeissner's solution (#7739 (comment)) seemed to make my mail system more robust. Whereas the bug in my code prevented all emails in the process from being sent out, applying his fix allowed at least the emails not related to my own code to go out. Thought that might be worth mentioning, as it may be worth rolling it into the official Magento core. For now, I'm just going to maintain my own patch for it that I'll apply whenever I update via composer. |
Just be aware that when you get the error saying From set twice, this means that the $message object that Magento is using to formulate an email has already been used for a previous email and still has content in it. In the #7739 fix, the from address is cleared before being reset, but what about the other fields. Sure the body and subject will be rewritten as the new email is formed, but what if for example the previous user of the $message object had a Bcc or cc set. Will this be cleared also? Could you inadvertently release sensitive info. This may be the preferred behaviour when the second email is a copy of the first, for example to admin, but what if the second email is unrelated to the first. There is some logic inside the Transport Builder class in the getTransport and reset functions where the "reusing" of message objects happens. Would be worth checking that out and making sure its doing exactly what you think it is doing. |
FYI This issue was fixed in 2.2-develop branch and planned to be included to 2.2.9 release |
@gwharton Is there anyway to approach the problem in another way implementation while sending multiple emails because seem message still used |
Someone from Magento will need to pick this up and look at what needs to be done moving forward. |
Preconditions
Steps to reproduce
Expected result
Actual result
main.CRITICAL: exception 'Zend_Mail_Exception' with message 'From Header set twice' in /../../../../vendor/magento/zendframework1/library/Zend/Mail.php:680
The text was updated successfully, but these errors were encountered: