Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 631 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I send an email notification when programatically creating a Drupal user?

#1
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);
Reply

#2
Have you implemented user_register_notify?

[To see links please register here]


Here are the instructions on how to set it up:

[To see links please register here]

Reply

#3
You could use [Rules][1]. You can add an action to be fired when user is created.


[1]:

[To see links please register here]

Reply

#4
If you want to mimic how Drupal core handles this, take a look at [user_register_submit()][1]. 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()][2], which handles the sending of the message.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
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]');
Reply

#6
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()][1] 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.


[1]:

[To see links please register here]

Reply

#7
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]'));
Reply

#8
You can use the standard [_user_mail_notify()][1] 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.*/


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through