2

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?

  • your email_templates.xml solved my problems, thanks :) – fudu Oct 5 '18 at 4:15
2

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.

  • I have update my code as per your suggestions but it's returning error Fatal error: Maximum function nesting level of '256' reached, aborting! in $transport = $this->_transportBuilder->getTransport() line. – Manoj Kumar Dec 4 '15 at 9:38
  • i think your code wrong when send mail, i facing with this same issue – Bill Dec 4 '15 at 9:45
  • have you find any solution? while you are facing this issue. – Manoj Kumar Dec 4 '15 at 9:48
  • i updated my answer, i hope it correct – Bill Dec 4 '15 at 9:55
  • $message= $this->_transportBuilder->getMessage(); Error throwing Error: undefined method getMessage() – Manoj Kumar Dec 4 '15 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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