How can PHP be used to handle form submissions and send emails with user input data?
To handle form submissions and send emails with user input data in PHP, you can use the $_POST superglobal to retrieve the form data, validate it, and then use the mail() function to send an email with the user input data.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Validate input data here
$to = "recipient@example.com";
$subject = "New message from $name";
$body = "From: $name\nEmail: $email\nMessage: $message";
if (mail($to, $subject, $body)) {
echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}
}
?>
Related Questions
- What are some best practices for efficiently reading and processing large log files in PHP?
- What is the best way to determine which checkboxes were selected in PHP?
- What are some best practices for handling large forms with multiple input fields in PHP to ensure all values are properly submitted and processed?