Maison >développement back-end >tutoriel php >PHP函数mail()的乱码解决方法介绍_PHP教程
一些人在使用PHP进行编程操作时,经常会遇到一些问题。比如在使用PHP函数mail()出现乱码的解决方法:
先用函数base64_encode() — 使用 MIME base64 对数据进行编码
标题字符串前加编码类型例如: =?UTF-8?B?
标题字符串后加:?=
邮件header说明Content-type — 防止邮件正文也乱码 举例说明:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">to</span><span> = ‘name@example.com’; </span></span></li> <li><span> </span></li> <li class="alt"> <span>$</span><span class="attribute">subject</span><span> = “=?UTF-8?B?”.base64_encode(’邮件标题’).”?=”; </span> </li> <li><span> </span></li> <li class="alt"> <span>$</span><span class="attribute">headers</span><span> = ‘MIME-Version: 1.0′ . “rn”; </span> </li> <li><span> </span></li> <li class="alt"> <span>$headers </span><span class="attribute">.</span><span>= ‘Content-type: text/html; </span><span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8′ . “rn”; </span> </li> <li><span> </span></li> <li class="alt"><span>// Additional headers </span></li> <li><span> </span></li> <li class="alt"> <span>$headers </span><span class="attribute">.</span><span>= ‘To: Name </span><span class="tag"><span> </span><span class="tag-name">name</span><span>@example.com</span><span class="tag">></span><span>’ . “rn”; </span></span> </li> <li><span> </span></li> <li class="alt"> <span>$headers </span><span class="attribute">.</span><span>= ‘From: Admin </span><span class="tag"><span> </span><span class="tag-name">admin</span><span>@example.com</span><span class="tag">></span><span>’ . “rn”; </span></span> </li> <li><span> </span></li> <li class="alt"> <span>$headers </span><span class="attribute">.</span><span>= ‘Reply-To: Name </span><span class="tag"><span> </span><span class="tag-name">name</span><span>@example.com</span><span class="tag">></span><span>’ . “rn”; </span></span> </li> <li><span> </span></li> <li class="alt"><span>mail($to, $subject, $message, $headers); </span></li> <li><span> </span></li> </ol>
以上就是PHP函数mail()在使用时出现乱码的具体解决方法。