1

I'm working in Drupal 8.2.4 currently and building a headless webapp using Rest Web Services module for the endpoints I need. I'm using the endpoint /entity/user to create users, however the default welcome emails are not getting sent out by Drupal when I create the users.

I do have the emails set up to be sent properly and I know this because if I create a user like normal in Drupal with "Notify user of new account" checked, it does send the email. Is there some simple way that I am just missing to get this to work?

I have also tried using hook_ENTITY_TYPE_insert using _user_mail_notify to trigger the email to be sent, however this is not working either for what I want. My code is as follows:

/** * Implements hook_ENTITY_TYPE_insert(). * When a new user is created, do things. */ function custom_rules_user_insert(Drupal\Core\Entity\EntityInterface $entity) { _user_mail_notify('register_no_approval_required', $entity); }

Am I doing something wrong, is there a simple way to get this to work or does this just not work at all yet in Drupal 8? I will also need to set up Pass Reset emails, so any feedback would be appreciated.

0

I've tested this and it seems like _user_mail_notify just isn't working currently in 8.2.4, however I did find a workaround using the hook_ENTITY_TYPE_insert() still, but using hook_mail and MailManager also.

/**
 * Implements hook_mail().
 */
function custom_rules_mail($key, &$message, $params) {
$options = array(
    'langcode' => $message['langcode'],
);
 switch ($key) {

   case 'create_user':
     $message['from'] = \Drupal::config('system.site')->get('mail');
     $message['subject'] = t('Welcome to the site!', $options);
     $message['body'][] = $params['message'];
     break;
 }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 * When a new user is created, do things.
 */
function custom_rules_user_insert(Drupal\Core\Entity\EntityInterface $entity)
{
  global $user;

//  _user_mail_notify('register_no_approval_required', $entity);


  $mailManager = \Drupal::service('plugin.manager.mail');
  $module = 'custom_rules';
  $key = 'create_user';
  $to = $entity->getEmail();
  $params['message'] = "Welcome to our site! \r\nYour username is " . $entity->getUsername() . " " . "\r\nIf you would like to reset your password, you can at the following link: \r\n" . user_pass_reset_url($entity);
  $langcode = $entity->getPreferredLangcode();
  $send = true;

  $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
  if ($result['result'] !== true) {
    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.'));
  }

}

The way I have this set up works. When a user is created (through drupal GUI or REST API), they are sent a welcome email that also includes a link to reset their password. To do this, you use the user_pass_reset_url function that is defined here. MailManager was pretty simple to use and figure out.

I followed this blog to figure out how to do this.

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.