cancel
Showing results for 
Search instead for 
Did you mean: 

custom module - send email

SOLVED

custom module - send email

hello,

I'm getting this error when trying to send email from my custom module:

Uncaught Error: Call to a member function setTemplateIdentifier() on null 

at that line I have:

->setTemplateIdentifier('contact_form')

and in etc, inside my email_templates.xml I have:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd">
    <template id="contact_form" label="Contact Form" file="send_form.html" type="text" module="Hoop_Contacts" area="frontend"/>
</config>

what can lead to not recognizing the email template?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: custom module - send email

@agiorgini,

 

I have checked your code and you have missed construct parameter in your index.php file.

 

keep below code in your controller file and remove var/generation folder,

<?php
namespace Hoop\Contacts\Controller\Index;
use Braintree\Exception;
use Magento\Framework\Mail\Template\TransportBuilder;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $transportBuilder;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        TransportBuilder $transportBuilder
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->transportBuilder = $transportBuilder;
        parent::__construct($context);
    }
    
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        if (!empty($post)) {
            $customer_email = $this->getRequest()->getPost('email');
            $sender_fullname = $this->getRequest()->getPost('firstname') . ' ' . $this->getRequest()->getPost('lastname');

            $senderInfo = [
                'name' => 'admin',
                'email' => 'rakesh.jesadiya@krishtechnolabs.com',
            ];
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('contact_form')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )->setTemplateVars(['data' => 'nonprende'])
                ->setFrom($senderInfo)
                ->addTo('a.giorgini@hoopcommunication.it')
                ->getTransport();
            $transport->sendMessage();
        }
        return $this->resultPageFactory->create();
    }
}

if issue solved, click kudos/accept as solutions.

 

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

5 REPLIES 5

Re: custom module - send email

@agiorgini,

Might be in html file, you have defined $data.name $data.email and $data.comment create issue because you havent pass any data yet to html file.

 

You can first pass templateVars data as your name email and comment data currently you have passed data as nonprende.

 

You can also check system log if any log are generated for mail.

 

You can refer this blog for more information of send mail,

Custom email template

 

If issue resolved, Click Kudos/Accept as solutions.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: custom module - send email

Hi Rakesh,

 

/app/code/Hoop/Contacts/Controller/Index/Index.php

<?php
namespace Hoop\Contacts\Controller\Index;
use Braintree\Exception;
use Magento\Framework\Mail\Template\TransportBuilder;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $transportBuilder;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        TransportBuilder $transportBuilder
    ) {
        $this->resultPageFactory = $resultPageFactory;

        parent::__construct($context);
    }
    
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();

        if (!empty($post)) {
            $customer_email = $this->getRequest()->getPost('email');
            $sender_fullname = $this->getRequest()->getPost('firstname') . ' ' . $this->getRequest()->getPost('lastname');

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('contact_form')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                );
            $receiverInfo = [
                'name' => 'pippo',
                'email' => 'a.giorgini@hoopcommunication.it'
            ];

            $senderInfo = [
                'name' => 'giorg',
                'email' => 'a.giorgini@hoopcommunication.it',
            ];

            $transport->setTemplateVars(['data' => 'nonprende'])
                ->setFrom($senderInfo)
                ->addTo($receiverInfo);
            $transport = $transport->getTransport();
            $transport->sendMessage();
        }


        return $this->resultPageFactory->create();
    }
}

/app/code/Hoop/Contacts/etc/email_templates.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd">
    <template id="contact_form" label="Contact Form" file="send_form.html" type="text" module="Hoop_Contacts" area="frontend"/>
</config>

/app/code/Hoop/Contacts/view/frontend/email/send_form.html (this one not finished yet)

<!--@subject Contact Form@-->
<!--@vars {
"var data.comment":"Comment",
"var data.email":"Sender Email",
"var data.name":"Sender Name"
} @-->

{{trans "Name: %name" name=$data.name}}
{{trans "Email: %email" email=$data.email}}

{{trans "Comment: %comment" comment=$data.comment}}

model is working because I have many test saved  in the db. Structure created with pestle.

thanks

Re: custom module - send email

thanks for your answer.

I changed this:

$transport->setTemplateVars(
                array('store' => $this->storeManager->getStore(),
                    'customer_name' => $sender_fullname,
                    'message'   => $post['message'])
            )

and I set send_form.html as per that blog you mentioned:

<!--@subject Email Subject @-->
<!--@vars
{"store url=\"\"":"Store Url",
"skin url=\"images/logo_email.gif\" _area='frontend'":"Email Logo Image"}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->
{{template config_path="design/email/header_template"}}
<table cellspacing="0" cellpadding="0" border="0" width="100%">
    <tr>
        <td align="center" valign="top" style="padding:20px 0 20px 0">
            <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                <tr>
                    <td valign="top">
                        <h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Hello"}},</h1>
                    </td>
                </tr>
                <tr>
                    <td>
                        <table cellspacing="0" cellpadding="0" border="0" width="650">
                            <tbody>
                            <tr>
                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                    {{var message}}
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA;text-align:center;">
                        <center>
                            <p style="font-size:12px;margin:0;">
                                <strong>{{trans "Thank you"}}</strong>
                            </p>
                        </center>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
{{template config_path="design/email/footer_template"}}

but unfortunately the error remains the same.

thanks

Re: custom module - send email

@agiorgini,

 

I have checked your code and you have missed construct parameter in your index.php file.

 

keep below code in your controller file and remove var/generation folder,

<?php
namespace Hoop\Contacts\Controller\Index;
use Braintree\Exception;
use Magento\Framework\Mail\Template\TransportBuilder;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $transportBuilder;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        TransportBuilder $transportBuilder
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->transportBuilder = $transportBuilder;
        parent::__construct($context);
    }
    
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        if (!empty($post)) {
            $customer_email = $this->getRequest()->getPost('email');
            $sender_fullname = $this->getRequest()->getPost('firstname') . ' ' . $this->getRequest()->getPost('lastname');

            $senderInfo = [
                'name' => 'admin',
                'email' => 'rakesh.jesadiya@krishtechnolabs.com',
            ];
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('contact_form')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )->setTemplateVars(['data' => 'nonprende'])
                ->setFrom($senderInfo)
                ->addTo('a.giorgini@hoopcommunication.it')
                ->getTransport();
            $transport->sendMessage();
        }
        return $this->resultPageFactory->create();
    }
}

if issue solved, click kudos/accept as solutions.

 

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: custom module - send email

**bleep** dependency injection, I was too tired Smiley Tongue

Thanks a lot Rakesh!