How can encoding email content with BASE64 help resolve issues with special characters in PHP email headers?

When sending emails with special characters in PHP email headers, encoding the content with BASE64 can help resolve any encoding issues. This is because BASE64 encoding converts the characters into a format that is safe for email headers, ensuring proper display and delivery of the email content.

// Set email headers
$subject = 'Subject with special characters é and ü';
$subject_encoded = '=?UTF-8?B?' . base64_encode($subject) . '?=';
$headers = 'From: sender@example.com' . "\r\n" .
           'Subject: ' . $subject_encoded . "\r\n" .
           'MIME-Version: 1.0' . "\r\n" .
           'Content-Type: text/plain; charset=UTF-8' . "\r\n";

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