Hi! Came to share a solution that might be helpful for you.
I needed to send a Bricks form to different recipients based on a select field selected value.
For example, if you have a “subject” field, and you based on that subject you want to sent the form notification to a sales team, or a tech support, or to hr… this is the way.
I used custom action for this one.
It has 3 parts:
- Define the recipient emails based on select field values:
- Set the dynamic subject based on the select field (optional)
- Build the nofitication email content (customize it, it depends on what fields you use)
- Send the email
It’s pretty code heavy, make sure to customize all the fields (sender email, subject, etc etc)
Here is the code:
<?php
/**
* Bricks Builder – Custom Form Action: Send Conditional Email with HTML Content
*
* This function allows you to send an email to different recipients based on the selected value
* in a Bricks form select field. It also builds a formatted HTML message using submitted form data.
*
* HOW TO USE:
* 1. Replace the field IDs with the actual ones from your form.
* 2. Replace the conditional logic and email addresses with your own rules.
* 3. Attach this code to your theme’s functions.php or a custom plugin.
*/
function my_custom_form_email_action( $form ) {
// 1) Get submitted data and form settings
$fields = $form->get_fields();
$settings = $form->get_settings();
// 2) Define recipient based on a specific select field
$selected_option = $fields['form-field-SELECT_FIELD_ID'] ?? ''; // <-- Replace SELECT_FIELD_ID with your actual field ID
if ( $selected_option === 'option_1_value' || $selected_option === 'option_2_value' ) {
$to = 'example1@yourdomain.com'; // <-- Change this email to the desired recipient
} else {
$to = 'example2@yourdomain.com'; // <-- Change this email to the alternative recipient
}
// 3) Use the selected option's LABEL as the email subject
$subject_label = '';
if ( ! empty( $settings['fields'] ) && is_array( $settings['fields'] ) ) {
foreach ( $settings['fields'] as $field ) {
if ( $field['id'] === 'SELECT_FIELD_ID' && ! empty( $field['options'] ) ) { // <-- Replace SELECT_FIELD_ID
foreach ( $field['options'] as $option ) {
if ( $option['value'] === $selected_option ) {
$subject_label = $option['label'];
break 2;
}
}
}
}
}
// Fallback subject if no label is found
if ( ! $subject_label ) {
$subject_label = ucwords( str_replace( ['-', '_'], ' ', $selected_option ) );
}
$subject = sprintf( 'Contact Form Submission - %s', $subject_label );
// 4) Build HTML message using submitted fields
$name = esc_html( $fields['form-field-NAME_FIELD_ID'] ?? '' ); // <-- Replace with actual name field ID
$email = esc_html( $fields['form-field-EMAIL_FIELD_ID'] ?? '' ); // <-- Replace with actual email field ID
$company = esc_html( $fields['form-field-COMPANY_FIELD_ID'] ?? '' ); // <-- Replace with actual company field ID
$phone = esc_html( $fields['form-field-PHONE_FIELD_ID'] ?? '' ); // <-- Replace with actual phone field ID
$message = esc_html( $fields['form-field-MESSAGE_FIELD_ID'] ?? '' ); // <-- Replace with actual message field ID
$email_body = '<b>Name:</b><br>' . $name . '<br><br>';
$email_body .= '<b>Email:</b><br>' . $email . '<br><br>';
$email_body .= '<b>Company:</b><br>' . $company . '<br><br>';
$email_body .= '<b>Phone:</b><br>' . $phone . '<br><br>';
$email_body .= '<b>Message:</b><br>' . $message;
// 5) Email headers
$sender = 'youremail@example.com' ;
$blog_name = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
$headers = [
"From: {$blog_name} <{$sender}>",
"Reply-To: {$email}",
"Content-Type: text/html; charset=UTF-8",
];
// 6) Send the email and return the result to Bricks
$sent = wp_mail( $to, $subject, $email_body, $headers );
if ( $sent ) {
$form->set_result([
'action' => 'my_custom_action',
'type' => 'success',
'message' => esc_html__( 'Thank you! We will get back to you shortly.', 'bricks' ),
]);
} else {
$form->set_result([
'action' => 'my_custom_action',
'type' => 'error',
'message' => esc_html__( 'Oops! Something went wrong. Please try again later.', 'bricks' ),
]);
}
}
add_action( 'bricks/form/custom_action', 'my_custom_form_email_action', 10, 1 );
Hope it helps!