I have a module that creates a new user and automatically logs

//set up the user fields
$fields = array(
  'name' => $login,
  'mail' => $to_insert['email'],
  'pass' => user_password(10),
  'status' => 1,
  'init' => $to_insert['email'],
  'timezone' => 'Europe/Paris',
  'roles' => array(
     DRUPAL_AUTHENTICATED_RID => 'authenticated user',

//the first parameter is left blank so a new user is created
$user = user_save('', $fields);

However, I want to create a rule to send the information to the new user. I would use the token [account: one-time-login-url] but it does not work.

What for? How to refresh this token when I create a new user?

Thanks for your help

2 Answers 2


You can handle this very easily in your module. Once you save the account and you have the user object just simply use _user_mail_notify(). As an example:

 _user_mail_notify('password_reset', $account);

If you want to alter the email in any way you can do that with hook_mail_alter() from within your custom module.


The one time login URL token is a special token because it is not implemented by any hook_tokens(). This means that in the list of the tokens implemented by a module, you will never find that token.
It is a token that is expressly generated when some emails are sent to the users, such as:

  • register_no_approval_require
    The users registered an account, and they don't need any approval

  • register_admin_created
    An admin user created an account

  • password_reset
    A user asked for a password reset

  • status_activated
    An account has been activated

From the code you are showing, it seems it's an administrator user creating accounts through your module. In that case, I would use the register_admin_created email, that could be sent using the following code.

$params['account'] = $user;
$language = user_preferred_language($user);
$mail = drupal_mail('user', 'register_admin_created', $user->mail, $language, $params);

The $user variable is the same variable you have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.