Hook Cron Problem

Hi Drupal friends,

I created a module that should send an email notification each time the CRON runs, but the email is never sent.

But, if I replace the hook_cron by a hook_entity_insert it works perfectly every time I create an entity. It should be the same logic, but I don’t understand why it’s not working.

Any Idea ?

Thank you in advance for your answers !

Az

Here’s my code:

/**
* Implements hook_mail().
*/
function demomail_mail($key, &$message, $params) {
$options = array(
‘langcode’ => $message[‘langcode’],
);

switch ($key) {
case ‘create_email’:
$message[‘from’] = Drupal::config(‘system.site’)->get(‘mail’);
$message[‘subject’] = t(‘Article created: @title’, array(‘@title’ => $params[‘node_title’]), $options);
$message[‘body’] = ‘CRON’;
break;
}
}

/**
* Implements hook_cron().
*/
function demomail_cron() {

$mailManager = Drupal::service(‘plugin.manager.mail’);

$module = ‘demomail’;
$key = ‘create_email’;
$to = Drupal::currentUser()->getEmail();
$params[‘message’] = ‘toto’;
$params[‘node_title’] = ‘toto’;
$langcode = Drupal::currentUser()->getPreferredLangcode();
$send = true;

$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

if ($result[‘result’] !== 1) {
drupal_set_message(t(‘There was a problem sending your message and it was not sent.’), ‘error’);
}
else {
drupal_set_message(t(‘Your message has been sent.’));
}

}

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed