Back to Top

Magento2 – Create and Use Custom Email Templates

Updated 16 July 2021

Here will see how to create Email Templates in magento2.

Email Templates in Magento2

First create ’email_templates.xml’ file in folder ‘Webkul/Hello/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="hello_template" label="Hello World" file="hello.html" type="html" module="Webkul_Hello" area="frontend"/>
</config>

Now create email template file ‘hello.html’ in folder ‘Webkul/Hello/view/frontend/email’.

<!--@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; }
@-->
{{template config_path="design/email/header_template"}}
		<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;">{{trans "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>{{trans "Thank you"}}</strong>
									</p>
								</center>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	{{template config_path="design/email/footer_template"}}
Email Templates

This is an example of template. you can set your own message body and can send as many as variable as you can.

Now Call this template from where you want to call it.

Searching for an experienced
Magento 2 Company ?
Find out More
$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' => "[email protected]", 'name' => 'Name of Sender');
$this->inlineTranslation->suspend();
$to = array('[email protected]');
$transport = $this->_transportBuilder->setTemplateIdentifier('hello_template')
				->setTemplateOptions($templateOptions)
				->setTemplateVars($templateVars)
				->setFrom($from)
				->addTo($to)
				->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();

$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’

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


7 comments

  • dev Webwavers
  • Cero
  • vishal
  • dishank
  • dishank
  • vishal
    • Rahul
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home