1

Do we need to use quotes in $to and in from/cc/bcc mail headers when using PHP mail function?

I mean, let's say I want to send mail to:

User One <user@domain.com>

Do I have to call:

mail("\"User One\" <user@domain.com>", ...

OR

mail("User One <user@domain.com>", ...

I suppoose once you give me an answer for the $to, it is going to be the same for other mail headers, that I normally add in this way:

$mail_headers  = "From: " . $from . "\r\n";      
$mail_headers .= "Cc: " . $cc . "\r\n";
$mail_headers .= "Bcc: " . $bcc . "\r\n";
$mail_headers .= "MIME-Version: 1.0\r\nContent-type: text/plain;\r\n\tcharset=\"Windows-1252\";\r\nContent-Transfer-Encoding: 8bit" . "\r\n";      
//I use "Windows-1252" charset, cause "iso-8859-1" DOES NOT DISPLAY EURO CHAR!

mail($to, $subject, $body, $mail_headers);

Maybe I need to use quotes in case there is a single quote in header? I don't know sometimes I saw examples with quotes, other time without them, does anyone know, and maybe explain.

Thanks!

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159

3 Answers3

3

If I read the relevant RFC right:

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

A quoted-string is treated as a unit. That is, quoted-string is identical to atom, semantically.

the correct character to wrap a string in is the double quote " (and only that), but it is optional.

I would highly recommend using it, though, if your recipient name contains spaces.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

The recipient must conform with RFC2822 (see the PHP doc for the mail function).

Since the actual recipient is what's between < and > it doesn't really matter whether you use quotes or not - the mail will be sent to the same person; but his own e-mail client may display it differently.

In the documentation they do list examples without quotes; I'd tend to do it that way too.

Joubarc
  • 1,206
  • 10
  • 17
  • But just because I'd do it that way doesn't mean it's good :-/ - pekka is probably right that you should put some. The only thing I can say with definite certitude is: if you put an opening quote, use a closing one as well. Which isn't too useful an advise, I know. – Joubarc Jun 19 '10 at 16:22
  • Actually, I just had a problem with an address of the type `A, B `, as the server considered the comma to be a separator between two email addresses, thereby causing a send to `A` which is an invalid address; the correct formatting seems to have been `"A, B" `. Probably related: https://www.addedbytes.com/blog/code/email-address-validation/ and http://stackoverflow.com/questions/12008720/what-are-special-characters-in-e-mail-headers-and-when-to-use-quotes – sdaau Jul 07 '15 at 02:13
0

This:

mail("\"User One\" <user@domain.com>", ...

will output:

"User One" <user@domain.com>

while this:

mail("User One <user@domain.com>", ...

will output:

User One <user@domain.com>

Update:

I don't have a link to RFC, but just noticed that when you compose an email with Gmail, it puts in like:

"Sarah Chafer" <sarah.chafer@domain.com>, 

See the doubles quotes there.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • This doesn't answer the question yet. There is a RFC defined way to do this, and if I remember correctly, it even defines whether to use single or double quotes. (I'd like to know the answer to this as well.) – Pekka Jun 19 '10 at 15:04
  • @Pekka: It will be interesting to see that but I noticed that when you compose email with gmail, it puts it in like `"Sarah Chafer" ,`. – Sarfraz Jun 19 '10 at 15:06
  • @Sarfraz Ahmed: I don't understand what you mean, actually if a send an email to my Outlook Express from "\"User One\" " my Outlook Express shows in the From: User One and not "User One" (with quotes). – Marco Demaio Jun 19 '10 at 15:08
  • @Marco yes, that's exactly the way it's intended. The quotes are to wrap the string if it contains spaces, but they won't be displayed. – Pekka Jun 19 '10 at 15:08
  • @Marco: I have updated my answer, double quotes seem to be there in RFC but as @Pekka said they seem to be optional. – Sarfraz Jun 19 '10 at 15:09
  • 1
    The RFC seems to treat everything outside < > as comments or otherwise white space; which basically means it doesn't matter. What's inside the < > is the actual address. – Joubarc Jun 19 '10 at 15:11
  • @Joubarc I'm pretty sure I have seen instances of problems when quotes were not used, but I can't find an example right now. – Pekka Jun 19 '10 at 16:11
  • Could be, I must admit your reading of the RFC makes more sense than mine. What kind of problems? Undelivered mails? But yeah, in doubt, the double quotes shouldn't hurt. – Joubarc Jun 19 '10 at 16:20