What are the potential security risks associated with modifying PHP scripts to change the sender's email address?

Modifying PHP scripts to change the sender's email address can pose a security risk as it can be used for email spoofing or phishing attacks. To mitigate this risk, always validate and sanitize user input before using it to modify email headers.

// Validate and sanitize the sender's email address
$sender_email = filter_var($_POST['sender_email'], FILTER_SANITIZE_EMAIL);

// Use the validated and sanitized email address in the mail function
$success = mail('recipient@example.com', 'Subject', 'Message', 'From: ' . $sender_email);
if($success) {
    echo 'Email sent successfully';
} else {
    echo 'Failed to send email';
}