🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Send Custom Emails Programmatically in Magento 2

By Sanjay JethvaUpdated on May 22, 2025 2 min read

Emails are a traditional but effective way of communication for business. According to Hubspot, active Email accounts are expected to hit 5.6 billion by 2019.

This clearly shows that when there are more platforms to communicate with the audience than ever before, the Email is still a popular platform.

The Magento 2 store owners can make the most out of it by sending various types of Emails such as

  • Acknowledgment Emails
  • Marketing Emails
  • Transactional Emails
  • Newsletters/Informative Emails
  • Milestone Emails
  • Plain text Emails
  • Lead Nurturing Emails

However, the default Magento 2 does not support such a wide variety of Emails. It only supports:

  • Welcome Email
  • Forgot Password Email
  • Order Confirmation Email
  • Invoice Email
  • Shipment Email
  • Credit Memo Email
  • Newsletter subscription Email

However, when you want to send personalized emails like offers, birthday wishes to customers, etc, you can use the method given here to send custom Emails programmatically in Magento 2.

Steps to Send Custom Emails Programmatically in Magento 2:

<?php

namespace [Vendor]\[Module]\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
    protected $transportBuilder;
    protected $storeManager;
    protected $inlineTranslation;

    public function __construct(
        Context $context,
        TransportBuilder $transportBuilder,
        StoreManagerInterface $storeManager,
        StateInterface $state
    )
    {
        $this->transportBuilder = $transportBuilder;
        $this->storeManager = $storeManager;
        $this->inlineTranslation = $state;
        parent::__construct($context);
    }

    public function sendEmail()
    {
        // this is an example and you can change template id,fromEmail,toEmail,etc as per your need.
        $templateId = 'my_custom_email_template'; // template id
        $fromEmail = '[email protected]';  // sender Email id
        $fromName = 'Admin';             // sender Name
        $toEmail = '[email protected]'; // receiver email id

        try {
            // template variables pass here
            $templateVars = [
                'msg' => 'test',
                'msg1' => 'test1'
            ];

            $storeId = $this->storeManager->getStore()->getId();

            $from = ['email' => $fromEmail, 'name' => $fromName];
            $this->inlineTranslation->suspend();

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $templateOptions = [
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => $storeId
            ];
            $transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope)
                ->setTemplateOptions($templateOptions)
                ->setTemplateVars($templateVars)
                ->setFrom($from)
                ->addTo($toEmail)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->_logger->info($e->getMessage());
        }
    }
}

That’s it.

Note: The above solution is for the Magento 2.2.x and Magento 2.3.x versions.

Here’s another useful solution to add birthday field to checkout in Magento 2, which you can use to collect birthday information from customers for sending wishes.

Take the maximum benefit of emails by sending custom emails that you can think of using this method!

Feel free to share the solution with fellow developers on social media.

Thank you. Keep Emailing! 

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.