問題描述
電子郵件主題中出現的額外字符 £ 在英鎊符號前面 (Extra character appearing in email subject £ in front of pound symbol)
我正在使用類系統php文件從mysql數據庫發送HTML電子郵件,主題標題中的英鎊符號前面出現了一個額外的字符,但電子郵件的主要內容很好。
我曾嘗試為電子郵件使用 UTF 字符集,但這會破壞實際的電子郵件內容區域,因此所有電子郵件都不再發送 HTML 內容,儘管它確實解決了主題英鎊符號問題。還嘗試了 str_replace 如果有辦法對其進行編碼,因此標題僅使用 UTF 和內容 HTML,這將是完美的解決方案。
電子郵件主題代碼部分如下(省略私人細節)
function
sendTemplateEmail($groupid,$subject,$body,$tipster_code,
$email_image,$tipster_name,$only_active="",
{
global $conn;
$errors = "";
$email_list = array();
$total_users = 0;
$group = (is_numeric($groupid)) ? $groupid : 0;
#die("body = ".$body);
$body = str_replace("£","£",$body);
$body = str_replace("Â","",$body);
$subject = str_replace("Â","",$subject);
$html = "<!DOCTYPE HTML PUBLIC \"‑//W3C//DTD HTML 4.01 Transitional//EN\">\n";
$html .= "<html lang='en'>\n";
$html .= "<head>\n";
$html .= "<meta http‑equiv=\"Content‑Type\" content=\"text/html; charset=iso‑8859‑1\">\n";
$html .= "<title>O.com</title>\n";
$html .= "</head>\n";
$html .= "<body style='width:800px;' bgcolor=\"#FFFFFF\">\n";
$dw_mail‑>AddReplyTo("admin@","");
$dw_mail‑>ReturnPath = "admin@";
$dw_mail‑>From = $from;
$dw_mail‑>FromName = $from;
$dw_mail‑>AddAddress($users['email'],"");
$dw_mail‑>AddAddress("@hotmail.com","");
$dw_mail‑>WordWrap = 50; // set word wrap
//$dw_mail‑>AddAttachment(PDF_PATH."app‑".$app.".pdf");
$dw_mail‑>IsHTML(); // send as html
$dw_mail‑>Subject = stripslashes($subject);
$dw_mail‑>Body = $html;
$dw_mail‑>AltBody = stripslashes($alt_body);
參考解法
方法 1:
In UTF‑8 £ character is encoded on two bytes: C2 A3 C2 A3 decoded using ISO‑8859‑1 gives: £
So I guess that the value of the subject that you get from MySQL database is encoded in UTF‑8 whereas $dw_mail‑>Subject expect a value encoded in ISO‑8859‑1.
Using mb‑convert‑encoding should let you do the conversion from UTF‑8 to ISO‑8859‑1. Note that not all UTF‑8 characters can be mapped to ISO‑8859‑1 so you might have issue with other special characters.
(by Daniel、Antoine Mottier)