How can PHP be used to send form data via email?
To send form data via email using PHP, you can use the `mail()` function to send an email with the form data as the message body. You will need to set the appropriate headers for the email, such as the recipient email address, subject, and any additional headers. Make sure to sanitize and validate the form data before sending it to prevent security vulnerabilities.
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "recipient@example.com";
$subject = "Form Submission";
$message = "Name: " . $_POST['name'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Message: " . $_POST['message'];
$headers = "From: sender@example.com\r\n";
if(mail($to, $subject, $message, $headers)) {
echo "Email sent successfully.";
} else {
echo "Email sending failed.";
}
}
?>
Keywords
Related Questions
- What are the advantages of using Closures over create_function() in PHP?
- What are some common errors or misunderstandings when using mysql_field_seek in PHP, as indicated by the forum thread discussion?
- What is a common pitfall when using regular expressions in PHP for replacing multiple occurrences of a specific pattern within a string?