How to Override Email Templates in Custom Module in Magento 2

In Magento 2, email templates play a crucial role in delivering personalized and engaging communication to customers. However, there are instances when you may need to modify or customize these email templates to align them with your brand’s identity or add specific functionality. In this blog post, we will explore the process of overriding email templates in a custom module in Magento 2. By following these steps, you will be able to seamlessly customize email templates to suit your business requirements.

Overview of Email Templates in Magento 2

Before diving into the customization process, let’s have a brief overview of email templates in Magento 2. Email templates are predefined HTML files that contain the content and layout for various types of emails, such as order confirmation, account registration, and password reset emails. Magento 2 provides a set of default email templates that are located in the core modules.

However, directly modifying the email templates in the core modules is not recommended. When you update Magento or install patches, these modifications may be overwritten, resulting in the loss of your changes. To avoid this, it is best practice to override the email templates through a custom module.

By creating a custom module and overriding the email templates within it, you can ensure that your modifications remain intact during system updates. This approach provides a safer and more sustainable way to customize email templates in Magento 2.

Now let’s proceed to the next section, which covers the creation of a custom module in Magento 2.

How to Override the Email Template in the Custom Module

To override the existing email templates in Magento 2, you need to determine the ID of the template you want to modify. You can typically find this ID in the etc/email_templates.xml file of the core modules. In this article, I will perform an override of the “New Account Confirmation Key” email template, which is sent when a user completes the registration process and needs to verify their email to activate their account.

Step 1:
In your custom module, create the file app/code/Vendor/Module/etc/email_templates.xml with the following code:

<?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="customer_create_account_email_confirmation_template" label="New Account Confirmation Key" file="account_new_confirmation.html" type="html" module="Vendor_Module" area="frontend"/>
</config>

Step 2:
Next, create the file app/code/Vendor/Module/view/frontend/email/account_new_confirmation.html.

Note: the html file name must match the file="" attribute in email_templates.xml.
The HTML content in the account_new_confirmation.html file you can copy in the core module or use the code below to customize according to your needs:

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<!--@subject {{trans "Please confirm your %store_name account" store_name=$store.frontend_name}} @-->
<!--@vars {
"var store.frontend_name":"Store Name",
"var this.getUrl($store,'customer/account/confirm/',[_query:[id:$customer.id,key:$customer.confirmation,back_url:$back_url],_nosid:1])":"Account Confirmation URL",
"var this.getUrl($store, 'customer/account/')":"Customer Account URL",
"var customer.email":"Customer Email",
"var customer.name":"Customer Name"
} @-->

{{template config_path="design/email/header_template"}}

<!-- Your custom email template HTML code -->
<p class="greeting">{{trans "%name," name=$customer.name}}</p>
<p>{{trans "You must confirm your %customer_email email before you can sign in (link is only valid once):" customer_email=$customer.email}}</p>

<table class="button" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <table class="inner-wrapper" border="0" cellspacing="0" cellpadding="0" align="center">
                <tr>
                    <td align="center">
                        <a href="{{var this.getUrl($store,'customer/account/confirm/',[_query:[id:$customer.id,key:$customer.confirmation,back_url:$back_url],_nosid:1])}}" target="_blank">{{trans "Confirm Your Account"}}</a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

{{template config_path="design/email/footer_template"}}

At the beginning of the code snippet, Magento provides us with appropriate var variables for each type of email template, making it easier for us to customize. After finishing the customization, remember to save your changes, clear the cache, and perform testing to ensure everything is functioning as expected.

Additionally, you may consider using a Magento 2 SMTP Extension to enhance the email delivery capabilities of your store

Conclusion

Customizing email templates in Magento 2 is essential for delivering personalized and branded communication to customers. However, directly modifying core templates can lead to issues during system updates and potential loss of changes. To avoid this, it is recommended to override templates using a custom module, ensuring long-term stability and customization flexibility.

4.5 2 votes
Article Rating

Callula Huy

Callula is the Marketing Executive at Magetop. With more than 5 years of copywriting under his belt, Callula is into creating valuable content that is straight to the point. Life student. Workaholic. Foreign languages and traveling aficionado.

Leave a Reply or put your Question here

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x