2015-11-04 53 views
0

我已经使用内容类型创建了一个表单,并创建了一个具有以下代码的自定义模块,用于在表单提交后向管理员发送电子邮件通知。在表格提交后向管理员发送电子邮件通知

<?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', "[email protected]", language_default(), $params); 
drupal_set_message("Sent e-mail to [email protected]"); 

/** 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', "[email protected]", language_default(), $params); 
    drupal_set_message("Sent e-mail to [email protected]"); 

} 

对于模块的工作,我不得不在commet线mail.inc 413,因为我是在....../drupal_folder /包括/ mail.inc接收()的错误消息,调用未定义功能_filter_htmlcorrector。管理员正在收到表格提交后收到的电子邮件。 但问题是,即使在任何类型的行动后,例如管理员正在接收电子邮件的页面重新压缩。 有没有一种方法可以用来在代码中包含一个规则,如果表单已经提交,那么该规则只会设置发送邮件给管理员的限制。而且我还希望模块能够很好地工作,而无需评论mail.inc中包含_filter_htmlcorrector()的行。

+0

呼叫您的形式drupal_mail功能提交处理:) – Elendas

+0

谢谢,但我怎么做, – Tanah

+0

采取看看我的评论下面 – Elendas

回答

0

下面的代码添加到您的模块(假设它被称为“admin_email_notification”):

// 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', "[email protected]", language_default(), $params); 
    drupal_set_message("Sent e-mail to [email protected]"); 
} 
+0

我已经包含了代码,但模块仍然以相同的方式执行,每次刷新页面时都会发送电子邮件以及表单提交。可能是我把代码片段放在了错误的位置上? – Tanah

+0

您可以将模块的代码添加到您的问题中,包括模块的名称和内容类型吗? PS:始终刷新缓存以确保识别新功能。 – Elendas

+0

我在模块中的第一个代码下面添加了代码,仍然没有区别。我的模块名称是admin_email_notification,内容类型名称是posts,然后content_type的机器名称是send_email。 – Tanah

相关问题