How can PHP developers ensure that email headers, including subjects with special characters like umlauts, are properly encoded to comply with RFC822 standards?

Email headers, including subjects with special characters like umlauts, need to be properly encoded to comply with RFC822 standards. PHP developers can ensure this by using the mb_encode_mimeheader() function to encode the subject before sending the email.

$subject = "Subject with ümlauts";
$subject_encoded = mb_encode_mimeheader($subject, 'UTF-8', 'Q');
$headers = "From: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
$headers .= "Subject: $subject_encoded\r\n";

mail('recipient@example.com', 'Subject', 'Email body', $headers);