Home >Backend Development >PHP Tutorial >How Can I Send an Email from an HTML Form Using PHP?
Introduction
When a user submits an HTML form, a PHP script can be used to process the form data and send an email. However, some users encounter issues with the mail function not sending the email even when the script runs without errors.
Understanding the Problem
The provided code snippet fails to send an email due to a missing significant parameter in the mail() function. The correct syntax should include the recipient's email address, the email subject, the message content, and the email headers.
Revisiting the Code
Here is a revised version of the PHP script that will send an email from a form:
<?php if (isset($_POST['submit'])) { // Extract data from the form $to = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; $headers = "From: " . $from; // Send the email using the PHP mail() function if (mail($to, $subject, $message, $headers)) { echo "Mail Sent."; } else { echo "An error occurred while sending the email."; } } ?>
HTML Form
Here is the HTML form for which the PHP script will process the data:
<form action="" method="post"> <label for="email">Email:</label> <input type="email" name="email">
Explanation
Additional Notes
The above is the detailed content of How Can I Send an Email from an HTML Form Using PHP?. For more information, please follow other related articles on the PHP Chinese website!