2017-12-27 456 views
1

我有以下的PHP代码:PHP无法发送UTF-8的消息

<?php 

$to  = '[email protected]'; 
$subject = date("d/m/Y"); 
$message = 'Hey 123 [email protected]# αβγ'; 

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

mail($to,$subject,$message,$headers); 

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

邮件被发送罚款。问题是αβγ(Unicode字符)没有正确收到邮件收件人的结尾。

这是收件人看到的是:Hey 123 [email protected]# αβγ 这是他应该明白:Hey 123 [email protected]# αβγ

我到处找用尽一切方法,改变标题,将我的字符串为Unicode等等等等,但什么事都没有工作。也许我做错了什么?

+0

可能的问题与实际阅读器? – GrumpyCrouton

+0

你正在使用错误的字符集。 –

+0

@FunkFortyNiner我该如何解决这个问题? –

回答

4

使用UTF-8字符集标题,例如:

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

业大:

$headers .= "Content-Type: text/html; charset=UTF-8\n";