drupal中创建hook_user并调用drupal的mail函数发送email实例

本文实例讲述了drupal中创建hook_user并调用drupal的mail函数发送email的方法。分享给大家供大家参考。具体实现方法如下:

如果想在drupal的用户账号里添加一些自己的内容,就要用到hook_user钩子函数。
例如在wedpage.module中添加一个“创建页面的链接”和一个“发送邮件的form”


复制代码代码如下:function wedpage_user($type, &$edit, &$user, $category = NULL){
if ($type == 'view') {
$items['wedpage'] = array(
'value' => l(t('Create your wedding homepage.'), 'wedpage/add'),
);
$items_mail['send_mail'] = array(
'value' => drupal_get_form('wedpage_mail_form'),
);
$wedpage = array(
t('Wedding homepage') => $items,
t('Send email to your friends') => $items_mail,
);
return $wedpage;
}
}
user的钩子函数就设置成功了!
下面创建mail的发送函数


复制代码代码如下:function wedpage_mail_form(){
$form['wedpage_mail'] = array(
'#type' => 'textfield',
'#size' => 60,
'#prefix' => '<table><tr><td>',
'#suffix' => '</td>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
'#prefix' => '<td>',
'#suffix' => '</td></tr></table>',
);
return $form;
}
function wedpage_mail_form_submit($form_id, $form_values){
global $user, $base_url;
if (valid_email_address($form_values['wedpage_mail'])) {
$mailkey = 'wedpage';
$to = $form_values['wedpage_mail'];
$subject = 'wedding web page address';
$url = $base_url.'/ourwedding/'.$user->uid;
$body = $url;
if (drupal_mail($mailkey, $to, $subject, $body)) {
drupal_set_message(t('The mail send successful!'));
}else {
drupal_set_message(t('The mail send failed.'), 'error');
}
}else {
drupal_set_message(t('The e-mail address %mail is not valid.', array('%mail' => $form_values['wpage_mail'])), 'error');
}
}
上面的函数中valid_email_address是一个系统的验证email的函数,drupal_mail函数用于测试邮件是否发送成功!

希望本文所述对大家的drupal二次开发有所帮助。

drupal中创建hook_user并调用drupal的mail函数发送email实例

扫一扫手机访问