【问题标题】:Worpress contact form 7 wpcf7_before_send_mail Bcc fieldWordpress 联系表格 7 wpcf7_before_send_mail Bcc 字段
【发布时间】:2019-12-12 21:48:57
【问题描述】:

(对不起我的英语)我刚开始使用 wordpress,这是一个在 wpcf7_before_send_mail 的密件抄送字段中编写电子邮件的简单代码?谢谢你 (附:我必须在联系表格 7 中使用 wpcf7_before_send_mail 而不是邮件选项卡)

【问题讨论】:

  • 为什么不使用wpcf7_mail_components 过滤器?

标签: wordpress hook contact-form-7


【解决方案1】:

你可以试试这个

add_action('wpcf7_before_send_mail','dynamic_addcc');
   function dynamic_addcc($WPCF7_ContactForm){

// Check contact form id.
if (33 == $WPCF7_ContactForm->id()) {

    $currentformInstance  = WPCF7_ContactForm::get_current();
    $contactformsubmition = WPCF7_Submission::get_instance();

    if ($contactformsubmition) {

        $cc_email = array();

        /* -------------- */
        // replace with your email field's names
        if(is_email($_POST['friend1-email'])){
            array_push($cc_email, $_POST['friend1-email']);
        }
        if(is_email($_POST['friend2-email'])){
            array_push($cc_email, $_POST['friend2-email']);
        }
        /* -------------- */

        // saparate all emails by comma.
        $cclist = implode(', ',$cc_email);

        $data = $contactformsubmition->get_posted_data();

        if (empty($data))
            return;

        $mail = $currentformInstance->prop('mail');

        if(!empty($cclist)){
            $mail['additional_headers'] = "Cc: $cclist";
        }

        // Save the email body
        $currentformInstance->set_properties(array(
            "mail" => $mail
        ));

        // return current cf7 instance
        return $currentformInstance;
    } 
}
}

这个钩子(wpcf7_before_send_mail)会在邮件发送之前运行,所以你也可以修改当前的表单数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2012-09-13
    • 2017-01-12
    相关资源
    最近更新 更多