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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

9 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago