Write Review

How To Send Email In Magento 2?

Keeping a healthy working relationship between stores owners and customer essential in business. The only way to build this relationship is by maintaining contact and communication with customers. Emails allow us to do that.

This article will help you send an email in Magento 2.

Let’s begin!

Step 1: Declaring Config Module

The first step is to create config.xml. You can create a field to load the email template in:


<?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">
        <tab id="bss" translate="label" sortOrder="300">           
        <section id="email" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>HostAdvice Email Demo</label>
            <group id="demo" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Please Choose Your Email Template</label>
                <field id="template" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Email Template</label>
                    <comment>Email template is chosen based on theme fallback when "Default" option is selected.</comment>

Step 2: Declaration of Email Template

The next step is to create email_templates.xml file in:


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
<template id="email_demo_template" label="This is email demo" file="email_demo_template.html" type="html" module="HostAdvice_EmailDemo" area="frontend"/>

In this case, check the following details:

  • Template ID: The template ID created in the first step.
  • Label: Indicates the label that will appear on the drop-down menu of your email.
  • file: The template file of your email.
  • area: Location of your email in the module files.
  • type: email type in your module.

Step 3: Creating a Helper Email

Now create a helper email in:


namespace HostAdvice\EmailDemo\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\Template\TransportBuilder;

class Email extends \Magento\Framework\App\Helper\AbstractHelper
    protected $inlineTranslation;
    protected $escaper;
    protected $transportBuilder;
    protected $logger;

    public function __construct(
        Context $context,
        StateInterface $inlineTranslation,
        Escaper $escaper,
        TransportBuilder $transportBuilder
    ) {
        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->logger = $context->getLogger();

    public function sendEmail()
        try {
            $sender = [
                'name' => $this->escaper->escapeHtml('Test'),
                'email' => $this->escaper->escapeHtml('humorgodfather9x02@gmail.com'),
            $transport = $this->transportBuilder
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    'templateVar'  => 'My Topic',
        } catch (\Exception $e) {

In this case, take note of the following:

  • $sender: Represents the sender’s information including the name and email.
  • setTemplateVars: Indicates your email’s information.
  • setTemplateIdentifierr(’email_demo_template’): Is the template email ID discussed in step one and two.
  • addTo (‘testmagento@gmail.com’): Enter the email of the recipient here.

Step 4: Creating Email Template through html

Create email_template.html in:


{Error in template processing}
    <tr>{{trans "Wellcome To: %templateVar." templateVar=$templateVar}}</tr>
{Error in template processing}

Emails can be sent through Events or simply use a Plugin to add the emails.

Step 5: Testing Email Function

To test whether the email is functioning, we will capture the customer_register_success event. That is when the customer’s account was successfully registered.

This can be created in:


You should have an output like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_register_success">
        <observer name="ha_email_customer_register" instance="HostAdvice\EmailDemo\Observer\CustomerRegisterObserver"/>

Step 6: Creating Observer for Sending Email

Create the observer in:


The output should be like this:

namespace HostAdvice\EmailDemo\Observer;

use Magento\Framework\Event\ObserverInterface;
use HostAdvice\EmailDemo\Helper\Email;

class CustomerRegisterObserver implements ObserverInterface
    private $helperEmail;
    public function __construct(
        Email $helperEmail
    ) {
        $this->helperEmail = $helperEmail;
    public function execute(\Magento\Framework\Event\Observer $observer)
        return $this->helperEmail->sendEmail();

After the account is successfully registered, the customer should receive an email as shown in the screenshot below:


That’s it! These simple steps should help you send an Email successfully in Magento 2. We hope this information was useful in helping you achieve what you want.

Good Luck!


Check out these top 3 Magento hosting services:

Was this article helpful?