2026-07-15

How to send e-mail from form in wordpress

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

How to send e-mail from form in wordpress

How to Send E-mails from a Form in WordPress: A Complete Guide for Beginners

Welcome to the world of WordPress development! It’s completely understandable that you are looking to connect your form submissions to actual emails. This is one of the most fundamental and useful skills in web development. As a senior developer, I can tell you that sending emails reliably from a web form involves more than just writing HTML; it requires understanding the interaction between front-end (HTML/JavaScript) and back-end (PHP) logic, along with crucial security considerations.

This guide will walk you through the entire process, taking the code snippet you provided and turning it into a functional system that sends emails successfully.

Understanding the Foundation: HTML Forms vs. PHP Processing

The initial code you shared sets up the structure for your form (the front-end). This part tells the user what to enter. However, simply having an HTML form does nothing by itself; it only collects data on the client side. To actually send that data as an email, we need server-side scripting, which is where PHP comes in.

The key steps are:

  1. HTML Form: Collect user input and ensure the name attributes match what your PHP script expects.
  2. PHP Handling: Create a PHP script to check if the form has been submitted ($_POST), sanitize the data, and then use a function to dispatch the email.

Let's refine your provided structure first, focusing on clean naming conventions for better processing.

<form id="contactForm" method="POST" action=""> 
    <!-- Note: We need an action URL to specify where the form submits -->

    <div>
        <label for="name">Name:</label>
        <input type="text" name="user_name" id="name">
    </div>
    <div>
        <label for="email">Email:</label>
        <input type="email" name="user_email" id="email">
    </div>
    <div>
        <label for="phone">Phone:</label>
        <input type="text" name="user_phone" id="phone">
    </div>
    <div>
        <label for="message">Message:</label>
        <textarea name="user_message" id="message"></textarea>
    </div>
    <input type="submit" value="SEND" id="btn"> 
</form>

Notice how I used standard <label> tags and consistent name attributes (user_name, user_email, etc.). These names are what your PHP script will use to retrieve the data.

The Back-End Magic: Processing the Submission with PHP

The most critical part is the PHP code that runs on your WordPress server, which processes the submitted data. This logic must be placed in a separate PHP file (or within your theme's functions.php if you are using a custom plugin setup).

Here is a sample PHP script demonstrating how to capture the data and send an email using PHP's built-in wp_mail() function, which is natively supported by WordPress:

<?php
if (isset($_POST['submit_form'])) {
    // 1. Collect and Sanitize Input
    $name = sanitize_text_field($_POST['user_name']);
    $email = sanitize_email($_POST['user_email']);
    $phone = sanitize_text_field($_POST['user_phone']);
    $message = sanitize_textarea_field($_POST['user_message']);

    // 2. Define recipients and subject
    $to = 'your_email@example.com'; // Change this to where you want to receive the email
    $subject = 'New Form Submission from Website';
    
    // 3. Construct the email body
    $email_body = "Name: " . $name . "\n";
    $email_body .= "Email: " . $email . "\n";
    $email_body .= "Phone: " . $phone . "\n\n";
    $email_body .= "Message:\n" . $message;

    // 4. Send the email using WordPress function
    $headers = array('Content-Type: text/plain; charset=UTF-8');
    
    if (wp_mail($to, $subject, $email_body, $headers)) {
        echo "Email successfully sent!";
    } else {
        echo "Error: Failed to send the email.";
    }
}
?>

Essential Best Practices for Secure Email Sending

As a developer, security is non-negotiable, especially when dealing with user input and sending sensitive data.

  1. Sanitization is Key: Notice the use of functions like sanitize_text_field() and sanitize_email(). These functions are vital because they strip out potentially malicious code (like <script> tags) from the user input before you store it or use it in an email. Never trust user input!
  2. Use Native Functions: For standard WordPress setups, using the native wp_mail() function is often the simplest and most reliable method.
  3. Avoid Direct HTML in Email Body (for simplicity): While your initial code tried to set content types, for simple text-based notifications, keeping the body plain text (as shown above) is safer than injecting complex HTML unless you are absolutely certain of the output sanitation.

Conclusion

Sending emails from a form in WordPress requires bridging the gap between what the user sees (HTML) and what the server executes (PHP). By mastering input sanitization and using appropriate functions like wp_mail(), you can build robust, functioning forms. If you are looking to structure complex applications with clean separation of concerns, much like how modern frameworks handle data routing, exploring concepts from structured development, similar to those found in systems like Laravel, will serve you well on your journey. Start simple, prioritize security, and happy coding!

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.