2015-11-04 150 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

相關問題