2

I am creating a site in Drupal 8 and would like to be able to schedule an email to be sent to new users. The email should be sent automatically one week after they have registered.

In Drupal 7, I would have used Rules Scheduler to do this, but this doesn't appear to have been ported to Drupal 8 yet. Could you suggest another way that I could do this?

1

My way to do this is to schedule a cron job at X time. You can check here how to do this https://www.drupal.org/node/23714 or use Ultimate Cron (https://www.drupal.org/project/ultimate_cron).

If you choose to use the Drupal core, implement hook_cron() where you will check the users that are registered by one week ago( table users_field_data, column "created"). Getting all the users then it only remains to send the email. Here is an example of sending email using mail Manager:

$mailManager = \Drupal::service('plugin.manager.mail');
  $module = 'module-name';
  $key = 'key1'; // Replace with Your key
  $to = \Drupal::currentUser()->getEmail();
  $params['message'] = $message; // Create this variable $message with the concret meesage.
  $params['title'] = 'Title example'; // Replace with a title.
  $langcode = \Drupal::currentUser()->getPreferredLangcode();
  $send = true;

  $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
  if ($result['result'] != true) {
    $message = t('There was a problem sending your email notification to @email.', array('@email' => $to));
    drupal_set_message($message, 'error');
    \Drupal::logger('mail-log')->error($message);
    return;
  }

  $message = t('An email notification has been sent to @email ', array('@email' => $to));
  drupal_set_message($message);
  \Drupal::logger('mail-log')->notice($message);

All these need to be integrated into hook_cron() function.

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.