我正在尝试使用代码创建用户。我有以下创建用户。但是,它不会向用户发送电子邮件,说明该帐户已创建。我怎么能这样做?
$newUser = array(
'name' => 'username',
'pass' => 'password', // note: do not md5 the password
'mail' => 'email address',
'status' => 1,
'init' => 'email address'
);
user_save(null, $newUser);
答案 0 :(得分:28)
您可以使用Drupal核心的“用户”模块中的标准_user_mail_notify()功能。
// 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);
$ 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 :(得分:2)
您是否实施了user_register_notify? http://drupal.org/project/user_register_notify
以下是有关如何设置的说明:http://drupal.org/node/97183/cvs-instructions/HEAD
答案 2 :(得分:1)
如果您想模仿Drupal核心如何处理此问题,请查看user_register_submit()。这是对上面提到的复选框做出反应的函数,如果需要通知,则将保存的用户对象传递给_user_mail_notify(),用于处理邮件的发送。
答案 3 :(得分:0)
实现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;
}
}
然后,使用drupal_mail()函数:
$result = drupal_mail('example', 'mymail','email@email.com', 'en', 'params','admin@email.com', 'to@email.com');
答案 4 :(得分:0)
上述答案几乎都做同样的事情,但是在不同的点上跳进了链条;一些需要额外的模块;有些是指系统表格。
就个人而言,虽然规则模块可以为您完成此任务,但以编程方式创建用户似乎有点矛盾,然后使用UI发送通知。
我会选择使用_user_mail_notify()方法并传递您想要的操作(register_pending_approval,register_no_approval_required等)。这使您处于链条中足够低,以至于您不依赖于其他模块,但又足够高,以至于您正在使用Drupal注册逻辑。
答案 5 :(得分:0)
标准方式可能会改变您的代码,比如这样(从user_save更改并添加其余代码);
$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'));
答案 6 :(得分:-3)
您可以使用Rules。您可以添加在创建用户时触发的操作。