• Resolved totallywp

    (@panatapattu)


    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 year, 10 months ago by totallywp. Reason: code formatting
    • This topic was modified 1 year, 10 months ago by totallywp.
    • This topic was modified 1 year, 10 months ago by totallywp.
  • The topic ‘Can I direct Contact Form 7 user submission emails to 3 users in order’ is closed to new replies.