How to format an email 'From' header that contains a comma
Asked Answered
D

3

39

The standard way to format the 'From' email header is

From: John Doe <[email protected]>

But what to do if there's a comma in the name?

From: John Doe, chief bottle washer <[email protected]>

If I do that, my MTA automatically converts this into:

From: [email protected], [email protected], chief bottle washer <[email protected]>

My first guess is to use double-quotes around the full name, but I can't find any official documentation confirming this and I'd like my emails to be readable by all email clients.

Developing answered 21/3, 2013 at 18:40 Comment(1)
This question and the answers have a surprising number of negative votes. The information seems good. Why the downvotes?Kennethkennett
D
35

To elaborate on the answer by @Fls'Zen, yes the proper method is to enclose the name in double-quotes.

From a practical point of view there's no harm in wrapping all names in double-quotes, just be sure to escape a double-quote if it appears in the display name \" (or just replace with a single-quote). But if you want to be completely by the spec, you shouldn't use the double quotes if you don't have to.

For all the dense details, E-mail header fields are defined by RFC 5322. The relevant section for multiple originators in the From header is 3.6.2, and the relevant sections for quoting delimiters is 3.2.1 and 3.2.4.

Developing answered 25/3, 2013 at 1:31 Comment(1)
Shouldn't you refer to RFC2822? Because RFC5322 isn't a propsed standard yet. It's still a draft.Tutelage
C
5

When the following regular expression matches, then an email display address must be quoted.

[^-A-Za-z0-9!#$%&'*+/=?^_`{|}~\s]

For ASCII characters, this can be done by escaping any double quote characters with a backslash, and enclosing the string in double quotes. For non-ASCII characters, the more complex MIME escaping is required.

Completion answered 6/7, 2017 at 12:49 Comment(3)
Whitespace characters are allowed. So this would be [^-A-Za-z0-9!#$%&\'*+-/=?^_`{|}~\\s]+ with \s being the white space character class.Thermobarograph
And the second - is too much. Like this it includes all characters between + and /. So the complete regex working for me is: [^-A-Za-z0-9!#$%&\'*+/=?^_`{|}~\\s]+ The ' only needs to be escaped when you enclose the string with it. So enclosing in " this would be correct: [^-A-Za-z0-9!#$%&'*+/=?^_`{|}~\\s]+Thermobarograph
Thank you, I incorporated your suggestions.Completion
V
3

E-mail header fields are defined by RFC 5322. The relevant section for multiple originators in the From header is 3.6.2. The relevant sections for quoting delimiters is 3.2.1 and 3.2.4.

Vizza answered 21/3, 2013 at 18:45 Comment(3)
If I'm interpreting this RFC correctly (and these things are by no means human readable), then the solution is to enclose in double quotes From: "John Doe, chief bottle washer" <[email protected]> ???Developing
Furthermore, is there any harm in wrapping all names in double quotes, regardless of whether a comma (or other illegal character) is present?Developing
Yes, that's the correct way. Using double quotes all the time doesn't hurt anything, just make sure to escape a double-quote if it appears in the display name.Dippy

© 2022 - 2024 — McMap. All rights reserved.