What are the potential security risks associated with using the 'mail' function in PHP for email sending?
One potential security risk associated with using the 'mail' function in PHP for email sending is the possibility of injection attacks if user input is not properly sanitized. To mitigate this risk, it is important to validate and sanitize all user input before using it in the 'mail' function.
// Sanitize user input before using it in the mail function
$to = filter_var($_POST['to'], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
// Send email using sanitized input
mail($to, $subject, $message);