How can PHP handle form submissions and retrieve input data for use in mail scripts?
To handle form submissions and retrieve input data for use in mail scripts in PHP, you can use the $_POST superglobal array to access form data submitted via the POST method. You can then sanitize and validate the input data before using it in your mail script to prevent security vulnerabilities.
// Handle form submission and retrieve input data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Sanitize and validate input data
$name = filter_var($name, FILTER_SANITIZE_STRING);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$message = filter_var($message, FILTER_SANITIZE_STRING);
// Use input data in mail script
$to = "recipient@example.com";
$subject = "New message from $name";
$body = "Name: $name\nEmail: $email\nMessage: $message";
$headers = "From: $email";
// Send email
mail($to, $subject, $body, $headers);
}
Keywords
Related Questions
- What is the best practice for implementing an if statement in a button based on the content of a text file in PHP?
- Are there specific characters or symbols that should be filtered out in SQL queries to prevent potential security vulnerabilities in PHP?
- If the error "Call to undefined function session_is_registered()" persists, where else could the issue lie in the code?