Elementor dynamic email issue

User
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' => 'neeraj.tiwari@nethues.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_mail', $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);
}
}
}, 10, 2);
MAIL NOT going here but defult mail set to elementor is received email

Assistant
If the default email set in Elementor is receiving the mail but your dynamic `$record->update_form_settings()` code doesn't change the recipient, it's likely because **Elementor Pro's `new_record` hook is fired *after* the email has already been sent**.

### πŸ” Why this happens
– `elementor_pro/forms/new_record` runs **after the form submission is processed**, including email sending.
– So, updating the `email.to` setting at this point has no effect.

### βœ… Working Solution: Use `elementor_pro/forms/validation` filter

Use the `elementor_pro/forms/validation` filter instead, which runs **before** the email is sent. Here's how you can dynamically change the recipient:

add_filter('elementor_pro/forms/validation', function($record, $ajax_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'   => 'neeraj.tiwari@nethues.com',
        'oldenburg'  => 'Oldenburg@auto-lange.de',
        'wismar'     => 'wismar@auto-lange.de',
    ];

    if (array_key_exists($department, $email_map)) {
        update_option('elementor_cf_mail', $email_map[$department]);

        $form_settings = $record->get_form_settings();
        $form_settings['email']['to'] = $email_map[$department];
        $record->update_form_settings($form_settings);
    }
}, 10, 2);

### βœ… Things to Double Check
– Ensure `field_17e133f` is the correct **ID** of your department field (not the label).
– Elementor email **"Actions After Submit"** β†’ ensure **Email** is enabled and not overridden by another custom handler.
– Clear any Elementor cache or plugin cache if enabled.

Let me know if you want to log debug output or check actual values being captured.

User
now its giving 500 error
Assistant
A 500 error means there's a **server-side PHP error**. Let's go step-by-step to debug and fix it:

### βœ… Step 1: Enable WP Debug Mode (temporarily)
Edit your `wp-config.php` and enable debugging:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false); // Keeps the error hidden on frontend

Then, check the log in:

/wp-content/debug.log

### βœ… Step 2: Common Error Fixes

Here's a refined and **safe version** of your code with error prevention:

add_filter('elementor_pro/forms/validation', function($record, $ajax_handler) {
    try {
        $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'   => 'neeraj.tiwari@nethues.com',
            'oldenburg'  => 'Oldenburg@auto-lange.de',
            'wismar'     => 'wismar@auto-lange.de',
        ];

        if (!empty($department) && isset($email_map[$department])) {
            $email_to = $email_map[$department];
            update_option('elementor_cf_mail', $email_to);

            $form_settings = $record->get_form_settings();
            if (isset($form_settings['email']) && is_array($form_settings['email'])) {
                $form_settings['email']['to'] = $email_to;
                $record->update_form_settings($form_settings);
            }
        }
    } catch (Throwable $e) {
        error_log('Elementor form error: ' . $e->getMessage());
    }
}, 10, 2);

### βœ… Step 3: Things to Check
1. βœ… Is `'field_17e133f'` the correct field ID? If unsure, log or `var_dump($raw_fields);` and submit the form.
2. βœ… Did you leave out a semicolon or bracket in your PHP?
3. βœ… Are you sure `email` is defined in the form settings?

### βœ… Optional: Log debug info
You can add:

error_log(print_r($form_settings, true));
error_log(print_r($raw_fields, true));

To verify what data you’re getting from Elementor.

Once debug log gives an error trace, you can share it here and I’ll pinpoint the exact cause.

Want me to help you find the correct field ID from your form data?

User
[25-Apr-2025 06:53:07 UTC] Elementor form error: Too few arguments to function ElementorProModulesFormsClassesForm_Record::get_form_settings(), 0 passed in /html/autohaus-lange/website-autohaus-lange/web/app/themes/autohaus-lange/functions.php on line 287 and exactly 1 expected
Assistant
Thanks! The error clearly says:

> `Too few arguments to function get_form_settings(), 0 passed … and exactly 1 expected`

### πŸ”§ **The Fix**

You're using this line:

$form_settings = $record->get_form_settings();

But as per the error, **`get_form_settings()` expects 1 argument**, which is usually the full settings array (or `null` is not acceptable in your Elementor version).

Replace:

$form_settings = $record->get_form_settings();

With:

$form_settings = $record->get_form_settings( $record->get( 'form_settings' ) );

### βœ… Final Working Code
Here's the corrected version:

add_filter('elementor_pro/forms/validation', function($record, $ajax_handler) {
    try {
        $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'   => 'neeraj.tiwari@nethues.com',
            'oldenburg'  => 'Oldenburg@auto-lange.de',
            'wismar'     => 'wismar@auto-lange.de',
        ];

        if (!empty($department) && isset($email_map[$department])) {
            $email_to = $email_map[$department];
            update_option('elementor_cf_mail', $email_to);

            $form_settings = $record->get_form_settings($record->get('form_settings'));

            if (isset($form_settings['email']) && is_array($form_settings['email'])) {
                $form_settings['email']['to'] = $email_to;
                $record->update_form_settings($form_settings);
            }
        }
    } catch (Throwable $e) {
        error_log('Elementor form error: ' . $e->getMessage());
    }
}, 10, 2);

Try this and test the form β€” let me know if the emails are now sent to the dynamic recipients based on the department field.

User
[25-Apr-2025 06:55:37 UTC] Elementor form error: Cannot access offset of type array in isset or empty
Assistant
Perfect β€” that’s a common PHP 8.1+ warning: you’re checking `isset($form_settings['email'])` **but `$form_settings` is not guaranteed to be an array**, so it throws:

> **"Cannot access offset of type array in isset or empty"**

### βœ… Fix
First, confirm `$form_settings` is an array **before** accessing keys like `['email']`.

Update this block:

if (isset($form_settings['email']) && is_array($form_settings['email'])) {

to:

if (is_array($form_settings) && isset($form_settings['email']) && is_array($form_settings['email'])) {

### βœ… Updated Full Code (Safe for PHP 8+)

add_filter('elementor_pro/forms/validation', function($record, $ajax_handler) {
    try {
        $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'   => 'neeraj.tiwari@nethues.com',
            'oldenburg'  => 'Oldenburg@auto-lange.de',
            'wismar'     => 'wismar@auto-lange.de',
        ];

        if (!empty($department) && isset($email_map[$department])) {
            $email_to = $email_map[$department];
            update_option('elementor_cf_mail', $email_to);

            $form_settings = $record->get_form_settings($record->get('form_settings'));

            if (is_array($form_settings) && isset($form_settings['email']) && is_array($form_settings['email'])) {
                $form_settings['email']['to'] = $email_to;
                $record->update_form_settings($form_settings);
            }
        }
    } catch (Throwable $e) {
        error_log('Elementor form error: ' . $e->getMessage());
    }
}, 10, 2);

Try submitting the form again. If the issue persists or you're unsure about the `field_17e133f`, I can help log or verify its value too. Want help with that?

User
[25-Apr-2025 06:58:44 UTC] Elementor form error: Cannot access offset of type array in isset or empty
Assistant
Thanks β€” it looks like Elementor's `$record->get_form_settings()` might **not be returning an array**, or maybe even `null`.

Let’s **log the actual value of `$form_settings`** to be sure, then safely handle it.

### βœ… Step 1: Add Debug Logging

Right before the `if` block, insert:

error_log('Form settings: ' . print_r($form_settings, true));

### βœ… Step 2: Add Full Type-Safe Check

Let’s fully guard that section against any invalid structure:

if (
    is_array($form_settings) &&
    array_key_exists('email', $form_settings) &&
    is_array($form_settings['email']) &&
    array_key_exists('to', $form_settings['email'])
) {
    $form_settings['email']['to'] = $email_to;
    $record->update_form_settings($form_settings);
}

### βœ… Final Safe Version

add_filter('elementor_pro/forms/validation', function($record, $ajax_handler) {
    try {
        $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'   => 'neeraj.tiwari@nethues.com',
            'oldenburg'  => 'Oldenburg@auto-lange.de',
            'wismar'     => 'wismar@auto-lange.de',
        ];

        if (!empty($department) && isset($email_map[$department])) {
            $email_to = $email_map[$department];
            update_option('elementor_cf_mail', $email_to);

            $form_settings = $record->get_form_settings($record->get('form_settings'));

            error_log('Form settings: ' . print_r($form_settings, true)); // debug

            if (
                is_array($form_settings) &&
                array_key_exists('email', $form_settings) &&
                is_array($form_settings['email'])
            ) {
                $form_settings['email']['to'] = $email_to;
                $record->update_form_settings($form_settings);
            }
        }
    } catch (Throwable $e) {
        error_log('Elementor form error: ' . $e->getMessage());
    }
}, 10, 2);

Once you test this version:
– βœ… Check if the form is sent to the correct email.
– πŸ“„ Check `/wp-content/debug.log` β€” it will show what `$form_settings` looks like.

Want help analyzing the log output next? Just paste what shows.

Leave a Comment

Your email address will not be published. Required fields are marked *