cancel
Showing results for 
Search instead for 
Did you mean: 

custom module - send email

SOLVED

custom module - send email

hello,

I'm building my own custom module, this is my controller:

<?php
namespace Hoop\Contact\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Mail\Template\TransportBuilder;

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

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        TransportBuilder $transportBuilder
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->transportBuilder = $transportBuilder;

        return parent::__construct($context);
    }
    public function execute()
    {
        if ($this->getRequest()->isPost()) {
            $customer_email = $this->getRequest()->getPost('email');
            $fullname = $this->getRequest()->getPost('firstname') . ' ' . $this->getRequest()->getPost('lastname');

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('gama_contact')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                );
           $transport->setTemplateVars(['data' => 'nonprende'])
                ->setFrom('ppp@test.it')
                ->addTo('myemail@mydomain.it')
                ->getTransport();
            $transport->sendMessage();
        }
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Contact'));
        return $resultPage;
    }
}

but when called I have a blank page and I can see this in the log:

 

PHP Fatal error:  Uncaught TypeError: Argument 3 passed to Hoop\\Contact\\Controller\\Index\\Index::__construct() must be an instance of Magento\\Framework\\Mail\\Template\\TransportBuilder, none given, called in /var/www/magento/var/generation/Hoop/Contact/Controller/Index/Index/Interceptor.php on line 14 and defined in /var/www/magento/app/code/Hoop/Contact/Controller/Index/Index.php:13

 

how to fix it?

many thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: custom module - send email

@agiorgini

 

Hello

 

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

Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

View solution in original post

11 REPLIES 11

Re: custom module - send email

@agiorgini

 

please remove var/generation and var/di

 

Notice :- when you change into construct you must need to delete var/generation  and var/di folder.

 

if your problem solved then mark as solutiion.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: custom module - send email

Hi Sunil,

thanks a lot for your answer. I did but same error still there. After deletion should I clean cache or do setup:upgrade and then recompile?

thanks

Re: custom module - send email

@agiorgini 

 

do you not do rcompile while doing develop, it need when site live and in production mode.

 

Just remove var/cache, var/view_proced, var/generaion, var/di and check it.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: custom module - send email

ok now the error changed:

 

HP Fatal error:  Uncaught Error: Call to undefined method Magento\\Framework\\Mail\\Template\\TransportBuilder::sendMessage() in /var/www/magento/app/code/Hoop/Contact/Controller/Index/Index.php

 

any further hint?

thanks

Re: custom module - send email

@agiorgini,

Check Blogs for step by step guide to send mail, Send Mail Programmatically In Magento 2

Its seem look like your code error for setFrom('email,'areacode') Method of transport builder,

 

You can set areacode like, admin

You have just pass email id in setFrom() method, you have to pass second argument to setFrom method,

 

$transport->setTemplateVars(['data' => 'nonprende'])
->setFrom('ppp@test.it','admin')
->addTo('myemail@mydomain.it')
->getTransport();
$transport->sendMessage();

Change your code in controller with above code,

Please let me know if you have query.

 

If issue solved, click Kudos and Accept as Solution.

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

Re: custom module - send email

@agiorgini

 

$transport->setTemplateVars(['data' => 'nonprende'])
                ->setFrom('ppp@test.it')
                ->addTo('myemail@mydomain.it')
                ->getTransport()->sendMessage()

Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: custom module - send email

Hello Rakesh,

thanks for your answer; ok the error message changed again:

 

1 exception(s):
Exception #0 (Magento\Framework\Exception\MailException): Dati mittente non validi

Exception #0 (Magento\Framework\Exception\MailException): Dati mittente non validi
#0 /var/www/magento/vendor/magento/framework/Mail/Template/TransportBuilder.php(167): Magento\Email\Model\Template\SenderResolver->resolve('ppp@test.it')
#1 /var/www/magento/app/code/Hoop/Contact/Controller/Index/Index.php(83): Magento\Framework\Mail\Template\TransportBuilder->setFrom('ppp@test.it', 'admin')

 "dai mittente non validi" means sender's data not valid.

 

Thanks

Re: custom module - send email

@agiorgini

 

please check below tutorial for that 

https://webkul.com/blog/magento-2-send-transactional-email-programmatically-in-your-custom-module/


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: custom module - send email

according to that tutorial, I did set:

$receiverInfo = [
                'name' => 'pippo',
                'email' => 'myname@mydomain.it'
            ];
            /* Sender Detail  */
            $senderInfo = [
                'name' => 'giorg',
                'email' => 'myname@mydomain.it',
            ];
            $transport->setTemplateVars(['data' => 'nonprende'])
                ->setFrom($senderInfo)
                ->addTo($receiverInfo)
                ->getTransport();
            $transport->sendMessage();

but now I get again:

 

PHP Fatal error:  Uncaught Error: Call to undefined method Magento\\Framework\\Mail\\Template\\TransportBuilder::sendMessage() in /var/www/magento/app/code/Hoop/Contact/Controller/Index/Index.php

 

I don't need an external helper class, I just need to send the email loading my custom email template.

Thanks