Site icon MageComp Blog

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

How to fix wrong sales order email sender issue in Magento 2 1

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!

Exit mobile version