Skip to content

Automatically set From Email and Name before sending an Email from Magento

My current requirement is to use AWS SES to send emails from my Magento(2.4.7) project. Since AWS also requires the from email which is a verified identity on SES itself. When i tried to configure that in Stores->Configuration->Advanced->System->Mail Sending System it didn’t have From Email and Sender Name. So i created a Custom module with following system.xml to add those 2 properties

etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="system" translate="label" type="text" sortOrder="900" showInDefault="1" showInWebsite="1" showInStore="1">
            <group id="smtp" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="fromemail" translate="label comment" type="text" sortOrder="121" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>From Email</label>
                    <comment>From Email</comment>
                    <depends>
                        <field id="transport">smtp</field>
                    </depends>
                </field>
                <field id="sendername" translate="label comment" type="text" sortOrder="122" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Sender Name</label>
                    <comment>Sender Name</comment>
                    <depends>
                        <field id="transport">smtp</field>
                    </depends>
                </field>
            </group>
        </section>
    </system>
</config>

After this when sending email through following code it works fine and i am able to send email

private function sendContactAlert($post){
        $fromEmail = $this->scopeConfig->getValue('system/smtp/fromemail',ScopeInterface::SCOPE_STORE);
        $fromName = $this->scopeConfig->getValue('system/smtp/sendername',ScopeInterface::SCOPE_STORE);
        $from = ['email' => $fromEmail, 'name' => $fromName];
        $templateData = [
            'name' => $post['name'],
            'message' => $post['message'],
            'email' => $post['email'],
            'phone_no' => $post['phone_no']
        ];
        $this->transportBuilder
                ->setTemplateIdentifier('techneat_contact_email_template')
                ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => Store::DEFAULT_STORE_ID])
                ->setTemplateVars(['data' => new DataObject($templateData)])
                ->setFromByScope($from)
                ->addTo($this->contactsConfig->emailRecipient());
        $this->transportBuilder->getTransport()->sendMessage();
    }

But i dont want to load the From Email everytime from settings before sending emails. If i do that it makes the custom module that i built as a required module for whatever solutions i implement. Is there anyway to configure magento to use the above properties to load the values from settings and apply it before sending email in my custom module?

Btw i did try Mageplaza Smtp plugin and it didn’t even work and crashed Magento admin after installing via composer as recommended. Had to disable it

Thanks in advance