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 );
}
}
CodePudding user response:
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;
}
// mail it to them using wp_mail.
wp_mail( $posted_data['my-email'], 'Thanks for your enquiry', $msg );
}
}
}