Are there any security measures to consider when setting up a form on a website to send emails using PHP?
When setting up a form on a website to send emails using PHP, it is important to implement security measures to prevent abuse such as spamming or injection attacks. One common security measure is to use input validation to sanitize and validate user input before sending the email. This can help prevent malicious code from being injected into the email body or headers.
// Validate and sanitize user input before sending email
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
// Send email
$to = 'recipient@example.com';
$subject = 'Contact Form Submission';
$headers = 'From: ' . $email;
$body = "Name: $name\nEmail: $email\n\n$message";
// Additional security measures can be added here, such as checking for spam keywords or limiting the number of emails sent per hour
mail($to, $subject, $body, $headers);