Magento – Email template ‘custom/mail/template’ is not defined

email-templatesmagento2magento2-dev-betaphp-5.4

I'm written code for sending mail from my custom module, but it's throwing error. But I have configured right way. I don't know where I went wrong. Could you please throw me right direction.

Even though I have fallowed this thread, but no luck.

my code is:

Helloworld/Custom/etc/adminhtml/system.xml

<?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</label>
        </tab>
        <section id="custom" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Custom</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>

Helloworld/Custom/etc/email_templates.xml

<?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="Custom Mail" file="custom.html" type="html" module="Learning_Custom" area="frontend"/>
</config>

Helloworld/Custom/view/frontend/email/custom.html

{{template config_path="custom/mail/template"}}

<p class="greeting">{{trans "%name," name=$customer.name}}</p>
<p>{{trans "Welcome to %store_name." store_name=$store.getFrontendName()}}</p>

Helloworld/Custom/Model/Observer.php

public function execute(\Magento\Framework\Event\Observer $observer){

$emailtemplate = 'custom_mail_template';
                            $this->_transportBuilder->setTemplateIdentifier($emailtemplate)->setTemplateOptions(
                                [
                                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                                    'store' => $this->_storeManager->getStore()->getId(),
                                ]
                            )->setTemplateVars(
                                    [
                                        'store' => $this->_storeManager->getStore(),
                                        'store URL' => $prodUrl
                                    ]
                                )->setFrom(
                                    [
                                        'email' => 'dotnet@gmail.com',
                                        'name' => 'Store Owner'
                                    ]
                                )->addTo(
                                    'training@gmail.com',
                                    'Training'
                                );

                            $transport = $this->_transportBuilder->getTransport();
                            $transport->sendMessage();

}

Please let me know where I went wrong?

Best Answer

In Helloworld/Custom/Model/Observer.php file, i think you need change

public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
){
    $this->_scopeConfig = $scopeConfig;
    $this->_transportBuilder = $transportBuilder;
    $this->inlineTranslation = $inlineTranslation;
}

public function execute(\Magento\Framework\Event\Observer $observer){

    $this->inlineTranslation->suspend();

    //return template id(custom_mail_template) setup in Configuration
    $emailtemplate = $this->_scopeConfig->getValue('custom_mail_template',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);

    $transport = $this->_transportBuilder->setTemplateIdentifier($emailtemplate)->setTemplateOptions(
                                [
                                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                                    'store' => $this->_storeManager->getStore()->getId(),
                                ]
                            )->setTemplateVars(
                                    [
                                        'store' => $this->_storeManager->getStore(),
                                        'store URL' => $prodUrl
                                    ]
                                )->setFrom(
                                    [
                                        'email' => 'dotnet@gmail.com',
                                        'name' => 'Store Owner'
                                    ]
                                )->addTo(
                                    'training@gmail.com',
                                    'Training'
                                )->getTransport();

    $transport->sendMessage();
    $this->inlineTranslation->resume();
}

And rename: Helloworld/Custom/etc/email_template.xml to Helloworld/Custom/etc/email_templates.xml

I hope it help you.

Related Topic