How to set a new password using a module? I'm sending out mails to existing users telling them their username and password with tokens.

Because there is no way to access the user's password (fortunately!), I'm simply setting a new password. But It seems not to work at all.

This is what I've tried so far:

// Include the password.inc file to use hashing functions
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

// This is my user account (dummy function)
$account = get_some_user();

// Generates new random password of length 12
$password = user_password(12);

// Prepares the new user object and hashes the password
$edit = array();
$edit['revision'] = 0;
$edit['pass'] = user_hash_password($password);

// Saves the user object
user_save($account, $edit);

// Do stuff with password...


But this does not really work. The password hash gets changed in the $account[pass] field, I've checked that, but I can't login using the new password.

What did I miss?
Okay, I was trying to make it more complicated than it is.

The user_save() function already hashes the password. This works:

// Prepares the user object with new password
$edit = array();
$edit['revision'] = 0;
$edit['pass'] = $password;

// Saves the user object
user_save($account, $edit);


There is no need to include the password.inc and to use user_hash_password().