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 = 'paul@gmail.com';
$to = 'Paul';
$nameTo = 'paul@gmail.com'';
$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)"} []