What is the recommended method to encode variables in PHP for email subjects?

When encoding variables in PHP for email subjects, it is recommended to use the mb_encode_mimeheader() function to ensure proper encoding, especially for non-ASCII characters. This function will encode the subject using the MIME header encoding format, which is compatible with most email clients.

$subject = 'Subject with non-ASCII characters: äöü';
$subject_encoded = mb_encode_mimeheader($subject, 'UTF-8');