Search code examples
phpwordpressformscontact-form-7

Conditional Autoresponder based on Contact Form 7 fields


I'd like to set up an autoresponder based on the fields chosen in the Contact Form 7 form. For instance, I will have the fields of the form change based on the "Job Title" field. This field will have "Job Title 1" "Job Title 2". When the autoresponder is sent, it will only include a message based on the selection of one of the two Job Titles.

Any help would be much appreciated!

Update:

So I tried the suggestion here Conditional auto responder is Contact Form 7.

Here's the code they suggested:

#hook in to wpcf7_mail_sent - this will happen after form is submitted
add_action( 'wpcf7_mail_sent', 'contact_form_autoresponders' ); 

#our autoresponders function
function contact_form_autoresponders( $contact_form ) {

    if( $contact_form->id==1 ){ #your contact form ID - you can find this in contact form 7 settings

        #retrieve the details of the form/post
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();                          

        #set autoresponders based on dropdown choice            
        switch( $posted_data['position'] ){ #your dropdown menu field name
            case 'Media Buyer':
            $msg="This is an auto response for Media Buyer";
            break;

            case 'Agency':
            $msg="This is an auto response for Agency";
            break;

            case 'Business Owner':
            $msg="This is an auto response for Business Owner";
            break;

        }

        #mail it to them
        mail( $posted_data['YourEmail'], 'Thanks for your enquiry', $msg );
    }

}

After trying this, I don't see a response based on the selection of one of the three positions, nor any auto response at all. I'm guessing this will override the Mail 2 function on the CF7 control panel? I've tried it with it checked, and without it checked.


Solution

  • i had the same problem but solved it by changing the switch construct to an if, like this:

    #hook in to wpcf7_mail_sent - this will happen after form is submitted
    add_action( 'wpcf7_mail_sent', 'contact_form_autoresponders' ); 
    
    #our autoresponders function
    function contact_form_autoresponders( $contact_form ) {
    
        if( $contact_form->id==1234 ){ #your contact form ID - you can find this in contact form 7 settings
    
            #retrieve the details of the form/post
            $submission = WPCF7_Submission::get_instance();
            $posted_data = $submission->get_posted_data();                          
    
            #set autoresponders based on dropdown choice            
            if ($posted_data['location']=='California'){
                $msg="California email body goes here";
            } else 
            {$msg="Texas email body goes here`enter code here";}
    
            #mail it to them
            mail( $posted_data['your-email'], 'Thanks for your enquiry', $msg, );
        }