What are the best practices for handling umlauts in mail headers in PHP?

When handling umlauts in mail headers in PHP, it is important to encode them properly to ensure they are displayed correctly in the email. One common method is to use the mb_encode_mimeheader() function to encode the header value before sending the email.

// Example of encoding umlauts in a mail header
$subject = "Äpfel und Birnen"; // Umlauts in the subject
$subject_encoded = mb_encode_mimeheader($subject, 'UTF-8', 'Q');

// Set the encoded subject in the mail headers
$headers = "From: sender@example.com\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Subject: " . $subject_encoded . "\r\n";

// Send the email
mail('recipient@example.com', 'Subject', 'Body', $headers);