Utf-8 encoded PHP mail does not display correctly in Microsoft email clients

I searched and searched for a solution for this, but to no avail.

I am using the php mailer to send mixed text / html email encoded in utf8. Here is the relevant code:

$headers = "From: $fromPerson\r\n" . "Content-Transfer-Encoding: 8bit\r\n". "Reply-To: $fromPerson\r\n" . "Content-Type: multipart/alternative; boundary=". $mime_boundary_header. "\r\n" . "X-Mailer: PHP/" . phpversion(); $message = "$notice_text --$mime_boundary Content-Type: text/plain; charset='UTF-8' Content-Transfer-Encoding: 8bit $textEmail --$mime_boundary Content-Type: text/html; charset='UTF-8' Content-Transfer-Encoding: 8bit $htmlEmail --$mime_boundary--"; //mb_detect_encoding($message) returns UTF-8 $mail_sent = @mail( $to, $subject, $message, $headers); 

Messages contain Spanish along with these tricky characters. Email messages are perfectly displayed in gmail, hotmail (online prospects), mac mail, phones, etc., but not in direct Windows or Microsoft Outlook mail.

If I manually set the default font in Windows Live Mail in utf8, the message will display correctly, but otherwise it will not. If I forward an email from another client to Outlook or to Windows Live, it also displays great.

I could find work around, I'm sure, but am I missing something? I don’t want to rely on receivers, knowing how to change the encoding of the message, so is there something I need to add to the letter in order to encourage these clients to recognize the encoding?

I apologize if this has been considered elsewhere and I would appreciate any advice. It seems like I should just go and use PHPMailer to see if this fixes the problem, but out of personal curiosity it would be interesting to know why this is happening ...

+6
source share
3 answers

I'm not sure if the encoding wrapper is necessary or even fixed. Try deleting them:

 Content-Type: text/plain; charset=UTF-8 
+12
source
 $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=UTF-8\r\n"; $headers .= "From: example@example.com \r\n"; $headers .= "Reply-To: example@example.com \r\n"; 
+7
source

Modifying Riko's answer makes for a bit cleaner code.

 $header_array = [ "MIME-Version: 1.0", "Content-type: text/html; charset=UTF-8", "From: example@example.com ", "Reply-To: example@example.com " ]; $headers = implode("\r\n", $header_array); 
+4
source

Source: https://habr.com/ru/post/945684/


All Articles