在表单提交后向管理员发送电子邮件通知

时间:2015-11-04 12:13:01

标签: drupal

我已使用内容类型创建了一个表单,并创建了一个自定义模块,其中包含以下代码,以便在提交表单后向管理员发送电子邮件通知。

<?php


$params = array(
    'subject' => "New submission",
    'body' => "<p>Hello world</p>",
    );

//send out the e-mail,
drupal_mail('admin_email_notification', 'admin_email_notification_example', "admin@gmail.com", language_default(), $params);
drupal_set_message("Sent e-mail to admin@gmail.com"); 

/** Implementing the hook_mail()**/

function admin_email_notification_mail($key, &$message, $params)
{
    //Grab the subject and body from params and add it to the messsage.
    $message['subject'] = $params['subject'];
    $message['body'][] = $params['body'];

    switch($key)
    {
        case "admin_email_notification_example":

        break;
    }
}


function admin_email_notification_FORM_ID_alter(&$form, &$form_state, $form_id)
{
    t($form['#submit'], 'admin_email_notification_custom_submission');
}

function admin_email_notification_custom_submission(&$form, $form_state)
{
    $params = array(
    'subject' => "New submission",
    'body' => "<p>Hello world</p>",
    );

    //send out the e-mail,
    drupal_mail('admin_email_notification', 'admin_email_notification_example', "admin@gmail.com", language_default(), $params);
    drupal_set_message("Sent e-mail to admin@gmail.com"); 

}

要使模块工作,我必须在mail.inc中使用com3第413行,因为我收到一条错误消息,说明在/ drupal_folder / includes / mail.inc中调用未定义函数_filter_htmlcorrector()。管理员现在在表单提交后收到电子邮件。 但问题是甚至在任何类型的操作之后,例如,管理员正在接收电子邮件的页面的重新压缩。 有没有一种方法可以用来在代码中包含一个规则,只有在提交表单时才设置向管理员发送邮件的限制。而且我还希望模块能够很好地工作,而不会在mail.inc中注释包含_filter_htmlcorrector()的行。

1 个答案:

答案 0 :(得分:0)

将以下代码添加到您的模块中(假设它被称为&#39; admin_email_notification&#39;):

// First, add an extra submit function to your node/add form, where FORM_ID is the id of your form.
function admin_email_notification_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  array_unshift($form['#submit'], 'admin_email_notification_custom_submission');
}

// Next, the extra submit function, where you can, for example, send your mails
function admin_email_notification_custom_submission(&$form, $form_state) {
  $params = array(
    'subject' => "New submission",
    'body' => "<p>Hello world</p>",
    );

  //send out the e-mail,
  drupal_mail('admin_email_notification', 'admin_email_notification_example', "admin@gmail.com", language_default(), $params);
  drupal_set_message("Sent e-mail to admin@gmail.com"); 
}