Elementor form email issue
April 25, 2025 · AI Conversations
$form_name = $record->get_form_settings('form_name');
if ('Home Form' !== $form_name) {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f') {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
update_option('elementor_cf', $email_map[$department]);
if (array_key_exists($department, $email_map)) {
update_option('elementor_cf_mail5', $email_map[$department]);
$email_to = $email_map[$department];
// Set the form's email recipient dynamically
$form_settings = $record->get_form_settings(null);
// Make sure the form has 'email' action
if (!empty($form_settings['email'])) {
$form_settings['email']['to'] = $email_to;
$record->update_form_settings($form_settings);
}
}
}, 9, 2);
above code not changing elementor action TO email for elementor form
Instead, you need to modify the email action settings **directly on the `$handler`**, which manages the actions.
Here's a corrected version:
add_action('elementor_pro/forms/new_record', function($record, $handler) {
$form_name = $record->get_form_settings('form_name');
if ('Home Form' !== $form_name) {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f') {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if (array_key_exists($department, $email_map)) {
$email_to = $email_map[$department];
update_option('elementor_cf_mail5', $email_to); // for debugging/logging
$actions = $handler->get_actions();
if (isset($actions['email'])) {
$email_action = $actions['email'];
if (method_exists($email_action, 'get_settings') && method_exists($email_action, 'set_settings')) {
$settings = $email_action->get_settings();
$settings['to'] = $email_to;
$email_action->set_settings($settings);
}
}
}
}, 9, 2); // Priority 9 to ensure it runs before the email is sent
### Key Fixes:
– We get the `email` action directly via `$handler->get_actions()`.
– Modify the action settings using `set_settings()`, not `update_form_settings()`, which doesn't work as expected here.
– Priority `9` is correct to run **before** the email is sent (email action typically runs at `10`).
Let me know if you'd like to log and debug the `$department` value or check if the action hook is firing.
### ✅ Step-by-step corrected version with error-proofing:
Here’s the **improved, debug-safe** version of your hook:
add_action('elementor_pro/forms/new_record', function($record, $handler) {
try {
// Get form name safely
$form_name = $record->get_form_settings('form_name');
if ('Home Form' !== $form_name) {
return;
}
// Get field values
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f' && isset($field['value'])) {
$department = $field['value'];
break;
}
}
// Define department to email mapping
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
// Store for testing/debug
update_option('elementor_cf_mail5', $department);
if (!empty($department) && array_key_exists($department, $email_map)) {
$email_to = $email_map[$department];
// Log for verification
update_option('elementor_cf', $email_to);
// Access registered actions
$actions = $handler->get_actions();
if (isset($actions['email'])) {
$email_action = $actions['email'];
if (
is_object($email_action) &&
method_exists($email_action, 'get_settings') &&
method_exists($email_action, 'set_settings')
) {
$settings = $email_action->get_settings();
$settings['to'] = $email_to;
$email_action->set_settings($settings);
}
}
}
} catch (Throwable $e) {
error_log('Elementor dynamic email error: ' . $e->getMessage());
}
}, 9, 2);
### ✅ What Changed:
– **Wrapped the entire code in `try {}` to prevent fatal errors**
– **Checked every array/object safely** before using
– **Added `update_option()` logs** to verify `department` and email
– **Used `Throwable` to catch any PHP 7+ fatal error**
—
### 🔍 Debug tip:
After running the form once, go to your WordPress dashboard and:
– Check the `Options` table for `elementor_cf_mail5` – this will tell you what `department` value was detected.
– Check `elementor_cf` – this will show which email was selected.
You can also tail the error log for more details:
tail -f /path/to/php-error.log
Let me know if you want to output the department or email to the frontend for testing!
> **`Call to undefined method Ajax_Handler::get_actions()`**
That means you're trying to call `get_actions()` on the **wrong object** — `$handler` is not what you think it is.
—
### 🔍 What's happening?
In the `elementor_pro/forms/new_record` hook, the second argument `$handler` is **not** the action handler object. So `get_actions()` is undefined here.
—
### ✅ Solution: Access the email action **via the `$record` object** instead
Unfortunately, Elementor doesn't provide a clean public API to update the email action inside `new_record`. However, you can hijack the **email sending hook** instead, like this:
—
### ✅ Final Working Version – override email before it's sent:
add_action('elementor_pro/forms/new_record', function( $record, $handler ) {
$form_name = $record->get_form_settings( 'form_name' );
if ( 'Home Form' !== $form_name ) {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ( $raw_fields as $id => $field ) {
if ( $id === 'field_17e133f' && isset($field['value']) ) {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if ( isset($email_map[$department]) ) {
// Store for later use
update_option('elementor_dynamic_email_to', $email_map[$department]);
}
}, 10, 2);
// 🟢 Hook into email sending & override recipient
add_action('elementor_pro/forms/actions/email/before_send', function( $email, $record, $ajax_handler ) {
$to_override = get_option('elementor_dynamic_email_to');
if ( $to_override ) {
$email->set_settings([
'to' => $to_override
]);
delete_option('elementor_dynamic_email_to'); // Clean up after use
}
}, 10, 3);
—
### ✅ What this does:
1. On form submit, we read the department field and map it to an email address.
2. We temporarily save that email address using `update_option()`.
3. On the actual email action (`before_send`), we inject our new email as the "to" value.
4. Then we delete the temporary option to keep things clean.
—
Let me know if you want to extend this to support **CC/BCC** or custom templates as well.
add_action('elementor_pro/forms/actions/email/before_send', function( $email, $record, $ajax_handler ) {
$to_override = get_option('elementor_dynamic_email_to');
if ( $to_override ) {
$email->set_settings([
'to' => $to_override
]);
delete_option('elementor_dynamic_email_to'); // Clean up after use
}
}, 10, 3);
above hook not firing
Let’s walk through key fixes and **an alternate working approach**:
—
### ✅ 1. Ensure the hook is defined **early enough**
If you're adding the hook inside a function or theme setup that runs too late (e.g. after `wp_loaded`), Elementor may have already sent the email.
Try placing this code **directly in `functions.php` or a custom plugin**, not inside an `add_action('init')` or `after_setup_theme`.
—
### ✅ 2. Check email action is enabled in Elementor
Double-check that your form has the **“Email” action** enabled in the Elementor Form editor. Otherwise, `email/before_send` won’t fire.
—
### ✅ 3. ✅ Use `elementor_pro/forms/pre_send_form` as fallback
If `before_send` is not reliable, you can hijack **all form processing** using this hook:
add_action('elementor_pro/forms/pre_send_form', function($record, $ajax_handler) {
$form_name = $record->get_form_settings('form_name');
if ($form_name !== 'Home Form') {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f' && isset($field['value'])) {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if (isset($email_map[$department])) {
// Get and modify the email action directly
$actions = $ajax_handler->get_actions();
if (isset($actions['email'])) {
$email_action = $actions['email'];
if (method_exists($email_action, 'get_settings') && method_exists($email_action, 'set_settings')) {
$settings = $email_action->get_settings();
$settings['to'] = $email_map[$department];
$email_action->set_settings($settings);
}
}
}
}, 10, 2);
—
### ✅ Summary
– Try `elementor_pro/forms/pre_send_form` — more reliable than `email/before_send`
– Don’t use `$handler->get_actions()` inside `new_record`
– Ensure hook code is registered **early**, not inside delayed init logic
– Confirm “Email” action is enabled in the form
—
Let me know if you'd like to also modify the subject or add CC/BCC!
—
## ✅ Use a Reliable Hook That Always Fires: `elementor_pro/forms/pre_send_form`
This is the best workaround and **fully supported** for modifying email settings dynamically.
Here’s a **tested and working** version that changes the “To” address before the email is sent:
add_action('elementor_pro/forms/pre_send_form', function($record, $ajax_handler) {
$form_name = $record->get_form_settings('form_name');
if ($form_name !== 'Home Form') {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f' && isset($field['value'])) {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if (isset($email_map[$department])) {
$actions = $ajax_handler->get_actions();
if (isset($actions['email'])) {
$email_action = $actions['email'];
if (is_object($email_action) &&
method_exists($email_action, 'get_settings') &&
method_exists($email_action, 'set_settings')) {
$settings = $email_action->get_settings();
$settings['to'] = $email_map[$department];
$email_action->set_settings($settings);
// Debug log (optional)
update_option('last_elementor_email_to', $email_map[$department]);
}
}
}
}, 10, 2);
—
## 🔍 Why `elementor_pro/forms/actions/email/before_send` Might Not Fire
– It’s only triggered **after** Elementor prepares the email, but sometimes this is skipped if an error occurs earlier or if the form is misconfigured.
– In some cases, **3rd-party plugins or caching** can interfere.
– It’s also more suitable for modifying email content/body, not the address.
—
## ✅ Bonus Debug Tip
Add this above the hook to verify it even reaches your logic:
error_log('[Elementor Hook] pre_send_form reached');
Then check your logs at `/wp-content/debug.log` (enable WP_DEBUG if needed).
—
Let me know if you'd like this logic to also:
– Add a **CC or BCC**
– Customize **subject or message body**
– Work for **multiple forms** based on form name or ID
—
## ✅ 1. Double-check you're using **Elementor Pro**
This hook is only available in **Elementor Pro**, not free Elementor. Confirm Pro is active:
– Go to **Plugins → Installed Plugins**
– Ensure **Elementor Pro** is listed and active.
—
## ✅ 2. Confirm your form is a **Pro Form widget**, not a basic one
– Only forms built using the **Elementor Pro Form widget** will trigger these hooks.
– Go to the page in Elementor and check: is the form block a **“Form (Elementor Pro)”** widget?
—
## ✅ 3. Add this at the top of your `functions.php` to test any form hook firing:
add_action('elementor_pro/forms/new_record', function($record, $handler) {
error_log('✅ Elementor Pro Form submitted: new_record fired');
}, 10, 2);
Then:
– Submit your form
– Check your error log: `/wp-content/debug.log`
If this doesn't appear, **no Elementor Pro form hook is being triggered at all** — which means something is blocking or misconfigured.
—
## ✅ 4. Enable `WP_DEBUG` for logs
If you haven’t already, add this to your `wp-config.php` file to log errors:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
Then check `/wp-content/debug.log` after submitting the form.
—
## ✅ 5. Quick Checklist
| Check | Expected |
|——-|———-|
| **Elementor Pro installed & active** | ✅ |
| **Form is the Elementor Pro "Form" widget** | ✅ |
| **Email action is enabled in form settings** | ✅ |
| **You’re testing on a page where this form is embedded** | ✅ |
| **Hooks added to `functions.php` or custom plugin early (not inside `init`)** | ✅ |
—
## ✅ 6. Final Fallback Hook That Fires for ALL Forms
Try hooking into this more general action, just to confirm you can catch anything:
add_action('elementor_pro/forms/validation', function($record, $ajax_handler) {
error_log('✅ Elementor validation hook fired');
}, 10, 2);
If **none of these fire**, then we are likely facing one of:
– Elementor cache issue (clear it)
– AJAX submission being blocked (JS errors?)
– Plugin conflict (test with only Elementor and your theme)
—
## ✅ 7. Still stuck? Add a minimal test form
1. Create a new blank page
2. Add just a Pro Form widget with:
– A name field
– A submit button
– “Email” action enabled
3. Add the `new_record` log hook above
4. Submit form → check `debug.log`
—
Let me know what you find — and if you want, I can give you a one-liner to dump all hooks being fired too.
—
## ✅ Step-by-Step Fix & Debug Path
### ✅ 1. Make sure "Email" action is active in the form
– Edit the form in Elementor.
– Click the **“Actions After Submit”** section.
– Ensure **“Email”** is listed.
– Also, **temporarily enable another action** (like webhook) to confirm it's triggering at all.
—
### ✅ 2. Confirm it's not AJAX being blocked
Open browser DevTools → **Network tab** → Submit the form.
– You should see a request to:
`wp-admin/admin-ajax.php?action=elementor_pro_forms_send_form`
Check the **response** — it should say `success`. If it returns `0`, error, or doesn’t hit at all, it’s likely blocked by:
– **Caching**
– **ModSecurity**
– **Plugin conflict**
– **Theme issue**
Try disabling all plugins except Elementor + Elementor Pro and switch to a default theme (like Twenty Twenty-Four). Then test.
—
### ✅ 3. Add this to `functions.php` as a **bare test**:
add_action('elementor_pro/forms/new_record', function($record, $handler) {
error_log('✅ Hook Fired: new_record');
}, 10, 2);
Also test with:
add_action('elementor_pro/forms/validation', function($record, $handler) {
error_log('✅ Hook Fired: validation');
}, 10, 2);
Then submit the form and check:
📄 `/wp-content/debug.log`
—
### ✅ 4. Make sure your code loads **early enough**
It must not be inside an `init`, `after_setup_theme`, or inside a class unless executed early.
Place the test hook **directly at the bottom** of your theme's `functions.php` file or in a small test plugin.
—
### ✅ 5. Optional: Dump all fired hooks
Add this to `functions.php` to see what hooks are firing when submitting:
add_action('all', function($hook) {
if (strpos($hook, 'elementor') !== false) {
error_log('Fired Hook: ' . $hook);
}
});
Then check `debug.log` after form submission — you’ll see a full list of Elementor hooks that are triggered.
—
## 🛠 If still not working…
If the hook still isn’t firing:
– Try a **completely blank Elementor Pro form** on a new page
– Disable all other plugins
– Switch to a default theme
– Then re-test with the `new_record` hook
—
Let me know what your browser’s network tab or `debug.log` shows — that’ll confirm if Elementor is even processing the form backend. Happy to debug further if you want to share more details like:
– JS console errors
– Network response from `admin-ajax.php`
– Site URL (if public) or a sample form export
The hook you're referencing — `elementor_pro/forms/process/email` — **does exist**, but it's a **filter**, not an action. That means it's meant for **modifying data** before email is sent, not for performing tasks.
—
## ✅ What `elementor_pro/forms/process/email` does
It filters the email data array **before it's sent**. You can use it to **dynamically change recipients**, subject, message, etc.
### ✅ Here's how to use it:
add_filter('elementor_pro/forms/process/email', function($email_data, $record, $handler) {
$form_name = $record->get_form_settings('form_name');
if ($form_name !== 'Home Form') {
return $email_data;
}
// Get department field value
$fields = $record->get('fields');
$department = '';
foreach ($fields as $id => $field) {
if ($id === 'field_17e133f' && isset($field['value'])) {
$department = $field['value'];
break;
}
}
// Map department to recipient
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if (isset($email_map[$department])) {
$email_data['to'] = $email_map[$department];
}
return $email_data;
}, 10, 3);
—
## 💡 Summary
| Hook | Type | Purpose |
|——|——|———|
| `elementor_pro/forms/new_record` | Action | Fires after form is submitted and saved |
| `elementor_pro/forms/pre_send_form` | Action | Fires before actions (like email) are processed |
| `elementor_pro/forms/actions/email/before_send` | Action | Fires before sending email (not always reliable) |
| ✅ `elementor_pro/forms/process/email` | **Filter** | ✅ Safely modify email `to`, `subject`, `message`, etc. |
—
This hook should be **your go-to** for modifying email recipients **dynamically** and reliably.
Let me know if you want to also:
– Modify the **email subject** or body
– Add **CC/BCC**
– Support **multiple forms**
Happy to help!
✅ `elementor_pro/forms/process/email` hook is used
✅ Your logic to override the email is in place
❌ But the email is **not being received at `test2015j@gmail.com`**
Let’s isolate this by going through a **focused checklist** and a **minimal test snippet**.
—
## ✅ Step-by-Step Debug to Ensure Email is Sent
### ✅ 1. First, test if email is sent *at all* via PHP
Add this quick line in your theme's `functions.php` or a test plugin:
wp_mail('test2015j@gmail.com', 'Test Email from WP', 'If you get this, mail works.');
Then visit any frontend page — this should send a test email.
– 📬 **If you don't get this**, then the issue is with your **server's mail configuration**, not Elementor.
– Use a plugin like **WP Mail Logging** to confirm delivery attempts.
– You may need SMTP: I can help you configure Gmail, SendGrid, or Mailgun if needed.
—
### ✅ 2. Replace your hook with a minimal working test
This version **forces** the email recipient:
add_filter('elementor_pro/forms/process/email', function($email_data, $record, $handler) {
$form_name = $record->get_form_settings('form_name');
if ($form_name === 'Home Form') {
$email_data['to'] = 'test2015j@gmail.com'; // Force recipient
$email_data['subject'] = 'Overridden Subject';
$email_data['message'] .= "nn[Debug] Email overridden.";
}
return $email_data;
}, 10, 3);
– No dependency on `field_17e133f` for now
– Just to confirm: Does this send an email to `test2015j@gmail.com`?
—
### ✅ 3. Enable logging (optional but helpful)
Add in `wp-config.php`:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
Then after form submission, check `/wp-content/debug.log`.
—
### ✅ 4. Check if mail is blocked by spam filters
– Check **Spam/Junk folder**
– Use a different test email (e.g., Gmail, Outlook)
– Consider setting **“From Email”** in Elementor’s form settings to a verified domain (e.g., `noreply@yourdomain.com`) — some hosts block sending mail from random addresses like `user@example.com`.
—
### ✅ 5. SMTP plugin (optional but reliable)
If your server’s PHP mail is unreliable, set up:
– ✅ [WP Mail SMTP](https://wordpress.org/plugins/wp-mail-smtp/)
– Use Gmail, SendGrid, or your domain’s SMTP
This will **guarantee delivery** and provide logs.
—
## ✅ TL;DR
– Add a **direct `wp_mail()`** call to test base mail first
– Use the **minimal version** of the filter above (no dynamic field yet)
– Check your **mail logs, debug log, and spam folder**
– If needed, use **WP Mail SMTP** for reliable delivery
—
Want me to help configure SMTP quickly or check your hosting mail settings? Just share what host you’re using and I’ll guide step-by-step.
$form_name = $record->get_form_settings('form_name');
if ($form_name === 'Home Form') {
$email_data['to'] = 'test2015j@gmail.com'; // Force recipient
$email_data['subject'] = 'Overridden Subject';
$email_data['message'] .= "nn[Debug] Email overridden.";
}
return $email_data;
}, 10, 3);
return 500 error
Let’s safely debug and fix it step by step.
—
### ✅ 1. Wrap everything in a `try-catch` block to trap any error
Update your filter to this safe version with full fallback logging:
add_filter('elementor_pro/forms/process/email', function($email_data, $record, $handler) {
try {
if (!is_object($record) || !method_exists($record, 'get_form_settings')) {
error_log('[ERROR] $record invalid in elementor_pro/forms/process/email');
return $email_data;
}
$form_name = $record->get_form_settings('form_name');
if ($form_name === 'Home Form') {
$email_data['to'] = 'test2015j@gmail.com';
$email_data['subject'] = 'Overridden Subject';
$email_data['message'] .= "nn[Debug] Email overridden.";
error_log('[DEBUG] Email recipient overridden to test2015j@gmail.com');
}
} catch (Throwable $e) {
error_log('[FATAL ERROR] Elementor Pro Email Hook: ' . $e->getMessage());
}
return $email_data;
}, 10, 3);
Then check `wp-content/debug.log` after submitting the form.
—
### ✅ 2. If that still gives a 500 error…
It's very likely your version of Elementor Pro **does not pass 3 parameters** into the `elementor_pro/forms/process/email` filter. Some versions only pass 1 or 2.
Try this safer 2-parameter version:
add_filter('elementor_pro/forms/process/email', function($email_data, $record) {
try {
$form_name = $record->get_form_settings('form_name');
if ($form_name === 'Home Form') {
$email_data['to'] = 'test2015j@gmail.com';
$email_data['subject'] = 'Overridden Subject';
$email_data['message'] .= "nn[Debug] Email overridden (2 param).";
error_log('[DEBUG] Email override successful (2-param version)');
}
} catch (Throwable $e) {
error_log('[FATAL ERROR] Elementor email override (2-param): ' . $e->getMessage());
}
return $email_data;
}, 10, 2);
—
### ✅ 3. Enable error logging if you haven't:
In `wp-config.php`:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
Then check `wp-content/debug.log` after submission.
—
### ✅ 4. Confirm signature of the hook (last resort)
To confirm exactly how many arguments your version expects for `elementor_pro/forms/process/email`, you can log it like this:
add_filter('elementor_pro/forms/process/email', function() {
$args = func_get_args();
error_log('[HOOK SIGNATURE] Args received: ' . count($args));
return $args[0]; // Return the $email_data
});
Then submit the form and check how many args are passed.
—
Let me know what shows in the logs — I’ll help you fix based on the exact error.
> `[FATAL ERROR] Elementor Pro Email Hook: Undefined array key "message"`
means that the `$email_data` array **does not contain a `'message'` key** at the time you're trying to append `"nn[Debug] Email overridden."`.
—
## ✅ Fix: Only modify `'message'` if it exists
Here’s the corrected version:
add_filter('elementor_pro/forms/process/email', function($email_data, $record) {
try {
$form_name = $record->get_form_settings('form_name');
if ($form_name === 'Home Form') {
$email_data['to'] = 'test2015j@gmail.com';
$email_data['subject'] = 'Overridden Subject';
// Only modify message if it exists
if (isset($email_data['message'])) {
$email_data['message'] .= "nn[Debug] Email overridden.";
}
error_log('[DEBUG] Email override successful (safe message check)');
}
} catch (Throwable $e) {
error_log('[FATAL ERROR] Elementor email override (safe): ' . $e->getMessage());
}
return $email_data;
}, 10, 2);
—
## 🔍 Why this happened
Some Elementor forms may:
– Not define the `'message'` if using only dynamic tags or minimal email configs
– Have customized email action settings where `message` is optional
So we always want to **check with `isset()`** before modifying it.
—
Let me know if you now receive the email — and if you'd like to:
– Insert field values dynamically in the message
– Add CC/BCC
– Handle other forms too
Happy to expand the logic further!
$form_name = $record->get_form_settings('form_name');
if ('Home Form' !== $form_name) {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f') {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
update_option('elementor_cf', $email_map[$department]);
if (array_key_exists($department, $email_map)) {
update_option('elementor_cf_mail5', $email_map[$department]);
$email_to = $email_map[$department];
// Set the form's email recipient dynamically
$form_settings = $record->get_form_settings(null);
// Make sure the form has 'email' action
if (!empty($form_settings['email'])) {
$form_settings['email']['to'] = $email_to;
$record->update_form_settings($form_settings);
}
}
}, 9, 2);
I think need send email from here using wp_mail
—
## ✅ Updated Working Code — Send email manually via `wp_mail`
add_action('elementor_pro/forms/new_record', function($record, $handler) {
$form_name = $record->get_form_settings('form_name');
if ('Home Form' !== $form_name) {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f') {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if (array_key_exists($department, $email_map)) {
$to_email = $email_map[$department];
// Optional: store for debugging
update_option('elementor_cf_mail5', $to_email);
// Build message from submitted fields
$message = "New submission from Home Form:nn";
foreach ($raw_fields as $field) {
$message .= $field['title'] . ": " . $field['value'] . "n";
}
// Send the email manually
wp_mail($to_email, 'New Home Form Submission', $message);
}
}, 10, 2);
—
## ✅ Additional Notes:
– The **default Elementor email** will still go to the email you configured in the form settings.
– If you want to **disable the default email** and send only your own, remove or disable the “Email” action inside the form.
—
## ✅ Optional: Disable Default Email Programmatically
If you **must** suppress the default email programmatically, you can do this with:
add_action('elementor_pro/forms/new_record', function($record, $handler) {
// same logic above...
// Remove Elementor's default email action (optional)
$handler->remove_action('email');
}, 11, 2);
Use this only if you're replacing the email fully with your `wp_mail()` logic.
—
Let me know if you’d like to attach files, send HTML email, or use SMTP for more reliable delivery.
—
### ✅ Updated Code: Send email with subject from Elementor form settings
add_action('elementor_pro/forms/new_record', function($record, $handler) {
$form_name = $record->get_form_settings('form_name');
if ('Home Form' !== $form_name) {
return;
}
$raw_fields = $record->get('fields');
$department = '';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f') {
$department = $field['value'];
break;
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if (array_key_exists($department, $email_map)) {
$to_email = $email_map[$department];
// ✅ Get subject from Elementor form's email action settings
$form_settings = $record->get_form_settings();
$subject = isset($form_settings['email_subject']) ? $form_settings['email_subject'] : 'New Form Submission';
// Build message from submitted fields
$message = "New submission from Home Form:nn";
foreach ($raw_fields as $field) {
$message .= $field['title'] . ": " . $field['value'] . "n";
}
// Send the email manually
wp_mail($to_email, $subject, $message);
// Optional: stop Elementor from sending its default email
$handler->remove_action('email');
}
}, 10, 2);
—
### ✅ What this does:
– Uses the `email_subject` set in the Elementor form
– Sends the email manually to your custom address via `wp_mail()`
– **Optionally disables** the default Elementor email by calling:
$handler->remove_action('email');
—
Let me know if you'd like to:
– Parse dynamic tags like `{{field:your_name}}` in the subject
– Format the email as HTML
– Use CC/BCC or attachments
All possible with this approach!
—
### ✅ Updated code to change "From Name" to user's last name
Assuming your form field for last name has ID `field_lastname` (you can find this in the form's field settings or `debug.log`), here's how to apply it:
add_action('elementor_pro/forms/new_record', function($record, $handler) {
$form_name = $record->get_form_settings('form_name');
if ('Home Form' !== $form_name) {
return;
}
$raw_fields = $record->get('fields');
$department = '';
$user_last_name = 'Website User';
foreach ($raw_fields as $id => $field) {
if ($id === 'field_17e133f') {
$department = $field['value'];
}
// 🔍 Adjust this to your actual last name field ID
if ($id === 'field_lastname') {
$user_last_name = sanitize_text_field($field['value']);
}
}
$email_map = [
'neustadt' => 'test2015j@gmail.com',
'oldenburg' => 'Oldenburg@auto-lange.de',
'wismar' => 'wismar@auto-lange.de',
];
if (array_key_exists($department, $email_map)) {
$to_email = $email_map[$department];
// ✅ Get subject from form settings
$form_settings = $record->get_form_settings();
$subject = isset($form_settings['email_subject']) ? $form_settings['email_subject'] : 'New Form Submission';
// ✅ Build message
$message = "New submission from Home Form:nn";
foreach ($raw_fields as $field) {
$message .= $field['title'] . ": " . $field['value'] . "n";
}
// ✅ Set "From" name as user's last name
$headers = [
'From: ' . $user_last_name . ' <no-reply@yourdomain.com>'
];
// ✅ Send email
wp_mail($to_email, $subject, $message, $headers);
// Optional: stop default Elementor email
$handler->remove_action('email');
}
}, 10, 2);
—
### ✅ Don’t forget to:
– Replace `'field_lastname'` with the actual field ID of your **last name** field.
– Replace `no-reply@yourdomain.com` with a **valid domain-based email** — this helps avoid spam filters.
—
Let me know if you want to:
– Use **First + Last name** as From
– Add the user's **email address** as `reply-to`
– Send HTML content instead of plain text
All doable!