Friday 23 July 2021
  • :
  • :

How to Send Email Programmatically in Magento 2

It is important to hire magento developers as they can resolve major and minor issues in magento development in lesser time. In this article, you will learn to send email programmatically in magento 2 like developers do.

In this tutorial, we will learn how to send Email in magento2, we know that in magento 1 how to send email programmatically but in magento 2 their some changes in code.

Let’s me explain you step by step for it.

  • Setup your custom module (In my case it is : DK/CustomEmail)
  • Now create email_templates.xml in the path app/code/DK/CustomEmail /etc
 <?xml version="1.0"?>  
 <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd">  
 <template id="send_email_email_template" label="Email Form" file="send_form.html" type="text" module="DK_CustomEmail" area="frontend"/>  
  • Create send_form.html in the path app/code/ DK /Email/view/frontend/email
 <!--@subject Contact Form@-->  
 <!--@vars {  
 "var data.comment":"Comment",  
 "var":"Sender Email",  
 "var":"Sender Name"  
 } @-->  
 {{trans "Name: %name" name=$}}  
 {{trans "Email: %email" email=$}}  
 {{trans "Comment: %comment" comment=$data.comment}}  
  • Create you custom controller and write below code :
 * Recipient email config path  
 const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';  
 * @var \Magento\Framework\Mail\Template\TransportBuilder  
 protected $_transportBuilder;  
 * @var \Magento\Framework\Translate\Inline\StateInterface  
 protected $inlineTranslation;  
 * @var \Magento\Framework\App\Config\ScopeConfigInterface  
 protected $scopeConfig;  
 * @var \Magento\Store\Model\StoreManagerInterface  
 protected $storeManager;   
 * @var \Magento\Framework\Escaper  
 protected $_escaper;  
 * @param \Magento\Framework\App\Action\Context $context  
 * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder  
 * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation  
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig  
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager  
 public function __construct(  
 \Magento\Framework\App\Action\Context $context,  
 \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,  
 \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,  
 \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,  
 \Magento\Store\Model\StoreManagerInterface $storeManager,  
 \Magento\Framework\Escaper $escaper  
 ) {  
 $this->_transportBuilder = $transportBuilder;  
 $this->inlineTranslation = $inlineTranslation;  
 $this->scopeConfig = $scopeConfig;  
 $this->storeManager = $storeManager;  
 $this->_escaper = $escaper;  
 * Post user question  
 * @return void  
 * @throws \Exception  
 public function execute()  
 $post = $this->getRequest()->getPostValue();  
 if (!$post) {  
 try {  
 $postObject = new \Magento\Framework\DataObject();  
 $error = false;  
 $sender = [  
 'name' => $this->_escaper->escapeHtml($post['name']),  
 'email' => $this->_escaper->escapeHtml($post['email']),  
 $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;   
 $transport = $this->_transportBuilder  
 ->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml  
 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,   
 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,  
 ->setTemplateVars(['data' => $postObject])  
 ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))  
 $transport->sendMessage(); ;  
 __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')  
 } catch (\Exception $e) {  
 __('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage())  

(Note: “’area’” is area that you work. If you want to send mail from backend use \Magento\Framework\App\Area::AREA_FRONTEND)

You may hire magento developers and experts to learn the best tricks for magento 2. This tutorial was shared by developers to make you learn about the code that helps in sending email programmatically.

Now run you custom controller with your base Url and check mail.


Vijay is a compulsive blogger who likes to educate like-minded people on various new technologies and trends. He works with Aegis SoftTech as a software developer and has been developing software for years. Stay Connected to him on Facebook and Google+.