What are some best practices for setting headers in emails sent from PHP to ensure proper formatting?
When sending emails from PHP, it is important to set the headers correctly to ensure proper formatting and delivery. One common best practice is to include the Content-Type header with a value of "text/html" if you are sending HTML emails. This tells the email client to interpret the content as HTML and display it accordingly. Additionally, setting the From header with a valid email address helps prevent your emails from being flagged as spam.
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = '<p>This is a test email.</p>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: sender@example.com' . "\r\n";
mail($to, $subject, $message, $headers);