What are best practices for formatting and including headers in PHP email scripts to ensure proper delivery and avoid common pitfalls?

When sending emails in PHP, it is important to include proper headers to ensure successful delivery and avoid being marked as spam. One common pitfall is not including necessary headers such as "From" and "Content-Type". To ensure proper delivery, make sure to include these headers in your PHP email script.

$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 .= "Content-type: text/html\r\n";

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