2013-10-31 88 views
4

我有以下脚本:PHP的邮件特殊字符UTF8

<?php 
    $subject = "Testmail — Special Characters"; 
    $msg = "Hi there,\n\nthis isn’t something easy.\n\nI haven’t thought that it’s that complicated!"; 

    mail($to,$subject,$msg,$from."\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n"); 
?> 

在电子邮件:

主题:Testmail ? Special Characters

身体:

Hi there, 

this isn?t something easy. 

I haven?t thought that it?s that complicated! 

我尝试了很多东西,但我没有任何想法了。 你能帮我吗?你有没有这个工作?

THX!

回答

12

你试过iconv_set_encoding

这应该工作:在地方的”

<?php 
iconv_set_encoding("internal_encoding", "UTF-8"); 

$subject = "Testmail — Special Characters"; 
$msg = "Hi there,\n\nthis isn’t something easy.\n\nI haven’t thought that it’s that complicated!"; 

mail(utf8_decode($to), utf8_decode($subject), utf8_decode($msg), utf8_decode($from)."\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n");?> 
+1

哇,它适用于身体,但不适用于主题:“Testmail 特殊字符” 想法? –

+0

@JohnDoeSmith:原因是你不能utf8_decode()这样的字符,比如长破折号,没有等效的1字节表示。我不明白你如何接受这个答案 –

+1

使用[mb_encode_mimeheader](http://php.net/manual/en/function.mb-encode-mimeheader.php)为主题: $ subject = mb_encode_mimeheader($受试者, “UTF-8”); – GregTheRules

-1

使用&rsquo;

例子:

The dog&rsquo;s bone. 

确保您更改文本/内容类型纯文本/ HTML。

+0

这太容易了。如果这样做,我会在输出/电子邮件中准确地得到’ –