What potential pitfalls should be considered when sending emails in PHP?

When sending emails in PHP, potential pitfalls to consider include ensuring that the email address is properly sanitized to prevent injection attacks, handling errors that may occur during the email sending process, and setting appropriate headers to avoid being marked as spam.

// Example code snippet for sending emails in PHP with proper error handling and header settings
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";

$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

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