How can one ensure that all form data, including the message content, is properly sent via email in PHP?
To ensure that all form data, including the message content, is properly sent via email in PHP, you can use the PHP `mail()` function to send the email with the form data as the message content. Make sure to properly sanitize and validate the form data to prevent any security vulnerabilities.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "recipient@example.com";
$subject = "Contact Form Submission";
$headers = "From: $email";
$body = "Name: $name\nEmail: $email\nMessage: $message";
if (mail($to, $subject, $body, $headers)) {
echo "Email sent successfully";
} else {
echo "Email sending failed";
}
}
?>