What is the purpose of using PHP to send form data in an HTML email?
When sending form data in an HTML email, using PHP can help process the form data and format it correctly before sending it via email. PHP can be used to retrieve the form data, sanitize it to prevent security vulnerabilities, and then format it in HTML to be included in the email body.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'recipient@example.com';
$subject = 'Contact Form Submission';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: ' . $email . "\r\n";
$body = "<p>Name: $name</p>";
$body .= "<p>Email: $email</p>";
$body .= "<p>Message: $message</p>";
mail($to, $subject, $body, $headers);
}
?>
Keywords
Related Questions
- What are the common pitfalls to avoid when working with dynamic SQL queries in PHP, especially when using user input as part of the query?
- How can PHP be used to dynamically select options in a dropdown menu based on user input?
- How can one ensure that relative path references in PHP fopen functions are correctly pointing to the desired file location within the directory structure?