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";
}
}
?>
Keywords
Related Questions
- How can PHP developers ensure that their scripts are secure and not susceptible to variable injection attacks when register_globals is enabled on the server?
- What is the best practice for writing if-else statements in PHP for readability and efficiency?
- What common issue arises when using PHP to validate user input in a form submission?