skip to Main Content

How do I add a CC/BCC to a WordPress using Contact Form 7 using the Contact Form 7 hook?

add_action( 'wpcf7_before_send_mail', 'dynamic_addcc' );

2

Answers


  1. Chosen as BEST ANSWER

    This is how, I commented where possible.

    /**
     * Add Cc & Bcc to all WordPress mails
     * @author: Archie M
     * 
     */
    function dynamic_addcc($WPCF7_ContactForm){
    
        $currentformInstance  = WPCF7_ContactForm::get_current();
        $contactformsubmition = WPCF7_Submission::get_instance();
    
        $form_id = $currentformInstance->id;
    
        // If you don't want this on every form, use the form ID instead
        if ($contactformsubmition && ($form_id === 100) ) {
    
            $bcc_email = array('[email protected]');  // CC or BCC
    
            //separate all emails by comma if more than one
            $cclist = implode(', ',$bcc_email);
    
            $data = $contactformsubmition->get_posted_data();
    
            if (empty($data))
                return;
    
            $mail = $currentformInstance->prop('mail');
    
            if(!empty($cclist)){
                $mail['additional_headers'] = "Bcc: $cclist";
            }
    
            // Save the email body
            $currentformInstance->set_properties(array(
                "mail" => $mail
            ));
    
            // return current cf7 instance
            return $currentformInstance;
        }
    
    }
    add_action( 'wpcf7_before_send_mail', 'dynamic_addcc' );
    

  2. If you login to your WordPress admin and click on the Contact icon on the far left, under the dashboard.

    Once you’ve done that if you click on the Contact Form button, and then click on the Mail tab.

    Under the mail tab you will see an "Additional headers" box. In the additional headers box, you can enter the CC and BCC e-mail addresses.

    Details on this:

    https://michael-shore.uk/blog/add-cc-and-bcc-to-a-contact-form-7-form/

    Or you could try:

    add_action('wpcf7_before_send_mail','dynamic_addcc');
    
    function dynamic_addcc($WPCF7_ContactForm){
    
    // Check contact form id.
    if (12345 == $WPCF7_ContactForm->id()) {
    
        $currentformInstance  = WPCF7_ContactForm::get_current();
        $contactformsubmition = WPCF7_Submission::get_instance();
    
        if ($contactformsubmition) {
    
            $bcc_email = array('[email protected]', '[email protected]', '[email protected]');
    
            // saparate all emails by comma.
            $cclist = implode(', ',$bcc_email);
    
            $data = $contactformsubmition->get_posted_data();
    
            if (empty($data))
                return;
    
            $mail = $currentformInstance->prop('mail');
    
            if(!empty($cclist)){
                $mail['additional_headers'] = "Bcc: $cclist";
            }
    
            // Save the email body
            $currentformInstance->set_properties(array(
                "mail" => $mail
            ));
    
            // return current cf7 instance
            return $currentformInstance;
        }
    }
    
    
    }
    

    Hope you find this useful.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search