What are the potential issues with using the mail() function in PHP for sending emails from a website form?

One potential issue with using the mail() function in PHP for sending emails from a website form is that it can be vulnerable to email injection attacks if user input is not properly sanitized. To solve this issue, you should always validate and sanitize user input before using it in the mail() function to prevent malicious code injection.

// Validate and sanitize user input before using it in the mail() function
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

// Use the sanitized user input in the mail() function
$to = 'recipient@example.com';
$subject = 'Website Contact Form';
$headers = 'From: ' . $email;

$mail_sent = mail($to, $subject, $message, $headers);

if($mail_sent) {
    echo 'Email sent successfully';
} else {
    echo 'Failed to send email';
}