Reputation: 1393
I am trying to create a user using code. I have the following that created the user. It however does not send an email to the user saying that the account has been created. How can I do that?
$newUser = array(
'name' => 'username',
'pass' => 'password', // note: do not md5 the password
'mail' => 'email address',
'status' => 1,
'init' => 'email address'
);
user_save(null, $newUser);
Upvotes: 7
Views: 15175
Reputation: 8862
You can use the standard _user_mail_notify() function from the Drupal core's "User" module.
// Create user.
$new_user = array(
'name' => $username,
'pass' => $password,
'mail' => $email,
'status' => 1,
'init' => $email,
'roles' => array(DRUPAL_AUTHENTICATED_RID => TRUE),
);
$account = user_save(NULL, $new_user);
// Set operation.
$op = 'register_no_approval_required';
// Send an email.
_user_mail_notify($op, $account);
There are different values of $op:
/* @param $op
* The operation being performed on the account. Possible values:
* - 'register_admin_created': Welcome message for user created by the admin.
* - 'register_no_approval_required': Welcome message when user
* self-registers.
* - 'register_pending_approval': Welcome message, user pending admin
* approval.
* - 'password_reset': Password recovery request.
* - 'status_activated': Account activated.
* - 'status_blocked': Account blocked.
* - 'cancel_confirm': Account cancellation request.
* - 'status_canceled': Account canceled.*/
Upvotes: 30
Reputation: 1
The standard way may change your code a bit, like this (change from user_save and add the rest);
$account = user_save('', $newUser); //the first parameter is left blank so a new user is created
// If you want to send the welcome email, use the following code
// Manually set the password so it appears in the e-mail.
$account->password = $newUser['pass'];
// Send the e-mail through the user module.
drupal_mail('user', 'register_no_approval_required',
$email, NULL, array('account' => $account), variable_get('site_mail', '[email protected]'));
Upvotes: 0
Reputation: 952
The above answers pretty much all do the same thing, but jump into the chain at different points; some requiring additional modules; and some referring to system forms.
Personally, while the Rules module can accomplish this for you, it seems a bit contradictory to programmatically create a user, then use the UI to send the notification.
I would opt for using the _user_mail_notify() method and passing the operation you want (register_pending_approval, register_no_approval_required, etc.). This puts you in the chain low enough that you aren't relying on additional modules, but high enough that you're tapping into Drupal registration logic.
Upvotes: 0
Reputation: 5718
implement hook_mail:
function YOURMODULE_mail($key, &$message, $params) {
drupal_set_message('email test');
switch ($key) {
case 'mymail':
// Set headers etc
$message['to'] = '[email protected]';
$message['subject'] = t('Hello');
$message['body'][] = t('Hello @username,', array('@username' => $params['username']));
$message['body'][] = t('The main part of the message.');
break;
}
}
then, use the drupal_mail() function:
$result = drupal_mail('example', 'mymail','[email protected]', 'en', 'params','[email protected]', '[email protected]');
Upvotes: 0
Reputation: 3388
If you want to mimic how Drupal core handles this, take a look at user_register_submit(). That is the function that reacts to the checkbox you mention above, and if notifications are desired, passes the saved user object into _user_mail_notify(), which handles the sending of the message.
Upvotes: 1
Reputation: 11543
You could use Rules. You can add an action to be fired when user is created.
Upvotes: -3
Reputation: 12269
Have you implemented user_register_notify? http://drupal.org/project/user_register_notify
Here are the instructions on how to set it up: http://drupal.org/node/97183/cvs-instructions/HEAD
Upvotes: 2