简体   繁体   中英

PHP mail utf-8 issues

I've trying to send an email in utf-8 encoding using a PHP mail() func. 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". Otherwise, if there was no unicode chars I've got 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
  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?= . Instead of expected 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. As a client using 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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