Magento – Magento2 Cron job for sending mail not working

cronjobscrontabemail-templatesmagento-cronmagento2.3

I am trying to send an email to a address via cron job but there is no response I tried it using controller it sends the mail correctly.

Vendor\Module\Cron\Mail.php

    <?php

namespace Vendor\Module\Cron;
use Magento\Framework\Mail\Template\TransportBuilder;
class Mail
{   
        /**
    * @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()
    {
        try {
        $templateVars = [];
        $transport = $this->transportBuilder->setTemplateIdentifier('acme_dropshipment_email')
        ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => 1 ] )
        ->setTemplateVars( $templateVars )
        ->setFrom( [ "name" => "rick", "email" => "emailaddress@gmail.com" ] )
        ->addTo('emailaddressofreceiver@gmail.com')
        ->setReplyTo('emailaddress@gmail.com')
        ->getTransport();
        $transport->sendMessage();
        $this->logger->info('Cron Works');
        return $this;   
        } catch (Exception $e) {
            $this->logger->error($e);
        }
    }

}

Vendor\Module\etc\crontab.xml

    <?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="alert_group">
        <job instance="Vendor\Module\Cron\Mail" method="execute" name="vendor_module_cron">
            <schedule>* * * * *</schedule>
        </job>
    </group>
</config>

Vendor\Module\etc\cron_group.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="alert_group">
        <schedule_generate_every>1</schedule_generate_every>
        <schedule_ahead_for>4</schedule_ahead_for>
        <schedule_lifetime>2</schedule_lifetime>
        <history_cleanup_every>10</history_cleanup_every>
        <history_success_lifetime>60</history_success_lifetime>
        <history_failure_lifetime>600</history_failure_lifetime>
        <use_separate_process>1</use_separate_process>
    </group>
</config>

Vendor\Module\etc\email_templates.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="acme_dropshipment_email" label="DropShipment Email" file="dropshipment.html" type="html" module="Vendor_Module" area="frontend"/>
</config>

dropshipment.html

    Hello world :D

Best Answer

This library helps to identify the issues.

Run the below commands in the Magento root directory to download the n98-magerun2.phar.

wget https://files.magerun.net/n98-magerun2.phar

Then trigger your custom cron by running below command.

php n98-magerun2.phar sys:cron:run vendor_module_cron

If your custom cron php has any error, it will throw the exceptions. Use those exception details and fixed it.

I hope it helps.

Related Topic