梦飞科技 > 帮助中心 > 服务器相关 > PHP发送邮件出现乱码的解决办法

PHP发送邮件出现乱码的解决办法

在工作中遇到的一些问题,拿出来给大家分享一下!这只是我所遇到的问题的解决方案,此方法也许不能解决所有的问题,当然还得具体问题具体分析了!

邮件主题的乱码解决:
例如:邮件的主题用变量$subject,在取得到的主题后加上下面一句,这样标题乱码就解决了
$subject = "=?GB2312?B?".base64_encode($subject)."?=";

邮件内容的乱码解决:
并且在发送邮件的类文件smtpfunction.php中,在定义发送邮件的函数sendmail修改如下:
<?php
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(.)", "1.3", $body);
$header .= "MIME-Version:1.0rn";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html; charset=gb2312 rn";//这里的gb2312设置一下
}
$header .= "To: ".$to."rn";
if ($cc != "") {
$header .= "Cc: ".$cc."rn";
}

$header .= "From: $from<".$from.">rn";
$header .= "Subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "Date: ".date("r")."rn";
//$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
$header .= "X-Mailer:By Microsoft Outlook Express 5.00.2615.200rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}

if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."n");
$sent = FALSE;
continue;
}
?>

 

(责任编辑:李春艳)

分享按钮
相关文章