We will learn how to create a custom email template and how to use them.
I hope you know the Magento provides many native email templates for Magento entities.
If you want to create a custom email template for your custom module then it’s possible Magento.
Let’s start to create a custom email template with easy steps.
Step 1: To create an ’email_templates.xml’ file in the module ‘etc’ folder.
app/code/Mageclues/EmailTemplate/etc/email_templates.xml
<?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="welcome_template" label="Welcome Template" file="welcome.html" type="html" module="Mageclues_EmailTemplate" area="frontend"/>
</config>
Step 2: You need to create an email template file ‘welcome.html’ in the email folder at path app/code/Mageclues/EmailTemplate/view/frontend/email/welcome.html
<!--@subject Welcome Email Subject @-->
{{template config_path="design/email/header_template"}}
<!-- you need to put your email body contet here -->
<h2> Hello, </h2>
<p> Welcome to MageClues</p>
{{template config_path="design/email/footer_template"}}
That’s it. your custom email template is created with Magento.
We will look at how to use the created email template and send email.
We created a helper class with SendEmail method, you can make your
SendEmail function where you want.
<?php
namespace Mageclues\EmailTemplate\Helper;
use Magento\Framework\App\Area;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Helper\AbstractHelper;
class SendEmail extends AbstractHelper
{
/**
* @var StateInterface
*/
protected $_inlineTranslation;
/**
* @var TransportBuilder
*/
protected $_transportBuilder;
/**
* @var StoreManagerInterface
*/
protected $_storeManager;
public function __construct(
Context $context,
TransportBuilder $transportBuilder,
StateInterface $inlineTranslation,
StoreManagerInterface $storeManager
)
{
$this->_inlineTranslation = $inlineTranslation;
$this->_transportBuilder = $transportBuilder;
$this->_storeManager = $storeManager;
parent::__construct($context);
}
public function sendMail()
{
$variables = []
$sender = [
'name' => $this->scopeConfig->getValue('trans_email/ident_general/name', ScopeInterface::SCOPE_STORE),
'email' => $this->scopeConfig->getValue('trans_email/ident_general/email', ScopeInterface::SCOPE_STORE)
];
$this->_inlineTranslation->suspend();
$this->_transportBuilder->setTemplateIdentifier(
'welcome_template' // template identifier here
)->setTemplateOptions(
[
'area' => Area::AREA_FRONTEND,
'store' => $this->_storeManager->getStore()->getId()
]
)->setTemplateVars(
$variables
)->setFromByScope(
$sender
)->addTo(
'receiver.user@example.com'
);
$transport = $this->_transportBuilder->getTransport();
try {
$transport->sendMessage();
} catch (\Exception $exception) {
}
$this->_inlineTranslation->resume();
return $this;
}
I hope this blog is helpful for you. I request you please leave a comment below. if you have any questions.
Thank you):