Hi,

I have some lines of code (see below) which create a user programatically from values passed in an URL, and after that, it is supposed to send an email to that user using drupal_mail('user', 'register_no_approval_required', variable_get('site_mail', ini_get('sendmail_from')), language_default(), $params);.

The problem is that the email that the user receives does not contain the username in the greeting (there is only a comma), and the tokens are not replaced... you can see something like
Username:
Password: !password

Is there something wrong with the code? Thanks!

<?php

$data=$_GET;

	// Drupal User module function that generates MD5 hash password
	$pass = user_password();
	$newuser = array(
	  'name' => $data['username'],
	  'mail' => $data['mail'],
	  'status' => 1, // Sets user as Active
	  'pass' => $pass
	);
	$auto_user = user_save('', $newuser);
	$rid = 2; // Assign Role ID > registered user
	$uid = $auto_user->uid;
	db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $uid, $rid);
    // Populates profile values
	$profile = array(
	  'profile_nombre' => $data['nombre'],
	  'profile_apellidos' => $data['apellidos'],
	  'profile_empresa' => $data['empresa'],
	  'profile_direccion' => $data['direccion'],
	  'profile_poblacion' => $data['poblacion'],
	  'profile_provincia' => $data['provincia'],
	  'profile_pais' => 'Spain',
	  'profile_cp' => $data['cp'],
	  'profile_procedencia' => $data['procedencia'],
	 );
	 // Adds details under Personal Information Category set in Profile
	 profile_save_profile($profile, $auto_user, 'Información Personal'); 
	 // Load the new user Object
	 $account = user_load(array('uid' => $auto_user->uid, 'status' => 1));
	 global $user;
	 $user = $account;
	 drupal_set_message(t('You have been authenticated'));
	 db_query("UPDATE {users} SET login = %d WHERE uid = %d", time(), $user->uid);
	 sess_regenerate();
	 // Finally send an Email
	 /*
	 * Mail templates available:
	 * register_no_approval_required
	 * register_admin_created
	 * register_pending_approval - Need to set Status above as 0 if this option is required
	 */
 drupal_mail('user', 'register_no_approval_required', variable_get('site_mail', ini_get('sendmail_from')), language_default(), $params);
          // Set the redirection to where you want
	 drupal_goto('area-confidencial');

?>

Comments

nevets’s picture

Before calling drupal_mail I believe you need

$params = array('account' => $auto_user);
alextronic’s picture

Awesome! That worked... but not 100%

The "password" still says !password... Maybe there's a problem with $pass = user_password();?

Thanks nevets! You nailed it.

intrafusion’s picture

$pass = user_password(); 
$newuser = array(
  'name' => $name,
  'pass' => $pass, // note: do not md5 the password
  'mail' => $mail,
  'status' => 1,
  'init' => $mail,
);           
$user = user_save('', $newuser);
$user->password = $pass; // Add plain text password into user account to generate mail tokens.
_user_mail_notify('register_no_approval_required', $user);
fknxiao’s picture

That's brilliant. Thanks