How do I send an email notification when I create a Drupal user programmatically?

I am trying to create a user using code. I have the following which created the user. However, it does not send the user a message stating that the account has been created. How can i do this?

$newUser = array( 'name' => 'username', 'pass' => 'password', // note: do not md5 the password 'mail' => 'email address', 'status' => 1, 'init' => 'email address' ); user_save(null, $newUser); 
+6
drupal drupal-6
source share
7 answers

You can use Rules . You can add an action that will be launched when a user is created.

-2
source share

You can use the standard _ user_mail_notify () function from the Drupal 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 meanings 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.*/ 
+28
source share

Have you implemented user_register_notify? http://drupal.org/project/user_register_notify

Here are the setup instructions: http://drupal.org/node/97183/cvs-instructions/HEAD

+2
source share

If you want to emulate how Drupal core works, look at user_register_submit () . This is a function that responds to the checkbox mentioned above, and if notifications are desired, passes the stored user object to _ user_mail_notify () , which handles the sending of the message.

+1
source share

implement hook_mail:

 function YOURMODULE_mail($key, &$message, $params) { drupal_set_message('email test'); switch ($key) { case 'mymail': // Set headers etc $message['to'] = ' youremail@email.com '; $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@email.com ', 'en', 'params',' admin@email.com ', ' to@email.com '); 
0
source share

Almost all of the above answers do the same thing, but jump into a chain at different points; some of which require additional modules; and some relate to systemic forms.

Personally, although the Rules module can do this for you, it seems like it's a bit contrary to programmatically creating a user, then using the user interface to send a notification.

I would choose the _ user_mail_notify () method and pass the operation you need (register_pending_approval, register_no_approval_required, etc.). This puts you in a chain low enough to not rely on additional modules, but high enough for you to listen to the Drupal registration logic.

0
source share

The standard way can slightly change your code (for example, change its 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', ' noreply@yourdomain.com ')); 
0
source share

All Articles