What are common issues with encoding characters like Umlauts in PHP mail headers?

Common issues with encoding characters like Umlauts in PHP mail headers include garbled or incorrectly displayed characters due to encoding mismatches. To solve this issue, you can use the mb_encode_mimeheader() function in PHP to properly encode the header with the UTF-8 charset.

$subject = 'Äpfel und Birnen';
$subjectEncoded = mb_encode_mimeheader($subject, 'UTF-8');
$headers = 'From: sender@example.com' . "\r\n" .
           'Subject: ' . $subjectEncoded . "\r\n" .
           'MIME-Version: 1.0' . "\r\n" .
           'Content-Type: text/plain; charset=UTF-8' . "\r\n";

mail('recipient@example.com', '=?UTF-8?B?' . base64_encode($subject) . '?=', 'This is the message body', $headers);