Hi all, I am having an issue with changing user email programmatically that causes the user password to be somehow corrupted and subsequently user cannot login. Kindly, allow me to explain.

I have a PHP file that allow the change of the current login user to change his/her email address:
global $user;
$user = user_load($user->uid);
$user->mail = 'user@abc.com';
user_save((object) array('uid' => $user->uid), (array) $user);

After I run this PHP file, the email was changed successfully but it has a very bad side effect. Upon logging off. The user cannot login any more. Somehow the password was changed. I had to reset the password using an Admin account.

Should anyone have any pointers, I'd be very much grateful.

Thanks very much in advance.



ccheu’s picture

Status: Active » Fixed

Guys, thanks to anyone who has attempted to look into this issue. I've managed to solve this problem by using Entity API instead. The following codes did the job. Hope this helps someone who is facing the same issue. Cheers!

$user_entity = entity_metadata_wrapper('user', $user);
$user_entity->mail = 'user@abc.com';

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.