PHP mail()函数乱码出现的原因一般是函数中header参数缺少Content-type: text/html; charset=utf-8等原因引起的。

我们在使用PHP语言进行代码编程,处理中文字符时经常会遇到乱码出现的问题。我们今天就为大家介绍有关PHP mail()函数乱码的具体解决办法。#t#

PHP mail()函数乱码的具体解决方法(如何解决php乱码)  mail()函数乱码 第1张

一般这种问题出现的原因是 mail() 函数的 header 参数里少了 Content-type: text/html; charset=utf-8,或者 charset 不是utf-8。很多国外的PHP程序一开始开发的时候没有吧中文考虑进去,所以中文使用的时候就会出现PHP mail()函数乱码。

先用函数base64_encode() 使用 MIME base64 对数据进行编码
标题字符串前加编码类型例如: =?UTF-8?B?
当然如果是gb2312的话就 =?GB2312?B?
标题字符串后加:?=

PHP mail()函数乱码的处理办法举例如下

  1. $to='xinple@example.com';
  2. $subject="=?UTF-8?B?".
    base64_encode('邮件标题')."?=";
  3. $headers='MIME-Version:1.0'."\r\n";
  4. $headers.='Content-type:
    text/html;charset=utf-8'
    ."\r\n";
  5. //Additionalheaders
  6. $headers.='To:Xinple<
    < ahref="mailto:xinple@example.com">
    xinple@example.com</a>&gt;'
    ."\r\n";
  7. $headers.='From:Admin&lt;
    <ahref="mailto:admin@example.com">
    admin@example.com</a>&gt;'
    ."\r\n";
  8. $headers.='Reply-To:Xinple&lt;xinple@example&gt;'."\r\n";
  9. mail($to,$subject,$message,$headers);

以上就是PHP mail()函数乱码的具体处理办法,希望对有需要的朋友有所帮助。

转载请说明出处
知优网 » PHP mail()函数乱码的具体解决方法(如何解决php乱码)

发表评论

您需要后才能发表评论