Home/How to create custom email template in magento 2

How to create custom email template in magento 2

Published On: 6 August 2018.By .
  • Ecommerce
  • General
  • Product & platform Engineering

First we have to create a field in configuration ‘system.xml’ in folder app/code/Auriga/Email/etc/adminhtml/,  from where we can select which email template will use.

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="auriga" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
            <label>Custom Email</label>
            <tab>general</tab>
            <resource>Auriga_Addon::config_auriga_email</resource>
            <group id="custom" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
                <label>Custom Email Setting</label>
                <field id="email_template" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label comment" type="select">
                    <label>Custom Email Template</label>
                    <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
                <comment/>
                </field>
            </group>
        </section>
    </system>
</config>

Now create a email template configuration file at app/code/Auriga/Email/etc location file name should be email_templates.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="../../../Magento/Email/etc/email_templates.xsd">
   <template id="auriga_custom_email_template" label="auriga_custom_email_template" file="custom_email.html" type="html" module="Auriga_Addon" area="frontend" />
</config>

Now create email template file at app/code/Auriga/Email/view/adminhtml/email as name which you given in email_template.xml file . Here we will use  “custim_email.html” name for email template

<!--@Subject Email Template @-->
<!--@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"}}

<p>{{trans "Hi "}} {{var customer_name}},</p>
<p>
{{trans "Thank You for Registering,"}}
{{trans "We will send You Link along with the Password for you to log in. Stay connected for updates."}}
</p>

{{var store.getFrontendName()}}
{{template config_path="design/email/footer_template"}}

Now 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 Auriga!!.'
                );
$from = array('email' => "test@example.com", 'name' => 'Name of Sender');
$this->inlineTranslation->suspend();
$to = array('John@example.com');

$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$templateId = $this->scopeConfig->getValue ( 'auriga/custom/email_template', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId );

$transport = $this->_transportBuilder->setTemplateIdentifier($templateId, $storeScope)->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom($from)
            ->addTo($to)
            ->getTransport();
$transport->sendMessage();
$this->messageManager->addSuccess(__('Mail Sent Successfully'));
$this->inlineTranslation->resume();

 

Related content

We Love Conversations

Say Hello
Go to Top