What are the best practices for setting up SMTP mail delivery in PHP scripts to avoid spam detection?

When setting up SMTP mail delivery in PHP scripts, it is important to follow best practices to avoid triggering spam filters. This includes using a reputable SMTP server, authenticating with proper credentials, setting appropriate headers, and including relevant content in the email body.

// Example PHP code snippet for setting up SMTP mail delivery
$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";

// Set up SMTP configuration
ini_set("SMTP", "smtp.example.com");
ini_set("smtp_port", "25");
ini_set("sendmail_from", "sender@example.com");

// Send the email
mail($to, $subject, $message, $headers);