1

Attempting to achieve conditional auto-responses for contact form 7, depending on what's written in an input field. This thread (Conditional auto responder is Contact Form 7) suggested a solution, but implementing the code via the "snippets" plugin doesn't seem to work - no mail response is sent out.

If possible, please advise on how to implement the following code with cf7. Thanks,

#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            
        switch( $posted_data['location'] ){ #your dropdown menu field name
            case 'California':
            $msg="California email body goes here";
            break;

            case 'Texas':
            $msg="Texas email body goes here";
            break;

        }

        #mail it to them
        mail( $posted_data['your-email'], 'Thanks for your enquiry', $msg );
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
wssupport
  • 23
  • 4

1 Answers1

1

The data stored from dropdowns is by default an array. With that being the case, you were close. However, you should also use wp_mail rather than mail

add_action( 'wpcf7_mail_sent', 'contact_form_autoresponders' );

function contact_form_autoresponders( $contact_form ) {
    // The contact form ID.
    if ( 1234 === $contact_form->id ) {
        $submission  = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();
        // Dropdowns are stored as arrays.
        if ( isset( $posted_data['location'] ) ) {
            switch ( $posted_data['location'][0] ) {
                case 'California':
                    $msg = 'California email body goes here';
                    break;
                case 'Texas':
                    $msg = 'Texas email body goes here';
                    break;
                default:
                    $msg = 'Unfortunately, that location is not available';
            }
            // mail it to them using wp_mail.
            wp_mail( $posted_data['my-email'], 'Thanks for your enquiry', $msg );
        }
    }
}
Howard E
  • 5,454
  • 3
  • 15
  • 24
  • Works great! Thank you. How could I make this work for a text or email (contact form) field as opposed to a dropdown? – wssupport Mar 11 '22 at 15:48
  • Is there a way to insert a generic message that will appear before each $msg? Example "Thanks, you have selected the location: $msg". In addition, could one implement a if else declaration so that anything else written in the input which is not specified will return "Unfortunately, that location is not available"? Thanks again! – wssupport Mar 13 '22 at 09:08
  • Hello. You can simply add a `default:` which would be for all other cases to provide your message. I've updated the answer. – Howard E Mar 13 '22 at 11:36
  • Don't forget you can also upvote the answer if you find it helpful. – Howard E Mar 13 '22 at 11:36