17

I'm trying to send mail from my custom module. For this I have created system.xml file, but it's returning error.

Error:

Email template 'custom_mail_template' is not defined.

my code is:

 <?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>
        <tab id="custom" translate="label" sortOrder="1">
            <label>custom mail</label>
        </tab>
        <section id="custom" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>custom mail</label>
            <tab>custom</tab>
            <resource>Learning_Custom::config_custom</resource>
            <group id="mail" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Custom</label>
                <field id="active" translate="label" type="select" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="template" translate="label comment" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Email Template</label>
                    <comment>Email template chosen based on theme fallback when "Default" option is selected.</comment>
                    <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Any suggestions will appreciate.

3 Answers 3

31

You need create file email_templates.xml in etc folder with content:

<?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="custom_mail_template" label=" Your Custom Label" file="yourfile.html" type="html" module="Vendor_YourModule" area="frontend"/>
</config>

This file using declare template have id custom_mail_template in Configuration with path of template file you want use.

After, you create template email file yourfile.html in path Vendor/YourModule/view/frontend/email/yourfile.html

I hope it help for you.

2
  • Does not work for me.
    – Black
    Mar 15, 2021 at 10:23
  • 1
    So template id in email_templates.xml should match the path in system.xml separated by underscores. Woah Magennnnntooooo!!!! Sep 1, 2023 at 8:17
5

I face the same problem when I use a custom email template and its resolved by the following changes.

If you check your system.xml file you can find that you path for system config for email template is created based upon that. In simple terms, email template-id is created upon sectionid_groupid_fieldid.

Here it is custom_mail_template.

1
  • This. And it looks like that even if you have a config.xml defining the defaults for the config path set in system.xml, that default must still be equal to the config path itself, but delimited with underscores (_) instead of slashes (/). Otherwise, you get the error. Oct 11, 2023 at 8:48
-2

If everything looks correct and still getting this error, please try running below commands and check. It worked for me.

bin/magento setup:upgrade
bin/magento setup:di:compile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.