Sending HTML form data to an email using PHP is a common task in web development. This tutorial will guide beginners on how to do this step by step.
Fundamentals of an HTML form
An HTML form is a structured element of an HTML document that allows users to input data, which can then be submitted to a server for processing. It’s essentially a container for different types of form controls, such as text inputs, checkboxes, radio buttons, dropdown menus, and more.
Here’s a step-by-step breakdown of how an HTML form works:
- The user navigates to a web page that contains an HTML form.
- The user fills out the form by entering information into the various form controls.
- When the user clicks the “submit” button, the form data is packaged into an HTTP request and sent to the server.
- The server receives the request and processes the data.
- The server sends a response back to the user, which may include a confirmation message or additional content based on the form data.
Let’s take a closer look at some of the key components of an HTML form:
Form controls:
These are the individual elements within the form that allow the user to input data. For example, a text input control allows the user to enter text into a field, while a checkbox control allows the user to select one or more options from a list.
Form attributes:
These are additional properties that can be set on the form element to control how it behaves. For example, the action attribute determines where the form data should be submitted to, while the method attribute specifies the HTTP method to use (e.g. GET or POST).
Submit button:
This is the button that the user clicks to submit the form data to the server.
Form data:
This is the information that the user enters into the form controls. Each form control has a name and a value, which are used to identify the data when it’s submitted to the server.
HTML alone cannot process form submissions and send email because HTML is a markup language that is used to create the structure and content of a web page. While HTML forms can collect user input, they cannot process that input or perform any actions based on it.
That’s where PHP comes in. PHP is a server-side scripting language that can be used to process form submissions and perform various tasks on the server, such as sending email. When the user submits a form, the form data is sent to the server, where it can be processed by a PHP script.
Here are some reasons why a PHP script is required to process form submissions and send email:
Data validation:
When a user submits a form, it’s important to validate the data to ensure that it’s in the correct format and meets any necessary criteria. For example, an email address should be in the format of “example@example.com”. A PHP script can check the data submitted by the user and ensure that it’s valid before processing it or sending an email.
Data processing:
Once the form data is validated, it may need to be processed in some way. For example, if the form is a registration form, the user’s information may need to be stored in a database. A PHP script can take the form data, process it, and perform any necessary actions on the server.
Sending Email Notifications:
If the form is designed to send an email, PHP can be used to send the email from the server. PHP has built-in functions for sending email, such as the mail()
function, which allows developers to send email messages programmatically.
Let’s now dive into building our form backed by the PHP script
Step 1: Create the HTML Form
The first step is to create the HTML form where the user can input their data. Here’s an example of a simple form:
<form method="post" action="process_form.php">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<input type="submit" value="Submit">
</form>
Note that the form’s method
attribute is set to post
and the action
attribute is set to process_form.php
. This means that when the user submits the form, the data will be sent to the process_form.php
file.
Step 2: Create the PHP Script
The next step is to create the PHP script that will handle the form data and send it to an email address. Here’s an example of a simple script:
<?php
// Get the form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Set the recipient email address
$to = 'youremail@example.com';
// Set the email subject
$subject = 'New message from website';
// Build the email content
$body = "Name: $name\n\nEmail: $email\n\nMessage:\n$message";
// Send the email
mail($to, $subject, $body);
// Redirect the user to a thank-you page
header('Location: thank_you.php');
In this script, we first use the $_POST
superglobal to get the form data submitted by the user. We then set the recipient email address, email subject, and email content. Finally, we use the mail
function to send the email and the header
function to redirect the user to a thank-you page.
Step 3: Test the Form
The final step is to test the form and make sure it’s working as expected. Upload the HTML form and the PHP script to your web server, and then fill out the form and submit it. Check your email inbox to see if you received the message.
If everything is working correctly, you should receive an email with the message content that was submitted through the form.