How can I send to multiple Contact Form 7 recipients based on form input? [closed]

No need to write any code, Contact form 7 has features of Additional Headers in the Mail section. In that you just need to write the email’s header inside the Additional headers textbox in Mail(Second Tab) section.

Put this inside the Additional Headers textbox.

Cc: [friend1-email], [friend2-email], [friend3-email], [friend4-email], [friend5-email]

enter image description here

OR

You can alter the email header data by hooks wpcf7_before_send_mail try below code.

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 hook runs before email send, you can modify the form data.

Leave a Comment