How To

How to fix Wrong Sales Order Email Sender Issue in Magento 2

Order is a key part of any Ecommerce store, once an order is placed & gets confirmed store owner delivers the product at the customer’s doorsteps. But because every store doesn’t have the same order process that’s the reason why Magento 2.x comes with a preloaded option for customizing order email templates that will constantly serve updates to your customers on each order status change. Not, only this, you can also set email sender if required.
But if you are using an older version of Magento and switched to latest Magento 2.2.x you will face an issue “Confirmation emails have no FROM or FROM email address 2.2.4 #14952” also this request was officially pulled in Magento 3 Github by @tstamplis. It means even if you set up your own sales order email sender, Magento will send an email using default server sender i.e.apache@domainname but other emails except sales are working perfectly fine. Because we have seen lots of people are facing the same issue we have decided to write a blog about.
Firstly, we have to create custom extension for this purpose and after that create “module.xml
file at this path inside your extension folder.
app\code\Vendor\Extension\etc\module.xml

<pre class="lang:default decode:true">
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Vendor_Extension" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>
</pre>

Now, create one more file “di.xml” in same extension folder.
app\code\Vendor\Extension\etc\ di.xml

<pre class="lang:default decode:true">
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="Vendor\Extension\Rewrite\Sales\Model\Order\Email\SenderBuilder" />
    <preference for="Magento\Framework\Mail\Template\TransportBuilder" type="Vendor\Extension\Rewrite\Framework\Mail\Template\TransportBuilder" />
</config>
</pre>

Now in this third step create “TransportBuilder.php” file at this path using given code.
app\code\Vendor\Extension\Rewrite\Framework\Mail\Template\TransportBuilder.php

<pre class="lang:default decode:true">
<?php

namespace Vendor\Extension\Rewrite\Framework\Mail\Template;
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{ 
    /**
     * @param $from
     * @param $store
     * @return $this
     * @throws \Magento\Framework\Exception\MailException
     */    public function setFromByStore($from, $store)
    {
        $result = $this->_senderResolver->resolve($from, $store);
        $this->message->setFrom($result['email'], $result['name']);
        return $this;
    }
}
</pre>

Now, in fourth and last step create SenderBuilder.php at this path.

app\code\Vendor\Extension\Rewrite\Sales\Model\Order\Email\SenderBuilder.php

<pre class="lang:default decode:true">
<?php

namespace Vendor\Extension\Rewrite\Sales\Model\Order\Email;
class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    /**
     * Configure email template
     *
     * @return void
     */    protected function configureEmailTemplate()
    {
        $this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());
        $this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions());
        $this->transportBuilder->setTemplateVars($this->templateContainer->getTemplateVars());      
        $this->transportBuilder->setFromByStore(
            $this->identityContainer->getEmailIdentity(),
            $this->identityContainer->getStore()->getId()
        );        
    }
}
</pre>

That’s it! After creating custom extension using above code, your Magento 2 will start sending sales order email to your customers from define email sender.

Lastly, If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.

And, Let us know if you are facing an issue while implementing this code.

Happy Fixing!

Click to rate this post!
[Total: 17 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago