0

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
0

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.

0

I recommend that you put your email logic into a helper and load that helper via a console command. Once you've confirmed the email is sending and using the template (and the template looks good) then load the helper via a cron process and trigger a send.

Cron and console will use same state.

IMHO a slight different approach on this makes your life easier.

But a quick scan over your code and it looks logical. No obvious errors.

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.