I am currently trying to dynamically add CC email addresses to an Elementor form’s email through the WordPress functions.php file. My client wants me to only add the CC email addresses when a specific value from a drop down / select field is selected. I’ve managed to get it to work but now it adds those CC email addresses to both emails. (Reference: Add CC or BCC email addresses dynamically when submitting a Elementor form, through the WordPress functions.php file)
I use the Elementor Form Actions After Submit Emails in the following ways:
- Email = supplied information is send to the website owner
- Email 2 = a copy of the supplied informi s send to the visitor who filled out the form
Here is the code:
##############################################################################
// Action is called when the forms is submitted and being processed
##############################################################################
add_action( 'elementor_pro/forms/process', 'add_cc_emails_to_submitted_elementor_forms', 10, 2 );
function add_cc_emails_to_submitted_elementor_forms( $record, $handler ) {
// Get the form name
$form_name = $record->get_form_settings( 'form_name' );
// Check which form is being processed
if ( $form_name !== 'FORM NAME') {
return;
}
// Create an array of all the fields used in the form
$raw_fields = $record->get( 'fields' );
// Get the selected drop down value
$drop_down_field_value = $raw_fields['field_name']['value'];
// Set the CC email addresses
if( $drop_down_field_value === 'SOMETHING' ){
// Call the filter to apply the needed CC Emails
add_filter( 'elementor_pro/forms/wp_mail_headers', 'add_cc_emails_to_elementor_form', 10, 1 );
}
}
##############################################################################
// Add CC email addresses to the Email Header
##############################################################################
function add_cc_emails_to_elementor_form( $headers ) {
// Set all the CC email addresses
$headers .= "Cc: [email protected]";
$headers .= "Cc: [email protected]";
return $headers;
}
Is there a way for me to set the headers based on the email that is being send. I only need the CC email addresses added to Email 1 (the one that gets send to the website owner) and not Email 2. The information on the developer docs (https://developers.elementor.com/docs/hooks/forms/) don’t mention anything about it.