Reputation: 143
I'm creating a drupal user account programmatically, code is below. Everything seems to work with the exception of Drupal following the standard user creation rules, mainly sending an email to confirm the email address. So, the user is created in Drupal, but the confirmation email never gets sent.
Email works fine in the installation. If I use the standard registration form, the user gets the email as expected and can finish the registration process.
Any tips on what I need to do to get user_save to know to send the confirmation email?
function _webform_submit($form, &$form_state) {
$data = $form_state['values']['submitted_tree'];
drupal_set_message('Data is: <pre>' . print_r($data, TRUE) . '</pre>');
if (array_key_exists('create_user', $data)) {
drupal_set_message('Found create_user.');
$name = $data['email'];
$account_id = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $name))->fetchField();
$account = new stdClass();
if ($account_id) {
//$account->uid = $account_id;
drupal_set_message("Found existing user");
}
else {
$pass = user_password();
drupal_set_message("Creating a new user: " . $name . " with pw: " . $pass );
$account->is_new = TRUE;
$account->status = TRUE;
foreach($data as $key => $value) {
//If this is an array, likely checkboxes.
if (gettype($value) == 'array') {
foreach($value as $arr_key => $arr_val) {
$edit[$arr_val]['und'][0]['value'] = '1';
}
}
else {
$edit[$key]['und'][0]['value'] = $value;
}
}
$edit['name'] = $name;
//$edit['pass'] = $pass;
$edit['mail'] = $name;
$edit['status'] = 1;
$edit['init'] = $name;
user_save($account, $edit);
}
}
}
Upvotes: 1
Views: 1112
Reputation: 143
Found the answer - user_save doesn't handle any of the notification, you need to handle it directly. I used:
_user_mail_notify('register_no_approval_required', user_save($account, $edit));
Upvotes: 3