What are best practices for encoding emails in PHP to ensure compatibility with different email clients?

When sending emails in PHP, it's important to properly encode the email content to ensure compatibility with different email clients. One common method is to use the mb_encode_mimeheader function to encode headers like the subject line, and mb_encode_mimeheader or base64_encode for encoding the email body text.

// Set the email headers
$subject = "Subject line";
$subject = mb_encode_mimeheader($subject, "UTF-8");
$headers = "From: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";

// Encode the email body
$body = "Email content";
$body = mb_encode_mimeheader($body, "UTF-8");

// Send the email
$to = "recipient@example.com";
mail($to, $subject, $body, $headers);