How can PHP developers ensure UTF-8 compliance when sending emails using the mail() function?
PHP developers can ensure UTF-8 compliance when sending emails using the mail() function by setting the appropriate headers in the email message. This includes setting the Content-Type header to specify UTF-8 encoding and the charset parameter. Additionally, developers should ensure that the subject and body of the email are properly encoded using utf8_encode().
$to = "recipient@example.com";
$subject = "Subject of the email";
$message = "Content of the email";
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
$headers .= "From: sender@example.com" . "\r\n";
mail($to, $subject, $message, $headers);