How can PHP developers ensure compliance with RFC standards, such as RFC 2047, when sending emails with non-ASCII characters like umlauts using PHP?

When sending emails with non-ASCII characters like umlauts using PHP, developers can ensure compliance with RFC standards, such as RFC 2047, by encoding the subject and body of the email using the base64 encoding method. This ensures that the characters are properly represented in the email headers and content.

$subject = 'Subject with non-ASCII characters like ümläuts';
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';

$body = 'Body with non-ASCII characters like ümläuts';
$body = '=?UTF-8?B?' . base64_encode($body) . '?=';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$headers .= 'From: sender@example.com' . "\r\n";

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