情況:收的人用outlook收信,發現是一團亂碼。
主旨、寄件人 皆比照辦理
$subject = "天氣 ".$state." 可能會...";
添加base64_encode
$subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
內容:加入html 和charset=utf-8
$message = '
Fillon soutient à fond le retour d\'un Grand Prix de France
Le Premier ministre François Fillon, passionné d\'automobile et pilote à ses heures, a apporté un soutien appuyé au retour d\'un Grand Prix de France au calendrier 2013 de la Formule 1, en faisant un passage-éclair vendredi sur le circuit Paul Ricard dans le Var.
header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf- 8\r\n";
如果再不見,只好見官方PHP 的MAIL 有前輩提供了一個fuhction..
網址:http://www.php.net/manual/en/function.mail.php#92976
/*
**
* Function responsible for sending unicode emails.
*
* @author Gajus Kuizinas
* @version 1.0.1 (2012 01 11)
*/
function mail_send($arr)
{
if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
throw new HelperException('mail(); not all parameters provided.');
}
$to = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
$from = empty($arr['from_name']) ? $arr['from_email'] : '"' . mb_encode_mimeheader($arr['from_name']) . '" <' . $arr['from_email'] . '>';
$headers = array
(
'MIME-Version: 1.0',
'Content-Type: text/html; charset="UTF-8";',
'Content-Transfer-Encoding: 7bit',
'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
'X-Mailer: PHP v' . phpversion(),
'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
);
mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));
}
2013.05.17 新增
因為寄件人亂碼了,所以做了變化,不知道有沒有成功。
function mail_send($arr)
{
if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
throw new HelperException('mail(); not all parameters provided.');
}
$to = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
//針對寄件人的姓名做DEBUG避免寄件人姓名亂碼
$from = empty($arr['from_name']) ? $arr['from_email'] : '"' .'=?UTF-8?B?' . base64_encode( mb_encode_mimeheader($arr['from_name'])) . '?=' . '" <' . $arr['from_email'] . '>';
$headers = array
(
'MIME-Version: 1.0',
'Content-Type: text/html; charset="UTF-8";',
'Content-Transfer-Encoding: 7bit',
'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
'X-Mailer: PHP v' . phpversion(),
'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
);
mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));
}
base64_encode 解釋:
Encodes the given data with base64.
This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.
Base64-encoded data takes about 33% more space than the original data.
參考連結:
php mail 寄送會有亂碼: 解決方式如下:
http://www.macsoho.net/blog/?p=143
http://www.php.net/manual/en/function.mail.php#92976
參考:
- PHPMailer - PHP email class