【问题标题】:utf-8 mimeheader, PHP mail() adds @domain in "From:" fieldutf-8 mimeheader, PHP mail() 在“From:”字段中添加@domain
【发布时间】:2012-12-10 11:50:40
【问题描述】:

我使用 PHP 的 mail() 函数发送电子邮件。我需要将 from 字段编码为 UTF-8,所以我设置了这些邮件标头:

function mail_headers($from, $contentType = "text/plain") {
    $headers = "From: ".mime_header_encode($from)."\n";
    $headers .= 'Reply-To: '.mime_header_encode($from)."\n";
    $headers .= 'MIME-Version: 1.0' ."\n";
    $headers .= 'Content-Type: '.$contentType.'; charset=utf-8' ."\n";
    $headers .= 'Content-Transfer-Encoding: 8bit'. "\n";

    return $headers;
}

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

它在我的 PC 上运行良好,但在生产服务器上不太好。它不会将编码字符串=?utf-8?Q?Website name <info@myshop.com>?= 识别为有效的电子邮件,因此它会尝试使其变得更好。服务器最终会发送一封带有这部分标头的电子邮件:

From: =?utf-8?Q?Website name <info@myshop.com>,
  ?=@hosting-domain.com

使用此标头,电子邮件收件人会看到另一个奇怪的发件人(逗号后的字符串)。我可以让 PHP 不触碰我的电子邮件字符串吗?

谢谢!

【问题讨论】:

    标签: php email utf-8 email-headers


    【解决方案1】:

    php 函数mb_encode_mimeheader() 旨在根据 php.ini 指令“mbstring.internal_encoding”对 mime 标头进行编码。如果仅使用mb_encode_mimeheader() 没有帮助,您应该检查该指令的正确设置。

    您还应该阅读我链接的网站上的 cmets。这也可能有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      相关资源
      最近更新 更多