【问题标题】:The e-mail header from the online form is displayed incorrectly在线表单中的电子邮件标题显示不正确
【发布时间】:2020-05-09 17:44:16
【问题描述】:

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

$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>

平方倍符号更像一个矩形。

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

【问题讨论】:

  • 错字:$headerd .= 应该是$headers .=
  • 隔壁的按键。
  • 请提供显示不正确的字符示例。什么是预期的输出?它们在 utf-8 字符集中吗?
  • 1) php 版本? 2) 您的 html 页面是否在标题部分包含 ? 3)您的表单是否使用属性accept-charset="xxxxxx",值是什么?

标签: php html-email


【解决方案1】:

我不确定 \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"

【讨论】:

    【解决方案2】:

    问题解决了。我的托管服务提供商对标头使用不同的字符编码 - 我无法解释原因,但下面的 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));
    

    如果其他人和我有同样的问题,我希望它可以帮助他们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 2015-10-31
      • 2021-05-21
      • 2017-03-09
      • 2019-08-30
      • 1970-01-01
      • 2011-04-10
      • 2019-11-24
      相关资源
      最近更新 更多