我的联系表格中有以下 PHP。
// Ensure a message was entered, then sanitize it
if(!empty($_POST['cf_m']) && $_POST['cf_m']!='Enter Your Message Here')
{
$message = strip_tags($_POST['cf_m']);
}
当我通过电子邮件收到消息时,挪威字符 æ、ø、å 变为 æ、ø、Ã¥。
我该怎么做才能显示正确的字符?
看来您的表单是作为UTF-8
,但电子邮件是作为ISO-8859-1
(或其他变体)发送的。您可能希望通过设置标头来显式设置已发送电子邮件的字符编码Content-type
,例如:
Content-type: text/plain; charset=UTF-8
设置$additional_headers
PHPmail()
函数的参数以完成此操作。
您必须在邮件标头中设置编码,就像在 mail() ( http://de.php.net/manual/de/function.mail.php ) 的 php-comments 中解释的那样:
<?php
function mail_utf8($to, $subject = '(No subject)', $message = '', $from) {
$header = 'MIME-Version: 1.0' . "\n" . 'Content-type: text/plain; charset=UTF-8'
. "\n" . 'From: Yourname <' . $from . ">\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header);
}
?>
我认为这些字符是 UTF8 编码的,所以你可以使用utf8_decode 函数来正确显示它们
在整个代码中使用 UTF-8。对我来说,这个页面非常有帮助。
这是关于发送带有挪威字符的电子邮件吗?
看看SwiftMailer,它是一个很棒的处理电子邮件的库。http://swiftmailer.org/docs/message-charset中描述了设置消息的字符集。(而且由于 utf-8 是 SwiftMailer 的默认值,你不必在你的情况下做任何事情。)