简体   繁体   English

PHP邮件utf-8问题

[英]PHP mail utf-8 issues

I've trying to send an email in utf-8 encoding using a PHP mail() func. 我试图使用PHP mail()函数以utf-8编码发送电子邮件。 And there's some issues with this. 这有一些问题。 Such as: 如:

  1. If in a body of email is present the unicode characters then I receive an email with header "Content-Type: application/octet-stream". 如果在电子邮件正文中存在unicode字符,那么我会收到一封标有“ Content-Type:application / octet-stream”的电子邮件。 Otherwise, if there was no unicode chars I've got Content-Type: text/plain; charset=us-ascii 否则,如果没有Unicode字符,我将拥有Content-Type: text/plain; charset=us-ascii Content-Type: text/plain; charset=us-ascii . Content-Type: text/plain; charset=us-ascii This behavior is not depending of sending a header Content-Type: text/plain; charset=UTF-8 此行为不依赖于发送标头Content-Type: text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8
  2. If the subject consists of utf-8 chars that encoded according to RFC2047 then I receive a double encoded subject like this Subject: =?us-ascii?Q?=3D=3FUTF-8=3FB=3Fw5HClsORwpfDkcKU=3F=3D?= . 如果主题由根据RFC2047编码的utf-8字符组成,那么我会收到一个类似此Subject: =?us-ascii?Q?=3D=3FUTF-8=3FB=3Fw5HClsORwpfDkcKU=3F=3D?=的双编码Subject: =?us-ascii?Q?=3D=3FUTF-8=3FB=3Fw5HClsORwpfDkcKU=3F=3D?= Instead of expected Subject: =?UTF-8?B?w5HClsORwpfDkcKU?= 而不是预期的Subject: =?UTF-8?B?w5HClsORwpfDkcKU?=

Example: 例:

$subject = 'іїє';
$body = 'іїє';
$headers = "Content-Type: text/plain; charset=UTF-8\r\n"
    . "MIME-Version: 1.0\r\n";

mail('some-address@gmail.com', mb_encode_mimeheader($subject, 'UTF-8', 'B'), $body, $headers);

Here is the part of received headers: 这是收到的标头的一部分:

Subject: =?us-ascii?Q?=3D=3FUTF-8=3FB=3Fw5HClsORwpfDkcKU=3F=3D?=
User-Agent: Heirloom mailx 12.5 6/20/10
MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: 8bit

С–С—С”

As MTA on server is installed postfix. 由于服务器上的MTA已安装后缀。 As a client using gmail 作为使用gmail的客户端

Email is a very archaic, outdated, and difficult technology. 电子邮件是一种非常古老,过时且困难的技术。 This is especially true with character encoding. 对于字符编码尤其如此。 I would suggest using a library, such as PHPMailer . 我建议使用库,例如PHPMailer If you insist upon doing this from scratch, the true answer will go beyond the scope of your question. 如果您坚持从头开始,那么真正的答案将超出您的问题范围。 Browsers, clients, and much more complicate this issue. 浏览器,客户端等使此问题复杂化。 I can't tell you the cause of your issue, but the solution is PHPMailer. 我无法告诉您问题的原因,但是解决方案是PHPMailer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM