duanlan4801 2011-08-10 10:52
浏览 998
已采纳

如何发送UTF-8编码的电子邮件?

I have inherited a script that sends some content out in three languages (all on same content - repeated) however when recieved the content characters are broken for what i assume is a UTF-8 issue.

Am i right all i need to do is change the charset part to utf-8, or does anything else need to change like the 7bit part ?

you can see where I inserted one UTF-8 reference (not tested yet)

there was something here http://bitprison.net/php_mail_utf-8_subject_and_message which seems to reference base encoding, but I'm not sure if I need that here ?

// Contruct message body.
$body = "";

// Add message for non-mime clients.
$body .= "This is a multi-part message in MIME format.
";

// Add text body.
$body .= "
--$boundary
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

" . $textContent;

// Add HTML body.
$body .= "
--$boundary
Content-Type: text/html; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

" . $htmlContent;

mail( $row["email"], "Update Your ArtsDB Listing", $body, $headers );

I looked on another post on here for an a example.

$body .= "
--$boundary
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

" . $textContent;

// Add HTML body.
$body .= "
--$boundary
Content-Type: text/html; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

" . $htmlContent;
  • 写回答

2条回答 默认 最新

  • dryk50495 2011-08-10 11:16
    关注

    You are using Content-Type: text/plain; charset=UTF-8 to tell the mail reader that such message part uses UTF-8, which is fine, but... What does the $textContent variable contain? That's the important bit. According to Content-Transfer-Encoding: 7bit, it's a 7 bit encoding so it can't be raw UTF-8. However, you are not using any of the usual 7-bit encodings used for e-mail. Otherwise, there would be a (e.g.) Content-Transfer-Encoding: quoted-printable header.

    To sum up, you need to:

    1. Have a source string that contains valid UTF-8.
    2. Pick a encoding for the transfer, such as quoted_printable_encode().
    3. Add a header to tell which transfer encoding you chose.

    You could also send the raw UTF-8 as-is and set Content-Transfer-Encoding: 8bit but I would not recommend it. You risk breaking the SMTP standard just by sending very long lines. Also, you have no idea of what kind of legacy programs this will go through.

    E-mail is harder than it seems, that's why sooner or later you end up using a third-party library: PHP Mailer, Swift Mailer, PEAR Mail...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 hc130怎么读写内部flash存储信息
  • ¥15 Axure rp9注册与登录交互
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面