Create Custom Email Templates Magento 2

Mail Template In Magento 2

Create a custom template using custom variables and custom styles.

create ’email_templates.xml’ file in  “Bluethink/Customhome/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="onboard_customer_mail" label="Onboard Customer" file="onboard_customer.html" type="html" module="Bluethink_Customhome" area="frontend"/>
</config>

Now create mail template file ‘onboard_customer.html’ in “Bluethink/Customhome/view/frontend/email”.

 


<!--@subject {{trans "Welcome to %store_name" store_name=$store.getFrontendName()}} @-->
<html>
<head>
<title>Mail Template</title>
</head>
<body>
<div width="100%" bgcolor="#f6f6f6" align="center">
<tbody>
<tr>
<td valign="top;">
<table align="center">
<tbody>
<tr>
<td>
<div>
<!-- Logo head start -->
<div>
<a href="#"><img src="{{var logo_url}}"> </a>
</div>

<!-- Introduction start -->
<div>
<table>
<tbody>
<tr>
<td class="intro-heading">Dear {{var name|raw}},</td>
</tr>
<tr>
<td class="ser_warm">Warm Regards</td>
</tr>
<tr>
<td class="ser_warm ser_warm2">Bluethink Team</td>
</tr>
</tbody>
</table>
</div>
<div class="coupon_detail">
<table width="100%;">
<tbody>
<tr><td>Email Id : </td><td class="name_info">{{var email|raw}}</td></tr>
<tr><td>Password : </td><td class="discount_info">{{var password|raw}}</td></tr>
</tbody>
</table>
</div>
<!--Coupon end-->
<div><a href="{{var baseurl}}">Click here to Login </a></div>
<!-- Footer start -->
<div>
<a href="#"><img src="{{var below_logo_url}}"> </a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

you can set your parameters (variables) that you have sent from template

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


$salesemail=$this->_scopeConfig->getValue('trans_email/ident_sales/email', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$salesname=$this->_scopeConfig->getValue('trans_email/ident_sales/name', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

$templateOptions = array(
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'store' =>$this->storeManager->getStore()->getId()
);
$templateVars = array(
'name' => $data[1].' '.$data[2].' '.$data[3],
'email' => $data[6],
'password' => 'admin123',
'logo_url' => $this->_assetRepo->getUrl('Bluethink_Customhome::images/bigfish-mailer-BD_2_02.png'),
'below_logo_url' => $this->_assetRepo->getUrl('Bluethink_Customhome::images/bigfish-mailer-BD_2_25.png'),
'baseurl' => $baseurl

);
$from = array('email' => $salesemail, 'name' =>$salesname);
$this->inlineTranslation->suspend();
$to = array($data[6]);
$transport = $this->_transportBuilder->setTemplateIdentifier('onboard_customer_mail')
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($to)
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();

$this->_scopeConfig is intance of “Magento\Framework\App\Config\ScopeConfigInterface”

$this->_assetRepo is intance of “Magento\Framework\View\Asset\Repository”

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