What security considerations should be taken into account when sending form data via PHP mail function?
When sending form data via the PHP mail function, it is important to sanitize and validate user input to prevent malicious code injections or email header injections. Additionally, it is recommended to use proper email headers to prevent spoofing and to ensure the email is sent securely.
// Sanitize and validate user input
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
// Set proper email headers
$headers = "From: Your Name <your@email.com>\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Send email
mail("recipient@email.com", "Subject", $message, $headers);