Search code examples
phpheaderemailquotes

PHP mail: use quotes in $to and in from mail header or NOT?


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!


Solution

  • 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.