views:

40

answers:

3

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 <[email protected]>

Do I have to call:

mail("\"User One\" <[email protected]>", ...

OR

mail("User One <[email protected]>", ...

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!

A: 
Sarfraz
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
@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" <[email protected]>,`.
Sarfraz
@Sarfraz Ahmed: I don't understand what you mean, actually if a send an email to my Outlook Express from "\"User One\" <[email protected]>" my Outlook Express shows in the From: User One and not "User One" (with quotes).
Marco Demajo
@Sarfraz yup, that seems to be indeed the right way.
Pekka
@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
@Marco: I have updated my answer, double quotes seem to be there in RFC but as @Pekka said they seem to be optional.
Sarfraz
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
@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
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
+1  A: 

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
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
+2  A: 

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