What are the security considerations when sending emails from PHP, especially in terms of avoiding spam filters and blacklisting issues?

When sending emails from PHP, it is important to ensure that the email headers are properly set to avoid being flagged as spam. One way to do this is by setting the "From" header to a valid email address from the same domain as the server. Additionally, using a reputable email service provider or setting up proper SPF and DKIM records can help prevent blacklisting issues.

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: yourname@yourdomain.com\r\n";
$headers .= "Reply-To: yourname@yourdomain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

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