How to programmatically send additional notification emails in Contact form 7 [closed]

I have a list of emails I have fetched from the WordPress database.

I would like to programmatically send emails to these emails using the contact form 7.

this is my code

function benson_call_before_form_submit( $wpcf7 ){

     $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }

    if( $wpcf7->id() == 2216 || $wpcf7->id() == "2216" ) {
        //$wpcf->skip_mail = true; 
        // we can now send the email here
        $emails = get_mod_and_admin_emails(); // these are emails

        // how do I send email inside here from contact form 7
        // ?????????????????

    }

}

add_action( 'wpcf7_before_send_mail', 'benson_call_before_form_submit' );

function get_mod_and_admin_emails(  ){
    global $bp; 
    $users = get_users( array( 'fields' => array( 'ID', 'user_email' ) ) );
    $emails = ""; 



    foreach($users as $key => $user){
       $user_id = $user->ID;

       if( groups_is_user_mod( $user_id, $bp->groups->current_group->id ) ||  groups_is_user_admin( $user_id, $bp->groups->current_group->id ) ){
           $email = $user->user_email; 

           $emails .=$email.","; 

       }

    }

    return $emails; 
}

2 Answers
2

You can manipulate recipients just before sending out the emails with the hook wpcf7_before_send_mail.

Here is a simple script that adds an extra recipient:

add_action( 'wpcf7_before_send_mail', 'add_my_second_recipient', 10, 1 );

function add_my_second_recipient($instance) {
    $properites = $instance->get_properties();
    // use below part if you want to add recipient based on the submitted data
    /*
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();
    */
    $additional_recipent="test@test.com";
    $properites['mail']['recipient'] .= ', ' . $additional_recipent;
        
    $instance->set_properties($properites);
    return ($instance);
}

Leave a Comment