Last updated on October 18, 2023

Elementor contact form not sending email

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Troubleshoot Elementor contact form email issues.

If you’re experiencing an issue where the Elementor contact form is not sending emails, there are a few potential causes and solutions to consider. One common reason for this problem is that the email configuration on your WordPress site is not properly set up. To resolve this, you can use the wp_mail() function to send emails from your contact form.

Here’s an example code snippet that demonstrates how to use the wp_mail() function to send an email when a contact form is submitted:

function wpsnippets_send_contact_form_email( $form_id ) {
    // Get the form data
    $form_data = $_POST['form_data'];

    // Extract the email address and message from the form data
    $email = $form_data['email'];
    $message = $form_data['message'];

    // Set up the email headers
    $headers = array(
        'Content-Type: text/html; charset=UTF-8',
        'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>',
    );

    // Send the email
    wp_mail( $email, 'Contact Form Submission', $message, $headers );
}
add_action( 'elementor_pro/forms/new_record', 'wpsnippets_send_contact_form_email' );

In this code snippet, we define a custom function wpsnippets_send_contact_form_email() that is hooked into the elementor_pro/forms/new_record action. This action is triggered when a new form record is created in Elementor. Inside the function, we retrieve the form data from the $_POST superglobal variable, extract the email address and message, and set up the email headers. Finally, we use the wp_mail() function to send the email.

You can add this code to your theme’s functions.php file or in a custom plugin. Remember to replace the email field and message field keys (email and message in the example) with the actual field keys used in your Elementor contact form.

By using this code snippet, you can ensure that the contact form submissions are sent via email using the wp_mail() function, which follows the WordPress coding standards.

Examples

Example 1: Using the wp_mail() function to send email from Elementor contact form

This example demonstrates how to use the wp_mail() function to send an email from an Elementor contact form.

function wpsnippets_send_contact_form_email( $form_id ) {
    if ( $form_id == '123' ) {
        $to = 'example@example.com';
        $subject = 'New contact form submission';
        $message = 'This is a new contact form submission.';
        $headers = array(
            'Content-Type: text/html; charset=UTF-8',
        );

        wp_mail( $to, $subject, $message, $headers );
    }
}
add_action( 'elementor_pro/forms/new_record', 'wpsnippets_send_contact_form_email' );

In this code example, we define a custom function wpsnippets_send_contact_form_email() that hooks into the elementor_pro/forms/new_record action. Inside the function, we check if the form ID matches the desired form (in this case, form ID 123). If it does, we set the recipient email address, subject, message, and headers for the email. Finally, we use the wp_mail() function to send the email.

Example 2: Using a custom SMTP plugin to send email from Elementor contact form

This example demonstrates how to use a custom SMTP plugin to send email from an Elementor contact form.

function wpsnippets_send_contact_form_email( $form_id ) {
    if ( $form_id == '123' ) {
        $mailer = WC()->mailer();
        $message = $mailer->wrap_message( 'This is a new contact form submission.', 'Contact Form Submission' );
        $mailer->send( 'example@example.com', 'New contact form submission', $message );
    }
}
add_action( 'elementor_pro/forms/new_record', 'wpsnippets_send_contact_form_email' );

In this code example, we define a custom function wpsnippets_send_contact_form_email() that hooks into the elementor_pro/forms/new_record action. Inside the function, we check if the form ID matches the desired form (in this case, form ID 123). If it does, we use the WooCommerce WC()->mailer() function to get the mailer instance. We then wrap the message with the subject and use the send() method to send the email.

Example 3: Using a third-party email service to send email from Elementor contact form

This example demonstrates how to use a third-party email service, such as SendGrid or Mailgun, to send email from an Elementor contact form.

function wpsnippets_send_contact_form_email( $form_id ) {
    if ( $form_id == '123' ) {
        $to = 'example@example.com';
        $subject = 'New contact form submission';
        $message = 'This is a new contact form submission.';

        $response = wp_remote_post( 'https://api.mailgun.net/v3/example.com/messages', array(
            'body' => array(
                'from' => 'noreply@example.com',
                'to' => $to,
                'subject' => $subject,
                'text' => $message,
            ),
            'headers' => array(
                'Authorization' => 'Basic ' . base64_encode( 'api:key-1234567890' ),
            ),
        ) );
    }
}
add_action( 'elementor_pro/forms/new_record', 'wpsnippets_send_contact_form_email' );

In this code example, we define a custom function wpsnippets_send_contact_form_email() that hooks into the elementor_pro/forms/new_record action. Inside the function, we check if the form ID matches the desired form (in this case, form ID 123). If it does, we set the recipient email address, subject, and message for the email. We then use the wp_remote_post() function to send a POST request to the Mailgun API with the necessary parameters and headers.

Last updated on October 18, 2023. Originally posted on December 20, 2023.

Leave a Reply