在线表格中的 e-mail header 显示不正确

The e-mail header from the online form is displayed incorrectly

我在网上有一份调查表。填写此表格后,我使用 PHP 功能 mail() 通过电子邮件将其发送给我。表单 body 及其包含的数据(包括私人消息)在 gmail.com 上正确显示。但是,问题出现在电子邮件本身的 header 中。某些字符显示不正确。 这是一个示例 header:

$headers = "Content-Type:text/html; charset=utf-8\r\n";
$headers .= "From:" .$email . "\r\n";
$headers .= "Reply:" . $email . "\r\n";
$headers .= "X-Mailer: PHP/". phpversion() . "\r\n" ;

需要显示的电子邮件主题:

Nový dotaz -- námět, od Fořt Petr <p.fort1990@gmail.com>

同时显示的主题:

Nový dotaz -- námÄ☒t od: FoÅ☒t Petr <p.fort1990@gmail.com>

平方乘以符号更像是一个矩形。

有什么问题吗?或者我应该在哪里寻找错误?

我不确定\r\n是否适用于所有平台

参见:Which line break in php mail header, \r\n or \n?

改为

("xxx\r\n\yyy");

使用

Header('xxx');
Header('yyy');

或使用 PHP_EOL,而不是“\r\n”

问题已解决。我的托管服务提供商对 headers 使用不同的字符编码 - 我无法解释原因,但以下 php 函数将完成所有工作。

function recode_to_utf8 ($text, $encoding = "utf-8")
{
    return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}

现在您所要做的就是结合上面定义的方法使用 mail() 方法发送电子邮件 recode_to_utf8()。像这样:

mail(recode_to_utf8($mail_to), recode_to_utf8($subject), recode_to_utf8($message), recode_to_utf8($headers));

希望对和我有同样问题的人有所帮助