如何发送采用UTF-8编码的电子邮件? [英] How can I send email with UTF-8 encoding?

查看:325
本文介绍了如何发送采用UTF-8编码的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个脚本,该脚本以三种语言发送一些内容(所有内容均相同-重复),但是收到内容字符时,我认为这是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.

我说对了,我需要做的就是将字符集部分更改为utf-8,还是需要更改其他内容,例如7bit部分?

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 ?

您可以看到我在哪里插入了一个UTF-8参考(尚未测试)

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

这里有些 http://bitprison.net/php_mail_utf-8_subject_and_message 基本编码,但是我不确定是否在这里需要它?

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.\n";

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

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

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

我在这里查看了另一篇文章作为示例.

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

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

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

推荐答案

您正在使用Content-Type: text/plain; charset=UTF-8告诉邮件阅读器,这样的消息部分使用UTF-8,这很好,但是... 变量包含?这很重要.根据Content-Transfer-Encoding: 7bit,它是7位编码,因此不能是原始UTF-8.但是,您没有使用任何用于电子邮件的常用7位编码.否则,将会有一个(例如)Content-Transfer-Encoding: quoted-printable标头.

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.

总结起来,您需要:

  1. 具有包含有效UTF-8的源字符串.
  2. 选择用于传输的编码,例如 quoted_printable_encode().
  3. 添加标题以告知您选择的传输编码.

您还可以按原样发送原始UTF-8并设置Content-Transfer-Encoding: 8bit,但我不建议这样做.仅通过发送很长的行,您就有可能违反SMTP标准.另外,您也不知道它将经历哪种旧程序.

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.

电子邮件比看上去难,这就是为什么您迟早要使用第三方库的原因: PHP Mailer Swift Mailer

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

这篇关于如何发送采用UTF-8编码的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆