I have my contact form with dropdown subject and I just need to Dynamically change recipient email
by hook into wpcf7_before_send_mail, get the submitted form data, find the enquiry-type dropdown option and then use an if/else statement to send the notifications to specific email addresses.
Below is the code I need help in finding where to place on my contact form…..
`// hook into wpcf7_before_send_mail
add_action( ‘wpcf7_before_send_mail’, ‘cf7dynamicnotifications’); // Hooking into wpcf7_before_send_mail
function cf7dynamicnotifications($contact_form) // Create our function to be used in the above hook
{
$submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class
$posted_data = $submission->get_posted_data(); // Get all of the submitted form data
/*
* Here are our ‘enquiry-type’ options and associated email address for reference
*
1 Bookings – [email protected]
2 Newsletters – [email protected]
4 default – [email protected]
*/
if( $posted_data[“enquiry-type”] == ‘Bookings’ ) { // If Bookings option is selected
$recipient_email = ‘[email protected]’;
}
elseif($posted_data[“enquiry-type”] == ‘Newsletters’) { // else if Newsletters option is selected
$recipient_email = ‘[email protected]’;
}
elseif($posted_data[“enquiry-type”] == ‘Jobs’) { // else if Jobs option is selected
$recipient_email = ‘[email protected]’;
}
else { // If no dropdown option is selected
$recipient_email = ‘[email protected]’;
}
// set the email address to recipient
$mailProp = $contact_form->get_properties(‘mail’);
$mailProp[‘mail’][‘recipient’] = $recipient_email;
// update the form properties
$contact_form->set_properties(array(‘mail’ => $mailProp[‘mail’]));
}