What are common pitfalls when testing the PHP mail() function on a web server?

Common pitfalls when testing the PHP mail() function on a web server include incorrect email addresses, misconfigured SMTP settings, and server restrictions on sending emails. To solve these issues, make sure to double-check the email addresses, ensure that the SMTP settings are correctly configured in the php.ini file, and verify with your hosting provider if there are any limitations on sending emails from the server.

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com";

if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully.";
} else {
    echo "Email sending failed.";
}