What are the potential security implications of using PHP's mail function?

Potential security implications of using PHP's mail function include the risk of email header injection, allowing attackers to inject additional headers into the email and potentially spoofing the sender's address. To mitigate this risk, it is important to sanitize user input and validate email addresses before using them in the mail function.

// Sanitize and validate email address before using it in the mail function
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Use the sanitized and validated email address in the mail function
    mail($email, $subject, $message, $headers);
} else {
    // Handle invalid email address
    echo "Invalid email address";
}