Can I direct Contact Form 7 user submission emails to 3 users in order

[ad_1]

I want to direct Contact Form 7 user submission emails to 3 users in order. First email submission to the first user, second email submission to the second user, and third email submission to the third user. Fourth email submission to the first user, fifth email submission to the second user again likewise.

Currently, I came across this solution and it always getting the sent email to the first user.

//Define the key to store in the database
define( 'CF7_COUNTER', 'cf7-counter' );

function cf7_form_counter() {
    $wpcf7 = WPCF7_ContactForm::get_current();
    $form_id = $wpcf7->id;

    if ($form_id === '280'):
        $val = get_option( CF7_COUNTER, 0) + 1;
        return $val;
    endif;
}
add_shortcode('CF7_counter', 'cf7_form_counter');

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    $wpcf7 = WPCF7_ContactForm::get_current();
    $form_id = $wpcf7->id;

    if ($form_id === '280'):
        $recipients = array( '[email protected]', '[email protected]', '[email protected]' );
        $recipient_index = $val % count( $recipients );
        $recipient = $recipients[$recipient_index];

        $properties = $contact_form->get_properties();
        $properties['mail']['recipient'] = $recipient;
        $contact_form->set_properties($properties);

        return $contact_form;
    endif;

}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
 
//Action performed when the mail is actually sent by CF7
function cf7_form_increment_mail_counter(){
    $wpcf7 = WPCF7_ContactForm::get_current();
    $form_id = $wpcf7->id;

    if ($form_id === '280'):
        $val = get_option( CF7_COUNTER, 0) + 1;
        update_option(CF7_COUNTER, $val);
    endif;
}
add_action('wpcf7_mail_sent', 'cf7_form_increment_mail_counter');
  • This topic was modified 1 hour, 15 minutes ago by totallywp. Reason: code formatting
  • This topic was modified 28 minutes ago by totallywp.
  • This topic was modified 23 minutes ago by totallywp.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer