0

I did create a cron job with custom sql query. It will send an email notification to our support email. Can someone tell me what's wrong my email coding, from the log it said I am unable to send mail? I had mageplaza SMTP setup, I am able to send testing email by using gmail SMTP

 <?php namespace ABC\CheckAddress\Cron; use Psr\Log\LoggerInterface; class Test { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * Write to system.log * * @return void */ public function execute() { //$this->logger->info('Cron Works'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $tableName = $resource->getTableName('sales_order_payment'); $attribute_information = "SELECT sales_order_payment.additional_information FROM sales_order LEFT JOIN sales_order_payment ON sales_order.entity_id = sales_order_payment.parent_id WHERE sales_order_payment.method = 'ABCpayment' AND sales_order.updated_at >= NOW() - INTERVAL 1 MONTH"; $results = $connection->fetchAll($attribute_information); foreach ($results as $result) { $json_array = implode($result); $data = json_decode($json_array); $address_verification_json = $data->shipping_address_verification; $data2 = json_decode($address_verification_json); $postal_code = $data2->postal_code; if ($postal_code < 1) { /* How I send ? SEND EMAIL TO CUSTOMER SERVICE */ $body = 'test'; $from = 'Paul'; $nameFrom = '[email protected]'; $to = 'Paul'; $nameTo = '[email protected]''; $email = new \Zend_Mail(); $email->setSubject("Feedback email"); $email->setBodyText($body); $email->setFrom($from, $nameFrom); $email->addTo($to, $nameTo); $email->send(); /* it show unable to send mail am I missing something? */ } } } } 

this is the error

 [2020-11-25 23:52:03] report.CRITICAL: Unable to send mail. {"exception":"[object] (Zend_Mail_Transport_Exception(code: 0): Unable to send mail. at /var/www/html/Magento2/vendor/magento/zendframework1/library/Zend/Mail/Transport/Sendmail.php:111)"} [] 
1

1 Answer 1

0

I finally manage to get the cron job running and able to send out email. I would like to share my code, if someone have the same problem in the future.

<?php namespace ABC\CheckPay\Cron; use Magento\Framework\Mail\Template\TransportBuilder; class Test { /** * @var \Psr\Log\LoggerInterface */ private $logger; /** * @var TransportBuilder */ private $transportBuilder; public function __construct( \Psr\Log\LoggerInterface $logger, TransportBuilder $transportBuilder ) { $this->logger = $logger; $this->transportBuilder = $transportBuilder; } /** * Execute the cron * * @return void */ public function execute() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $tableName = $resource->getTableName('sales_order_payment'); $attribute_information = "SELECT sales_order_payment.additional_information FROM sales_order LEFT JOIN sales_order_payment ON sales_order.entity_id = sales_order_payment.parent_id WHERE sales_order_payment.method = 'checkpay' AND sales_order.updated_at >= NOW() - INTERVAL 1 MONTH"; $results = $connection->fetchAll($attribute_information); foreach ($results as $result) { $json_array = implode($result); $data = json_decode($json_array); $address_verification_json = $data->shipping_address_verification; $data2 = json_decode($address_verification_json); $postal_code = $data2->postal_code; if ($postal_code < 1) { try { $templateVars = []; $transport = $this->transportBuilder->setTemplateIdentifier('59') ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => 1 ] ) ->setTemplateVars( $templateVars ) ->setFrom( [ "name" => "Magento ABC CHECK PAYMENT", "email" => "[email protected]" ] ) ->addTo('[email protected]') ->setReplyTo('[email protected]') ->getTransport(); $transport->sendMessage(); return $this; } catch (Exception $e) { $this->logger->error($e); } } } } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.