PHP fails to send UTF-8 Messages

php html html5 unicode

100 观看


72 作者的声誉

This question already has an answer here:

I have the following PHP code:


$to      = '';
$subject = date("d/m/Y");
$message = 'Hey 123 !@# αβγ';

$headers  = "From: testsite < >\n";
$headers .= "Cc: testsite < >\n"; 
$headers .= "X-Sender: testsite < >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path:\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";


echo "<script type='text/javascript'>alert('Message sent! We will get back to you soon!');</script>";
echo "<script>window.location.href = '';</script>";

The mail gets sent fine. The problem is that αβγ (Unicode Characters) doesn't get on the mail recipient's end correctly.

This is what the recipient sees: Hey 123 !@# αβγ This is what he should see: Hey 123 !@# αβγ

I've looked everywhere and tried everything, changing headers, converting my string to unicode etc. etc., but nothing has worked. Maybe I'm doing something wrong?

作者: Coto TheArcher 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


2397 作者的声誉


Use the UTF-8 charset header, for example:

$headers .= "Content-Type: text/html; charset=iso-8859-1\n";

Goes to:

$headers .= "Content-Type: text/html; charset=UTF-8\n";
作者: samdd 发布者: 27.12.2017 05:08