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.
I also found out that brackets in address-field don't work: (Is there an error in PHP's imap_fetch_overview()-function when reading headers with brackets?). 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>
.
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?
For example, PHP has no problems with brackets in the subject, although the Subject is also part of the header.
It seems that quotes can help me with the problem (https://www.rfc-editor.org/rfc/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?
In https://www.rfc-editor.org/rfc/rfc5322#section-3.2.4 it says:
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>
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>