What best practices should be followed when sending emails with PHP to ensure deliverability and proper display of special characters?

When sending emails with PHP, it is important to ensure proper encoding of special characters to avoid display issues. To achieve this, it is recommended to use the mb_encode_mimeheader() function to encode special characters in the email headers. Additionally, setting the content type to UTF-8 in the email headers will help ensure proper display of special characters in the email body.

// Set email headers
$subject = 'Subject with special characters é';
$subject_encoded = mb_encode_mimeheader($subject, 'UTF-8');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: sender@example.com' . "\r\n";
$headers .= 'Subject: ' . $subject_encoded . "\r\n";

// Send email
mail('recipient@example.com', 'Subject with special characters é', 'Email body with special characters é', $headers);