我想在使用PHP mail()函数发送的HTML电子邮件主题中插入特殊字符。
我希望我的主题看起来像这样:
★您的新帐户
我尝试使用像★
(★)这样的HTML实体,或者直接在我的代码中粘贴符号,但这也不起作用,除了少数电子邮件客户端。
$to = 'me@example.com';
$subject = '★ Your new account or ★ Your new account';
$message = 'HTML message...';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Me <me@example.com>' . "\r\n";
mail($to, $subject, $message, $headers);
有关如何使其正常工作的任何建议? 谢谢。
答案 0 :(得分:31)
尝试主题:
$sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
然后:
mail($to, $sub, $message, $headers);
答案 1 :(得分:2)
虽然可接受的答案很好用,但在查看原始标题时,无法阅读主题。这是一种替代方法,可以使行保持可读性,并且如果它主要由ascii字符组成,也可以使行更短。
$subject = '=?UTF-8?q?' . quoted_printable_encode($subject) . '?=';
这是已接受答案的编码主题行:
=?UTF-8?B?4piFIFlvdXIgbmV3IGFjY291bnQ=?=
这是我的答案的编码主题行:
=?UTF-8?q?=E2=98=85 Your new account?=
答案 2 :(得分:0)
$headers="";
$message = utf8_encode($message);
$message = wordwrap($message, 70, "\r\n");
$subject = '=?UTF-8?B?'.base64_encode(utf8_encode($subject)).'?=';
$to_name = '=?UTF-8?B?'.base64_encode(utf8_encode($to_name)).'?=';
$from_name = '=?UTF-8?B?'.base64_encode(utf8_encode($from_name)).'?=';
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
//$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
$headers .= "From: $from_name <$from_email>" . "\r\n";
$headers .= "To: $to_name <$to_email>" . "\r\n";
$headers .= "Subject: $subject" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail("", $subject, $message, $headers);