What are some common issues with using the mail() function in PHP, especially when testing locally with XAMPP?

One common issue when using the mail() function in PHP, especially when testing locally with XAMPP, is that emails may not be sent due to misconfigured SMTP settings or missing mail server. To solve this, you can configure XAMPP to use an SMTP server like Gmail's SMTP server to send emails. This involves updating the php.ini file with the SMTP settings and enabling the openssl extension in XAMPP.

// Example PHP code snippet to send email using Gmail's SMTP server in XAMPP
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email sent from XAMPP using Gmail's SMTP server.";
$headers = "From: your_email@gmail.com";

ini_set("SMTP", "smtp.gmail.com");
ini_set("smtp_port", "587");
ini_set("sendmail_from", "your_email@gmail.com");

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

if ($mail_sent) {
    echo "Email sent successfully";
} else {
    echo "Email sending failed";
}