What are the best practices for finding and using PHP scripts for form mailers on a website?
When looking for PHP scripts for form mailers on a website, it's important to choose a reliable and secure script that has been regularly updated. Make sure to thoroughly review the documentation and any user reviews before implementing the script on your site. Additionally, always sanitize and validate user input to prevent security vulnerabilities.
// Example PHP script for form mailer
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Validate input
if (!empty($name) && !empty($email) && !empty($message)) {
// Sanitize input
$name = filter_var($name, FILTER_SANITIZE_STRING);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$message = filter_var($message, FILTER_SANITIZE_STRING);
// Send email
$to = "your@email.com";
$subject = "Contact Form Submission";
$headers = "From: $email";
$body = "Name: $name\nEmail: $email\nMessage: $message";
if (mail($to, $subject, $body, $headers)) {
echo "Message sent successfully";
} else {
echo "Error sending message";
}
} else {
echo "Please fill out all fields";
}
}
Related Questions
- Are there alternative methods to retrieve path information in PHP if PATH_INFO is not set in Xampp?
- How can the logic for counting active modules be improved in the provided code snippet to avoid unnecessary complexity?
- How can PHP developers effectively manage and display data with expiration dates on a website?