6

In my custom module, I have added email functionality. It's working in 2.1 and 2.2. I have updated to 2.3.0 and now it crashes.

Email send code is as below.

$sender = [
            'name' => $this->_escaper->escapeHtml($customer_name),
            'email' => $this->_escaper->escapeHtml($customer_email),
        ];

$to_email = $this->_helper->getSendMailTo();

$sendToInfo = [
                'email' => $this->_escaper->escapeHtml($to_email),
            ];

$transport = $this->_transportBuilder->setTemplateIdentifier('config_email_settings_email_template')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($sender)
                    ->addTo($sendToInfo)
                    ->getTransport();


$transport->sendMessage();

I am getting exception as

main.CRITICAL: Exception message: The input is not a valid email address. Use the basic format local-part@hostname

Where email address is simple john@gmail.com

[2019-01-08 07:45:19] main.CRITICAL: Exception message: The input is not a valid email address. Use the basic format local-part@hostname
Trace: #0 D:\xampp\htdocs\magento23\vendor\zendframework\zend-mail\src\AddressList.php(233): Zend\Mail\Address->__construct('email', 'john@gmail...')
#1 D:\xampp\htdocs\magento23\vendor\zendframework\zend-mail\src\AddressList.php(33): Zend\Mail\AddressList->createAddress('email', 'john@gmail...')
#2 D:\xampp\htdocs\magento23\vendor\zendframework\zend-mail\src\AddressList.php(80): Zend\Mail\AddressList->add('email', 'john@gmail...')
#3 D:\xampp\htdocs\magento23\vendor\zendframework\zend-mail\src\Message.php(518): Zend\Mail\AddressList->addMany(Array)
#4 D:\xampp\htdocs\magento23\vendor\zendframework\zend-mail\src\Message.php(169): Zend\Mail\Message->updateAddressList(Object(Zend\Mail\AddressList), Array, NULL, 'Zend\\Mail\\Messa...')
#5 D:\xampp\htdocs\magento23\vendor\magento\framework\Mail\Message.php(104): Zend\Mail\Message->addTo(Array)
#6 D:\xampp\htdocs\magento23\generated\code\Magento\Framework\Mail\Message\Interceptor.php(102): Magento\Framework\Mail\Message->addTo(Array)
#7 D:\xampp\htdocs\magento23\vendor\magento\framework\Mail\Template\TransportBuilder.php(147): Magento\Framework\Mail\Message\Interceptor->addTo(Array, '')
#8 D:\xampp\htdocs\magento23\generated\code\Magento\Framework\Mail\Template\TransportBuilder\Interceptor.php(37): Magento\Framework\Mail\Template\TransportBuilder->addTo(Array, '')
#9 D:\xampp\htdocs\magento23\app\code\Vendor\ModuleName\Controller\Order\Save.php(113): Magento\Framework\Mail\Template\TransportBuilder\Interceptor->addTo(Array)
#10 D:\xampp\htdocs\magento23\generated\code\Vendor\ModuleName\Controller\Order\Save\Interceptor.php(24): Vendor\ModuleName\Controller\Order\Save->execute()
#11 D:\xampp\htdocs\magento23\vendor\magento\framework\App\Action\Action.php(108): Vendor\ModuleName\Controller\Order\Save\Interceptor->execute()
#12 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(58): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#13 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(138): Vendor\ModuleName\Controller\Order\Save\Interceptor->___callParent('dispatch', Array)
#14 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(153): Vendor\ModuleName\Controller\Order\Save\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#15 D:\xampp\htdocs\magento23\generated\code\Vendor\ModuleName\Controller\Order\Save\Interceptor.php(39): Vendor\ModuleName\Controller\Order\Save\Interceptor->___callPlugins('dispatch', Array, Array)
#16 D:\xampp\htdocs\magento23\vendor\magento\framework\App\FrontController.php(159): Vendor\ModuleName\Controller\Order\Save\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#17 D:\xampp\htdocs\magento23\vendor\magento\framework\App\FrontController.php(99): Magento\Framework\App\FrontController->processRequest(Object(Magento\Framework\App\Request\Http), Object(Vendor\ModuleName\Controller\Order\Save\Interceptor))
#18 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#19 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#20 D:\xampp\htdocs\magento23\vendor\magento\module-store\App\FrontController\Plugin\RequestPreprocessor.php(94): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#21 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(135): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#22 D:\xampp\htdocs\magento23\vendor\magento\module-page-cache\Model\App\FrontController\BuiltinPlugin.php(73): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#23 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(135): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#24 D:\xampp\htdocs\magento23\vendor\magento\framework\Interception\Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#25 D:\xampp\htdocs\magento23\generated\code\Magento\Framework\App\FrontController\Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, NULL)
#26 D:\xampp\htdocs\magento23\vendor\magento\framework\App\Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#27 D:\xampp\htdocs\magento23\generated\code\Magento\Framework\App\Http\Interceptor.php(24): Magento\Framework\App\Http->launch()
#28 D:\xampp\htdocs\magento23\vendor\magento\framework\App\Bootstrap.php(258): Magento\Framework\App\Http\Interceptor->launch()
#29 D:\xampp\htdocs\magento23\index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#30 {main} [] []
  • Can anyone give solution please for this? – Magecode Jan 2 at 11:33
  • Hi, did you setup cron job? – Sourav Jan 4 at 7:34
  • I have not set cron job. it's in local. I'm sending mail from controller. – Magecode Jan 4 at 7:35
  • Did you try with static email address? See same exception occur or not. – Sourav Jan 4 at 7:37
  • Yes I have tried with static email address. Same exception as above. – Magecode Jan 4 at 7:39
5
+50

Finally I have Found Solution and Now it's Working

$sender = [    
           'email' => $this->_escaper->escapeHtml($customer_email),
           'name' => $this->_escaper->escapeHtml($customer_name)
         ];

$to_email = $this->_helper->getSendMailTo();

$transport= $this->_transportBuilder->setTemplateIdentifier('config_email_settings_email_template')
        ->setTemplateOptions($templateOptions)
        ->setTemplateVars($templateVars)
        ->setFrom($sender)
        ->addTo($to_email, 'Name')
        ->getTransport();

$transport->sendMessage();
3

Please replace your code with following code and check after clear your cache, also please remove "_escaper" from your code.

$transport = $this->transportBuilder
            ->setTemplateIdentifier('config_email_settings_email_template')
            ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => $this->storeManager->getStore()->getId()
                ]
            )
            ->setTemplateVars($variables)
            ->setFrom($emailSender)
            ->addTo($emailRecipient)
            ->setReplyTo($replyToEmail, $replyToName)
            ->getTransport();

        $transport->sendMessage();
  • @Magecode does this work for you? – Aasim Goriya Jan 8 at 4:22
  • Still same issue – Magecode Jan 8 at 7:45
  • I'm getting eoor on this line ->addTo($sendToInfo) – Magecode Jan 8 at 7:45
  • I have passed email ID static in $sendToInfo = [ 'email' => 'magecode@example.com' ]; – Magecode Jan 8 at 7:46
  • Add like this : $senderInfo = [ 'name' => 'Mage Code', 'email' => 'magecode@example.com'' ]; – Aasim Goriya Jan 8 at 8:10
2

Since Magento 2.3, Magento uses Zend Framework 2 for sending emails. Which probably changed the logic used for parsing array with the recipient data.


So here are few variants on how you can properly send email in Magento 2.3:

  1. For multiple recipients: use array where key is the recipient's email address and value - recipient's name (note, the same rules applied to sender details):
$sender = [$this->_escaper->escapeHtml($customer_email) => $this->_escaper->escapeHtml($customer_name)];

$to_email = $this->_helper->getSendMailTo();

$sendToInfo = [$this->_escaper->escapeHtml($to_email) => 'Recipient Name'];

$transport = $this->_transportBuilder->setTemplateIdentifier('config_email_settings_email_template')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($sender)
                    ->addTo($sendToInfo)
                    ->getTransport();


$transport->sendMessage();
  1. Or you can use the code below to send an email to a single recipient. This code is backward compatible with Magento 2.2 and lower versions:
$to_email = $this->_helper->getSendMailTo();

$transport = $this->_transportBuilder->setTemplateIdentifier('config_email_settings_email_template')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($this->_escaper->escapeHtml($customer_email))
                    ->addTo($this->_escaper->escapeHtml($to_email))
                    ->getTransport();


$transport->sendMessage();

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.