How to send custom email in Magento 2? – Usually we want to write custom emails for our custom Magento website developments. Although we developed a lot of custom modules. We used to search this topic “send custom email in Magento” whenever we need to develop new custom modules. Today lets see how to send custom emails in Magento 2.
Following are the steps for Magento 2 send email programmatically.
- Create email_templates.xml.
- Create an email template file.
- Call the email template.
I am not going to explain how to create Magento 2 custom modules in this article.
Create email_templates.xml.
In our custom module, we need to create the email_templates.xml file under vendor_name/Module_name/etc directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0"?> <!-- /** * @author Mujahidh Haseem * @copyright Copyright (c) 2020 Ayakil (https://www.mujahidh.com) * @package Ayakil_CustomEmail */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> <template id="ayakil_email_label_template" label="Ayakil Return Shipping label customer notification template" module="Ayakil_CustomEmail" file="ayakil_label_send_customer.html" type="html" area="frontend"/> </config> |
Create an email template file.
Here we can create whatever the email body you want to send according to your website functionality. According to our email_templates.xml template file name is ayakil_label_send_customer.html. We want to create this template file under the directory of vendor_name/Module_name/view/frontend/email/ayakil_label_send_customer.html
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!--@subject Shipment label details for Order # {{var order_id}} @--> {{template config_path="design/email/header_template"}} <div class="ayakil-email-template"> <div class="ayakil-email-container"> <p class="ayakil-title">{{trans "Dear"}} <strong>{{var customer_name|escape}}</strong>, </p> <p> Please find the shipment label to use when returning your items.</p> <p><a style="color: #0A246A" href="{{var shipping_label}}">{{trans "Shipping Label"}}</a> </p> </div> </div> {{template config_path="design/email/footer_template"}} |
Call the email template.
We can call wherever we want to fire this email. Here in this module, I used a helper class to send this email to my custom module. You can place this code piece according to your code. So my Email.php helper class is under the directory vendor_name/Module_name/Helper.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php namespace Ayakil\CustomEmail\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Framework\Escaper; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\StoreManagerInterface; /** * Class ModifyOrders * * @package Ayakil\CustomEmail\Helper */ class Email extends AbstractHelper { protected $inlineTranslation; protected $escaper; protected $transportBuilder; protected $logger; protected $scopeConfig; protected $storeManager; /** * @param \Magento\Framework\App\Helper\Context $context */ public function __construct( Context $context, StateInterface $inlineTranslation, Escaper $escaper, TransportBuilder $transportBuilder, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig ) { parent::__construct($context); $this->inlineTranslation = $inlineTranslation; $this->escaper = $escaper; $this->transportBuilder = $transportBuilder; $this->logger = $context->getLogger(); $this->scopeConfig = $scopeConfig; $this->storeManager = $storeManager; } // You can pass whatever the arguments here according to your requirement. public function SendEmail( $orderId, $shipping_label_path,$pdf_name ,$customer_name, $customerEmail ) { try { $this->inlineTranslation->suspend(); $sender_name = $this->scopeConfig->getValue('general/store_information/name',\Magento\Store\Model\ScopeInterface::SCOPE_STORE); $sender_email = $this->scopeConfig->getValue('trans_email/ident_general/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE); $sender = [ 'name' => $this->escaper->escapeHtml($sender_name), 'email' => $this->escaper->escapeHtml($sender_email), ]; $transport = $this->transportBuilder ->setTemplateIdentifier('ayakil_email_label_template') ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId(), ] ) ->setTemplateVars([ 'customer_name' => $customer_name, 'shipping_label' => $shipping_label_path, 'order_id' => $orderId, ]) ->setFrom($sender) ->addTo($customerEmail) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); } catch (\Exception $e) { $this->logger->debug($e->getMessage()); } } } |
With this code, we can send the Magento 2 emails. For the setTemplateOptions i gave the exact store_id. Because I faced an issue in the custom email template. When an email receiving the default header and footer is picking. Not the theme’s header and footer for the template. To set the store themes header and footer for the email template we need to give the exact store_id here.
So in this Magento blog, we learned how to create a custom email template and sending the emails. This is pretty simple but easily forgetting stuff when it comes to Magento development. I hope reading will help someone in the future.
Visit Magento 2 GraphQl Tutorials for some amazing reading with the examples to easily understanding. Please share this article with your network too.