Changing Email Format for Magento Admin to Plain Text
As an e-commerce developer, you often need to customize your store’s email templates to fit specific business needs. One common requirement is to change the format of certain emails from HTML to plain textâspecifically for admin notifications while keeping customer emails in HTML format.
Understanding the Two Email Types in Magento
Magento 2 sends two types of order confirmation emails:
- Customer email (sent after an admin approves an order): This is always sent as an HTML-formatted email.
- Admin email (sent when a new order is created or status changes): By default, this is also sent in HTML format.
Changing Admin Email to Plain Text
To change the format of admin emails only, you’ll need to modify the core template file and create a custom layout XML. Here’s a step-by-step guide:
Step 1: Locate the Core Template File
The order confirmation email for admins is defined in the Vendor/Theme/view/frontend/templates/order_new.xml file located under your theme folder.
<code>app/design/frontend/Vendor/Theme/Magento_Theme/layout/order_new.xml</code>
Step 2: Create a New Template File
Create a new file called order_new_text.phtml in the same directory as above. This will be used for the plain text version of the admin email.
<code>app/design/frontend/Vendor/Theme/Magento_Theme/templates/order_new/text.phtml</code>
Step 3: Modify the Core Template
Open your order_new.xml file and add a new reference to your custom template. Replace <p> tags with <pre> tags to preserve line breaks.
<referenceBlock name="Magento\_Sales\/email\_order\item" name="item">
<action method="setTemplate"><argument name="template" xsi:type="string">Magento_Sales::email/order/items.phtml</argument></action>
</referenceBlock>
<!-- Add this section: -->
<referenceBlock name="Magento\_Sales\/email\_order\/" name="custom_items_text">
<action method="setTemplate"><argument name="template" xsi:type="string">Magento_Theme::order_new/text.phtml</argument></action>
</referenceBlock>
Step 4: Update the Layout XML
Create a new file called sales_order_email.xml in your theme’s layout folder:
<code>app/design/frontend/Vendor/Theme/Magento_Sales/layout/sales_order_email.xml</code>
Add the following content to this file:
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<update handle="sales_email_order_items_text"/>
</layout>
Step 5: Save and Refresh Your Cache
After saving the changes, remember to refresh your Magento cache by running bin/magento c:f in your terminal. This will apply the new changes.
Conclusion
In this guide, we’ve demonstrated how to change the format of admin emails from HTML to plain text while keeping customer emails unchanged. By creating a custom template and layout XML, you can achieve a clean separation between these two email types without affecting core Magento files.
Remember, it’s always a best practice to avoid direct modifications to core files. Consider using a child theme or creating a custom module for more robust maintenance and upgrade compatibility.
We hope this solution helps you customize your Magento store seamlessly! For further insights on Magento development, feel free to explore our resources at Laravel Company.