This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
This hook gives you added control over emails. You may customize it to send emails to specific recipients based on certain field selections or to conditionally stop the email from sending.
Usage
add_filter( 'frm_to_email', 'custom_set_email_value', 10, 4 );
Parameters
- $recipients (array)
- $values (array) - contains field objects
- $form_id (string)
- $args (array) - include entry object
Examples
Email Routing
The following code may be used to customize email routing based on a selection in your form. Replace 5 with the ID of your form and replace 4933 with the ID of the email notification. Replace 125 with the ID of a radio/dropdown field. Replace Option 1, Option 2, Option 3, Option 4, and Option 5 with the actual options in your dropdown or radio field.
add_filter('frm_to_email', 'custom_set_email_value', 10, 4);
function custom_set_email_value($recipients, $values, $form_id, $args){
if($form_id == 5 && $args['email_key'] == 4933){ // change 5 to the id of your form and 4933 with the ID of the email
foreach ( $values as $value ) {
if ( $value->field_id != 125 ) continue; //change 125 to the id of the drop-down or radio field
switch ( $value->meta_value ) {
case 'Option 1':
$recipients[] = 'Michael.@example.com';
break;
case 'Option 2' or 'Option 3' or 'Option 4': // change these values to the choices in your field
$recipients[] = 'Abe@example.com';
break;
case 'Option 5':
$recipients[] = 'Bob@example.com';
$recipients[] = 'Carrie@example.com';
break;
default: //this will be used if none of the other cases occur
$recipients[] = 'default@example.com';
break;
}
unset( $field_id, $value );
}
}
return $recipients;
}
Conditionally stop email
Stop the email notification based on the post status. This code will clear the list of email recipients if the status is set to Draft. Note: You need to add a status field to your form for this code to work properly.
add_filter('frm_to_email', 'stop_the_email', 10, 4);
function stop_the_email($recipients, $values, $form_id, $args){
if ( $form_id == 5 && $args['email_key'] == 123 ) { //change 5 to the id of the form and 123 to the ID of your email notification
if ( isset( $_POST['item_meta'][30] ) && $_POST['item_meta'][30] == 'Draft' ) { //change 30 to the ID of the status field
$recipients = array();
}
}
return $recipients;
}