I have imported a set of user information into drupal website and all the users are active.

Now I need to generate the password for each user and send an email to them to notify them that the account has been created.

I found the code here which shows how to update the password of the user by code.

$account = user_load(array('uid' => $userdata['uid']));
$password = user_password();
$username = $userdata['username'];
$updates = array(
  'pass' => $password,
   'roles' => array(
    <rid> => '<role title>';

user_save($account, $updates);

Now how can I notify the users of their new username and password.

2 Answers 2


You can notify your user by mail using drupal_mail either at the time of updating your password. You arrange your username/password in some template using HTML & send that by help of the above given function drupal_mail. I hope this help. Let me know if do..

  • Thanks it helps.. I have template to send a notification mail. Yes I can do this using the druapl_mail. Commented Apr 15, 2013 at 6:08
  • @GisleHannemyr - My mistake. I have updated the answer.
    – RajeevK
    Commented Apr 15, 2013 at 7:41

Drupal will automatically send out a user with a one time link to set the password when you toggle an account's status from "Blocked" to "Active" (at least this happens when you've set up acoounts so that admin approval is required to create an account - I haven't tested other configurations). This is IMHO better than generating passwords and sending them in cleartext. It also have the advantage of requiring zero PHP programming on your part.

If you want to send out passwords in clear text, the Registration Password Token module provides a token [user:password]. Since the site administrator allowed to customize the text of the e-mail that is sent to the user after registration have completed, you can then edit the welcome email in the admin GUI to include this token. This solution also requires zero PHP programming on your part, but you must install an extra module.

Caveat: Sending passwords with unencrypted email is a security hazard. This is why Drupal does not provide this function out of the box (as noted by senior core developer webchick: http://drupal.org/node/660302#comment-2592510).

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.