Worpress联系表格7 wpcf7_before_send_mail密件抄送字段

问题描述 投票:0回答:1

((对不起,我的英语很抱歉)我刚开始使用wordpress,这是一个简单的代码,可以在wpcf7_before_send_mail的密件抄送字段中写入电子邮件?谢谢(请注意,我必须使用wpcf7_before_send_mail而不是联系表格7中的“邮件”标签)]

wordpress hook contact-form-7
1个回答
0
投票

您可以尝试这个

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)将在发送电子邮件之前运行,因此您也可以修改当前表单数据。

© www.soinside.com 2019 - 2024. All rights reserved.