php邮件特殊字符utf8
Posted
技术标签:
【中文标题】php邮件特殊字符utf8【英文标题】:php mail special characters utf8 【发布时间】:2013-11-11 13:15:03 【问题描述】:我有以下脚本:
<?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!
我尝试了很多东西,但我已经没有任何想法了。 你能帮助我吗?你有没有得到这个工作?
谢谢!
【问题讨论】:
【参考方案1】:你试过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");?>
【讨论】:
哇,它适用于正文,但不适用于主题:“Testmail � Special Characters” Ideas? @JohnDoeSmith:原因是您不能 utf8_decode() 没有等效的 1 字节表示的字符,例如长破折号。我不明白你怎么能接受这个答案 主题使用mb_encode_mimeheader:$subject = mb_encode_mimeheader($subject,"UTF-8"); 添加正确的邮件标头而不是尝试将内容解码回较弱的编码会更好(阅读:对我来说效果更好)。更简洁的代码,更少的错误。 ***.com/questions/7266935/how-to-send-utf-8-email【参考方案2】:虽然iconv_set_encoding
对我不起作用,但以下方法对我有用:
// Force PHP to use the UTF-8 charset
header('Content-Type: text/html; charset=utf-8');
// Define and Base64 encode the subject line
$subject_text = 'Test email with German Umlauts öäüß';
$subject = '=?UTF-8?B?' . base64_encode($subject_text) . '?=';
// Add custom headers
$headers = 'Content-Type: text/plain; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: base64';
// Define and Base64 the email body text
$message = base64_encode('This email contains German Umlauts öäüß.');
// Send mail with custom headers
if (mail('recipient@domain.com', $subject, $message, $headers))
echo 'Email has been sent!';
else
echo 'Oops, something gone wrong!';
来源:https://dev.to/lutvit/how-to-make-the-php-mail-function-awesome-3cii
【讨论】:
【参考方案3】:使用&rsquo;
代替'。
例子:
The dog’s bone.
确保将内容类型从 text/plain 更改为 text/html。
【讨论】:
那太容易了。如果这样做,我会得到准确的’在输出/电子邮件中以上是关于php邮件特殊字符utf8的主要内容,如果未能解决你的问题,请参考以下文章
PHP电子邮件表单正在通过,单词之间没有空格,也没有特殊字符