What are some best practices for handling special characters, such as umlauts, in PHP when generating and sending emails with attachments?

Special characters, such as umlauts, can cause encoding issues when generating and sending emails with attachments in PHP. To handle these characters properly, it is recommended to use the mb_encode_mimeheader function to encode the subject line and attachment filenames. This function will ensure that special characters are properly encoded for email headers.

$subject = 'Subject with ümläuts';
$encoded_subject = mb_encode_mimeheader($subject, 'UTF-8');

$attachment_filename = 'file_with_ümläuts.pdf';
$encoded_filename = mb_encode_mimeheader($attachment_filename, 'UTF-8');

// Use $encoded_subject and $encoded_filename when setting email headers for subject and attachment filename