联系表格7 wpcf7_before_send_mail挂钩

时间:2019-02-01 01:53:38

标签: php wordpress contact-form-7

使用联系表7,我需要在邮件的“正文”中添加其他内容。根据谷歌和这里的许多搜索,我想到了这一点:

 add_action ('wpcf7_before_send_mail', 'cellarweb_add_to_message', 10, 2);

function cellarweb_add_to_message($additional_mail, $contact_form) {
    $submission = WPCF7_Submission::get_instance();
    $wpcf7      = WPCF7_ContactForm::get_current();
    $extracontent = "<p>This is more text for the message body.</p>";

    $mail         = $wpcf7->prop('mail');
    $mail['mail']['body'].= $extracontent ;
    // Save the email body
            $wpcf7->set_properties(array(
                "mail" => $mail
            ));
    return $wpcf7;
}

我怀疑我没有使用正确的对象,或者没有修改正确的对象。

将文本添加到CF7邮件正文的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我认为您在添加内容时缺少空格。请检查以下代码。

add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );

function wpcf7_add_text_to_mail_body($contact_form){
    $mail = $contact_form->prop( 'mail' );
    $wpcf7      = WPCF7_ContactForm::get_current();
    $submission = WPCF7_Submission::get_instance();
    if ($submission) { 
         $posted_data = $submission->get_posted_data();
    }
    $extracontent = "<p>This is more text for the message body.</p>";
    $mail['body'] .= '<br>';
    $mail['body'] .= $extracontent
    $contact_form->set_properties( array( 'mail' => $mail ) );
}