Here will see how to create Custom Email Templates programmatically in magento2. We can use this in our Custom Module. Assume Module name is Mageelite_Bare
First create ’email_templates.xml’ file in folder ‘Mageelite/Bare/etc’
<?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="mageelite_template" label="Email" file="megeelite_email.html" type="html" module="Mageelite_Bare" area="frontend"/>
</config>
So Lets Quickly disect above code,
- we have declared our email template "mageelite_email.html" with id "mageelite_template".
- If we would send email from backend module then we set ‘admin’ otherwise it will be frontend.
Now we need to create mageelite_email.html under under app/code/Mageelite/Bare/view/frontend/email
directory
<!--@subject Email Subject @-->
<!--@vars
{"store url=\"\"":"Store Url",
"skin url=\"images/logo_email.gif\" _area='frontend'":"Email Logo Image"}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->
{Error in template processing}
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td align="center" valign="top" style="padding:20px 0 20px 0">
<table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
<tr>
<td valign="top">
<h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">Hello,</h1>
</td>
</tr>
<tr>
<td>
<table cellspacing="0" cellpadding="0" border="0" width="650">
<tbody>
<tr>
<td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
{{var message}}
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA;text-align:center;">
<center>
<p style="font-size:12px;margin:0;">
<strong>Thank you</strong>
</p>
</center>
</td>
</tr>
</table>
</td>
</tr>
</table>
{Error in template processing}
At Last Call this template from where you want to call it.
$templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId());
$templateVars = array(
'store' => $this->storeManager->getStore(),
'customer_name' => 'John Doe',
'message' => 'Hello World!!.'
);
$from = array('email' => "test@example.com", 'name' => 'Name of Sender');
$this->inlineTranslation->suspend();
$to = array('john@mageelite.com');
$transport = $this->_transportBuilder->setTemplateIdentifier('mageelite_email')
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($to)
->getTransport();
//$transport->sendMessage();
$this->inlineTranslation->resume();
try {
// Send an email
$transport->sendMessage();
} catch (\Exception $e) {
// Write a log message whenever get errors
$this->logger->critical($e->getMessage());
}
return $this;
Magento\Framework\Mail\Template\TransportBuilder is used for sending custom email in Magento 2.
DONT FORGET to include below classes
$this->storeManager is instance of ‘Magento\Store\Model\StoreManagerInterface’
$this->_transportBuilder is instance of ‘Magento\Framework\Mail\Template\TransportBuilder’
$this->inlineTranslation is instance of ‘Magento\Framework\Translate\Inline\StateInterface’
That's it.