What is the recommended method to handle special characters, such as umlauts, in email subjects and senders when using PHP?

Special characters, such as umlauts, in email subjects and senders can sometimes cause encoding issues or display problems. To handle these characters properly in PHP, it is recommended to use the mb_encode_mimeheader() function to encode the subject and sender names before sending the email.

$subject = 'Subject with ümlauts';
$sender_name = 'Sender Name with ümlauts';

$subject_encoded = mb_encode_mimeheader($subject, 'UTF-8');
$sender_name_encoded = mb_encode_mimeheader($sender_name, 'UTF-8');

// Use the encoded subject and sender name in the email headers
$headers = "From: $sender_name_encoded <sender@example.com>\r\n";
$headers .= "Subject: $subject_encoded\r\n";

// Send the email
mail('recipient@example.com', $subject_encoded, 'Email body', $headers);