电子邮件标题中的特殊字符是什么?何时使用引号? [英] What are special characters in E-Mail-Headers and when to use quotes?

查看:319
本文介绍了电子邮件标题中的特殊字符是什么?何时使用引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP发送和阅读电子邮件。到目前为止,我发现我必须使用 mb_encode_mimeheader()函数对特殊字符进行编码,但是我不必对空格进行编码。

I'm trying to send and read e-mail using PHP. So far I found out, that I have to encode special characters using the function mb_encode_mimeheader(), But I don't have to encode spaces.

我还发现,地址栏中的括号不起作用:(http://stackoverflow.com/questions/11989915/is-there-an-error-in-phps-imap -在阅读标题-w时获取概述功能)。例如,PHP无法访问标头部分发件人:Admin []< user@mail.tld> ,但是可以读取标头部分发件人: Admin []< user@mail.tld>

I also found out, that brackets in adress-filed don't work: (http://stackoverflow.com/questions/11989915/is-there-an-error-in-phps-imap-fetch-overview-function-when-reading-headers-w). For example PHP is unable to the header-section From: Admin [] <user@mail.tld>, but can read the header-section From: "Admin []" <user@mail.tld>.

因此,显然,括号在邮件中具有特殊含义,标头(至少对于PHP)。 邮件头中有什么特殊字符它们的含义,以及需要在何处进行编码/引用?

So, obviously brackets have a special meaning in the mail-header (at least for PHP). What are there for special characters in the Mailheader, what ist their meaning, and where do they need to be encoded/quoted?

例如,尽管主题也是标头的一部分,但PHP的主题中的括号没有问题。

For example, PHP has no problems with brackets in the subject, although the Subject is also Part of the header.

看来引号可以帮助我解决该问题(http://tools.ietf.org/html/rfc5322#section-3.2.4-我仍然不是100%确定这是PHP的问题还是不正确的邮件头)。 但是如何使用引号,以及引号会转义什么呢?

It seems that quotes can help me with the problem (http://tools.ietf.org/html/rfc5322#section-3.2.4 - I'm still not 100% sure if this is a problem of PHP or an incorrect mailheader). But how to use quotes, and what is escaped by quotes?

http://tools.ietf.org/html/rfc5322#section-3.2.4 它说:


包含除原子中允许的
以外的其他字符的字符串可以用带引号的字符串格式表示,其中
字符被包围引用(DQUOTE,ASCII值34)
个字符。

Strings of characters that include characters other than those allowed in atoms can be represented in a quoted string format, where the characters are surrounded by quote (DQUOTE, ASCII value 34) characters.

因此,我现在应该转义/引用每个字符他自己的

So, should I now "escape/quote" each character on his own

From: Admin "[""]" <user@mail.tld>

还是将所有内容一起引用会很好吗?

or is it fine to quote everything together?

From: "Admin []" <user@mail.tld>

但是,如果其他控制序列用引号引起来,会发生什么情况?例如,我的字符串中有特殊字符ÄÖÜ,它们被编码为 =?UTF-8?B?w4PChMODwpbDg8Kc?= 因此,按照RFC的要求,引用和编码字符串仍然可以吗?

But what happens, if other control sequences are enclosed withing the quotes? For example I have the special characters ÄÖÜ within my String, which are encoded to =?UTF-8?B?w4PChMODwpbDg8Kc?=. So, will 'quoted AND encoded' strings still be fine according to the RFC?

From: "Admin [=?UTF-8?B?w4PChMODwpbDg8Kc?=]" <user@mail.tld>


推荐答案

如果您使用RFC2047,则最好对

If you have RFC2047, you might as well encode the entire header as RFC2047 and forget about quoting.

显然,您已经找到了RFC5322,这是需要引用什么以及为什么引用的权威信息。基本上,凡是不属于电子邮件地址的内容,都必须引用具有电子邮件地址含义的内容。传统的报价机制是反斜杠和/或双引号,但是使用MIME,您可以使用可用的MIME编码轻松地对所有内容进行透明编码。

Apparently you already found RFC5322, which is the authoritative source on what needs to be quoted and why. Basically, anything which has a meaning as an email address needs to be quoted if it's not part of an email address. The traditional quoting mechanism was backslash and/or double quotes, but with MIME, you can easily encode everything transparently with the available MIME encodings.

您提供的链接说明了字符在原子中不允许使用的元素需要引用。原子中允许使用的字符列表在上一节中。

The link you gave explains that characters which are not permitted in "atoms" require quoting. The list of characters which are permitted in atoms is in the previous section.


ALPHA / DIGIT /    ; Printable US-ASCII
                   "!" / "#" /        ;  characters not including
                   "$" / "%" /        ;  specials.  Used for atoms.
                   "&" / "'" /
                   "*" / "+" /
                   "-" / "/" /
                   "=" / "?" /
                   "^" / "_" /
                   "`" / "{" /
                   "|" / "}" /
                   "~"


如果您对ASCII表进行交叉检查,您会得到

If you cross-check against the ASCII table, you get

32   (space)                    not OK
33 !                            OK
34 "                            not OK
35 # through $%& 38             OK
39 ' through () 41              not OK
42 * through + 43               OK
44 ,                            not OK
45 -                            OK
46 .                            not OK
47 / through 0123456789 57      OK
58 : through ;< 60              not OK
61 =                            OK
62 >                            not OK
63 ?                            OK
64 @                            not OK
65 A through BCD...XYZ 90       OK
91 [ through \] 93              not OK
94 ^ through _ 95               OK
96 `                            not OK
97 a through bcd...xyz{|}~ 126  OK
127 DEL                         not OK

在某些情况下,上面的加点(句号,句点,ASCII 46)允许使用点原子

In some contexts, the set "dot-atom" which is the above plus dot (full stop, period, ASCII 46) are permitted without quoting.

某些cli ents显然在谨慎方面犯了错误(有些人只会将所有内容都用双引号引起来,就好像您的真实姓名不是您的真实姓名一样。

Some clients obviously err on the cautious side (some will simply put everything in double quotes, as if your real name wasn't really your real name. That sucks).

我的理解是,在允许一个原子的地方允许RFC2047序列,但这意味着它不能与另一个原子相邻。无论如何,我会提出建议,甚至不建议将引号和RFC2047换行符混合在同一标头中,而不是弄清楚它们之间的交互方式(然后可能发现您的解释不是镇上唯一的游戏,因为其他人一个错误,或者是因为该规范有多种有效解释)。

My understanding is that an RFC2047 sequence is permitted where an atom is permitted, but that means that it cannot be adjacent to another atom. Anyway, I would cop out and recommend against even trying to mix quoting and RFC2047 wrapping in the same header, instead of possibly figuring out how they interact (and perhaps then find that your interpretation is not the only game in town, either because others made a mistake when figuring it out, or because there are multiple valid interpretations of the spec).

这篇关于电子邮件标题中的特殊字符是什么?何时使用引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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