What are some best practices for sending emails in PHP to ensure deliverability and avoid spam filters?

To ensure deliverability and avoid spam filters when sending emails in PHP, it's important to follow best practices such as setting proper headers, using a reliable SMTP server, avoiding spam trigger words, and authenticating your emails with SPF and DKIM records.

<?php
$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";

ini_set("sendmail_from", "sender@example.com");

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