Automatically Setting "From Email" and "Sender Name" in Magento Using AWS SES

Table of Contents

  1. Introduction
  2. Configuring AWS SES with Magento
  3. Creating a Custom Magento Module to Manage Email Settings
  4. Troubleshooting Common Issues
  5. Conclusion
  6. FAQ

Introduction

Imagine running an e-commerce site where every email sent enhances your brand and builds deeper customer connections. Now, imagine the tedious task of manually setting the "From Email" and "Sender Name" for every email you send. Sounds like a chore, right? When dealing with Magento 2.4.7 and Amazon Web Services Simple Email Service (AWS SES), this task can either spiral into an administrative nightmare or become a seamless automated process with the right configuration. Today, we delve into how you can efficiently configure these settings in Magento, eliminating manual repetition and ensuring your emails always carry the right branding.

In this post, you'll learn how to automatically set the "From Email" and "Sender Name" before sending emails from your Magento store using AWS SES. We'll guide you through creating a custom Magento module, discuss why native modules might not work as intended, and how to effectively tackle common pitfalls. By the end, you'll have a streamlined email-sending framework integrated with AWS SES, ensuring consistent communication with your customers.

Configuring AWS SES with Magento

Understanding the Requirements

For integrating AWS SES with Magento, you need to verify your email identity in AWS. This ensures that AWS SES only sends emails from domains or addresses you control, preventing abuse of its services. The twist comes when Magento does not natively provide fields for "From Email" and "Sender Name" in its settings, necessitating a custom solution.

Why Default Solutions Often Fall Short

You might wonder why not use readily available solutions like the Mageplaza SMTP plugin. Well, as experienced, the Mageplaza SMTP module can sometimes fail, causing unexpected crashes and rendering the Magento admin interface unusable. Custom solutions provide more stability and flexibility, essential for maintaining seamless operations.

Creating a Custom Magento Module to Manage Email Settings

Setting up the Module

To begin with, you need to create a custom module that adds "From Email" and "Sender Name" fields in the Magento admin configuration.

  1. Create the Module Directory Structure:

    app/code/YourNamespace/YourModule
    

    Within this directory, create the following subdirectories:

    • etc
    • etc/adminhtml
  2. Defining the Module: Create registration.php:

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'YourNamespace_YourModule',
        __DIR__
    );
    

    Also, create module.xml:

    <?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="YourNamespace_YourModule" setup_version="1.0.0"/>
    </config>
    
  3. Adding Email Settings Fields: Create system.xml within etc/adminhtml:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/system_file.xsd">
        <system>
            <section id="trans_email">
                <group id="ident_general" translate="label">
                    <field id="name" translate="label" type="text">
                        <label>Sender Name</label>
                    </field>
                    <field id="email" translate="label" type="text">
                        <label>Sender Email</label>
                    </field>
                </group>
            </section>
        </system>
    </config>
    

Ensuring These Settings Are Utilized

To make sure the settings are applied during email sending, you have to extend Magento's email functionality.

  1. Creating an Override: Create a new class in your module that will extend the core Magento email sending class:

    Create Email.php:

    <?php
    namespace YourNamespace\YourModule\Model;
    
    use Magento\Framework\Mail\Template\TransportBuilder as OriginalTransportBuilder;
    
    class Email
    {
        protected $transportBuilder;
        protected $scopeConfig;
    
        public function __construct(
            OriginalTransportBuilder $transportBuilder,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
        ) {
            $this->transportBuilder = $transportBuilder;
            $this->scopeConfig = $scopeConfig;
        }
    
        public function sendEmail($templateId, $receiverInfo, $templateVars)
        {
            $this->transportBuilder->setTemplateIdentifier($templateId)
                ->setTemplateOptions([
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID
                ])
                ->setTemplateVars($templateVars)
                ->setFromByScope([
                    'name' => $this->scopeConfig->getValue('trans_email/ident_general/name'),
                    'email' => $this->scopeConfig->getValue('trans_email/ident_general/email')
                ])
                ->addTo($receiverInfo['email'], $receiverInfo['name'])
                ->getTransport()
                ->sendMessage();
        }
    }
    
  2. Utilizing the New Function in Your Custom Module:

    When you need to send an email, use the sendEmail method from the above class.

    Here is an example usage:

    $emailSender = $objectManager->create('YourNamespace\YourModule\Model\Email');
    $emailSender->sendEmail('your_email_template_id', $receiverInfo, $templateVariables);
    

Troubleshooting Common Issues

Mageplaza SMTP Failures

As noted, plugins like Mageplaza SMTP may cause unexplained crashes. Custom modules reduce such risks by tailoring the solution closely to your needs, ensuring tighter integration and less reliance on generalized third-party solutions.

Ensuring Values Load Automatically

When repeatedly loading settings manually, it increases chances of human error and inefficiency. By configuring your custom module to automatically use the stored configuration values, you sidestep these pitfalls, achieving a robust solution.

Conclusion

Incorporating AWS SES with Magento doesn't have to be a cumbersome task. By creating a custom module, you can seamlessly automate the setting of "From Email" and "Sender Name," ensuring every email sent from your Magento store is properly branded and managed. Rejecting unreliable plugins and embracing tailored solutions ensures your e-commerce operations remain smooth, reliable, and professional.

FAQ

1. Why not use a plugin for sending emails from Magento?

Plugins like Mageplaza SMTP can sometimes lead to system crashes and instability. Custom modules provide more control and reliability, ensuring smooth integration tailored to your needs.

2. How can I verify my email identity in AWS SES?

You need to log in to your AWS account, go to the SES section, and add and verify your email address or domain as a verified identity. AWS will send a confirmation email to complete the verification.

3. What if I need to change the "From Email" and "Sender Name" frequently?

With a custom module, you can easily update these settings in the Magento admin panel without rewriting or adjusting the backend code. This flexibility helps maintain operational efficiency.

4. Can I use this module for other SMTP services?

Yes, you can adapt the module to work with any SMTP service by adjusting the credentials and server settings accordingly in your configuration.

By establishing these practices, your Magento store will maintain a consistent and professional approach to email communication, directly benefiting your brand's reliability and customer trust.