CF7 dynamic email recipient
April 3, 2025 · AI Conversations
### Step-by-step solution:
#### 📝 1. Create your dropdown field
In your Contact Form 7 form, define a dropdown like this:
<select name="department">
<option value="sales">Sales</option>
<option value="support">Support</option>
<option value="marketing">Marketing</option>
</select>
And include the usual fields like name, email, message, etc.
—
#### 🧠 2. Add this code to your `functions.php` (in your theme or custom plugin)
add_action('wpcf7_before_send_mail', 'custom_cf7_dynamic_recipient');
function custom_cf7_dynamic_recipient($contact_form) {
$submission = WPCF7_Submission::get_instance();
if (!$submission) {
return;
}
$posted_data = $submission->get_posted_data();
// Map dropdown values to email addresses
$email_map = [
'sales' => 'sales@example.com',
'support' => 'support@example.com',
'marketing' => 'marketing@example.com',
];
$selected_department = $posted_data['department'] ?? '';
if (array_key_exists($selected_department, $email_map)) {
$mail = $contact_form->prop('mail');
$mail['recipient'] = $email_map[$selected_department];
$contact_form->set_properties(['mail' => $mail]);
}
}
—
### ✅ Notes
– Make sure the dropdown field name (`department` in this example) exactly matches what's used in the form.
– You can add logging or admin notification if an unrecognized value is selected (just in case).
Would you like to support sending a CC/BCC to specific departments as well, or add conditions based on more than one field?
[select* department "Sales" "Support" "Marketing"]
In this case:
– `select*` is the field type (`*` means required)
– `department` is the **field name** ✅
– The options are "Sales", "Support", and "Marketing"
So, in your PHP code (like when using the `wpcf7_before_send_mail` hook), you'll refer to this as:
$posted_data['department']
To confirm the field name in your form:
1. Go to the **Contact > Contact Forms** in your WP admin.
2. Edit the form.
3. Look at the code — the word **after the field type** is the field name.
Need help grabbing the value of a checkbox, radio, or multi-select field?
—
### ✅ Elementor Forms: Send to different email based on dropdown
Elementor uses the `elementor_pro/forms/new_record` hook.
Here’s how you can do it:
—
### 🧩 1. Make sure your dropdown has a field ID
In your Elementor form:
– Add a dropdown field (let’s say called “Department”).
– Set the **Field ID** to something like `department`.
Your options might look like:
Sales
Support
Marketing
—
### 🧠 2. Add this code to `functions.php` (or a custom plugin)
add_action('elementor_pro/forms/new_record', function($record, $handler) {
// Make sure it's our form
$form_name = $record->get_form_settings('form_name');
if ('Contact Form' !== $form_name) {
return;
}
// Get submitted fields
$raw_fields = $record->get('fields');
$fields = [];
foreach ($raw_fields as $id => $field) {
$fields[$id] = $field['value'];
}
// Get the dropdown value
$department = $fields['department'] ?? '';
// Define email mapping
$email_map = [
'Sales' => 'sales@example.com',
'Support' => 'support@example.com',
'Marketing' => 'marketing@example.com',
];
// If department matches, override the email
if (array_key_exists($department, $email_map)) {
$handler->add_email([
'from' => get_option('admin_email'),
'to' => $email_map[$department],
'subject' => 'New submission from ' . $department,
'message' => 'You have received a new message from the ' . $department . ' form.',
]);
}
}, 10, 2);
—
### 🔁 Optional Adjustments
– Change `'Contact Form'` to match your Elementor form’s **Form Name** (under form settings).
– If you want to send the full message content or include fields like `name`, `message`, etc., you can build a full message using `$fields`.
—
Want to send the **default admin email** *plus* a department-specific email (i.e., both)? Let me know — I can adjust the code for that too.
—
### ✅ Goal
When a user selects a department (e.g., "Sales", "Support") in a dropdown, the form submission goes to a specific email address.
—
### 🧩 Step 1: Set up your Elementor Form
1. **Add a Dropdown field** to your Elementor form.
2. Set the **Field ID** to: `department` (this is important!).
3. Add options like:
Sales
Support
Marketing
4. In the form settings, set a **Form Name** like: `Contact Form`.
—
### 🧠 Step 2: Add the following code to your theme's `functions.php`
add_action('elementor_pro/forms/new_record', function($record, $handler) {
// Change this to match your form name in Elementor
$form_name = $record->get_form_settings('form_name');
if ('Contact Form' !== $form_name) {
return;
}
// Get submitted fields
$raw_fields = $record->get('fields');
$fields = [];
foreach ($raw_fields as $id => $field) {
$fields[$id] = $field['value'];
}
// Get the department field value
$department = $fields['department'] ?? '';
// Map departments to specific email addresses
$email_map = [
'Sales' => 'sales@example.com',
'Support' => 'support@example.com',
'Marketing' => 'marketing@example.com',
];
if (array_key_exists($department, $email_map)) {
$email = $email_map[$department];
// Send email to selected department
$handler->add_email([
'from' => get_option('admin_email'),
'to' => $email,
'subject' => 'New message for ' . $department,
'message' => "You have a new form submission from the {$department} department.nn" . print_r($fields, true),
]);
}
}, 10, 2);
—
### 🔍 Tips
– You can customize the email `message` to include only the fields you want (`$fields['name']`, `$fields['message']`, etc.).
– You can also CC or BCC if needed.
– Don’t forget to replace `'Contact Form'` and email addresses with your actual form name and recipients.
—
Let me know if you want:
– To send one email to admin and a copy to the department
– To include uploaded files or format the email as HTML
Happy to help with that too!
—
### ✅ How to find the **Form Name** in Elementor:
1. **Open your page** with Elementor.
2. **Click on the Form widget** to edit it.
3. In the **left panel**, scroll down to the **"Additional Options"** section.
4. You'll see a field called **"Form Name"**.
📌 Example:
If your form's **Form Name** is set to `Contact Form`, then this line in your PHP code should match:
if ('Contact Form' !== $form_name) {
return;
}
—
### ✏️ If you haven’t set a Form Name:
– Elementor will **default to empty**, and `$form_name` will return `''`.
– In that case, either set a Form Name in the widget **(recommended)** or change your condition to:
if ('' !== $form_name) {
return;
}
But naming your form is best for clarity — especially if you're using more than one form.
—
Let me know if you want to dynamically handle all forms without checking form names or apply rules to multiple forms differently.
### Step-by-Step to Get the Form Name in Elementor:
1. **Edit the Page** where your form is located.
2. **Select the Elementor Form Widget** by clicking on the form.
3. In the **left sidebar**, under the **Content** tab, scroll down to **Form Settings**.
4. There, you'll see a field named **Form Name**. This is the name you'll reference in the code to ensure you're targeting the right form.
—
### Example of Form Name Setting in the Code:
Let’s say your **Form Name** in Elementor is `Contact Form`. The code will look like this:
$form_name = $record->get_form_settings('form_name');
if ('Contact Form' !== $form_name) {
return;
}
In the example above, `'Contact Form'` is the form name. You need to match this value with the actual form name you've set in Elementor.
—
### 🔑 Key Point:
Make sure the form name in your code matches exactly with the one you've set in the Elementor widget.