天天看点

php发送邮件出现乱码,解决 PHP 的 mail() 发送邮件时出现乱码的问题

当用php的mail()函数发送邮件时,如果包含中文标题产生乱码,需要做以下处理即可解决:

$subject = '邮件标题中文-php-mail()函数';

$subject = "=?UTF-8?B?".base64_encode($subject)."?=";

对应的,邮件的header可以简单设置一下,以下举例说明发送一封邮件:

$mail = '[email protected]';

$text = "邮件正文content……";

$subject = 'IVR 内置控制平台定时脚本运行SQL错误';

$subject = "=?UTF-8?B?".base64_encode($subject)."?=";

$headers = 'From: You ' . "\n";

$headers .= 'MIME-Version: 1.0' . "\n";

$headers .= 'Content-type: text/html; charset=uft-8' . "\r\n";

$headers .="Content-Transfer-Encoding: 8bit";

mail($mail, $subject, $text, $headers );

对邮件正文同样有效