note 77458 deleted from function.mail by danbrown

From: Date: Sat, 25 Apr 2009 14:58:49 +0000
Subject: note 77458 deleted from function.mail by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153733@lists.php.net to get a copy of this message
Note Submitter: omgs ---- I haven't seen in this page a reference about how to properly handle subject encoding when using non-ascii characters. I've found that info at http://www.johanvanmol.org/content/view/34/37/1/3/, which I paste: "According to RFC 2822, mail header fields, including the subject, MUST be composed of printable US-ASCII characters (i.e., characters that have values between 33 and 126, inclusive). So if you want a subject with accents, you must encode it from your original character set to a US-ASCII character set. There are 2 of ways to do this: quoted-printable or base64. [...] Now we have an encoded subject, but our mail reader won't know that. So we need to tell it by formatting our subject as follows: "=?" charset "?" encoding "?" encoded-text "?=" , where charset is the original character set and encoding is either "Q" for Quoted-Printable or "B" for Base64. E.g The subject containing the Quoted-Printable ISO-8859-1 string "Voilà une message", is written as: Subject: =?ISO-8859-1?Q?Voil=E0_une_message?= The Base64 version of the ISO-8859-1 string is: Subject: =?ISO-8859-1?B?Vm9pbOAgdW5lIG1lc3NhZ2U=?= The Quoted-Printable version of the UTF-8 string is: Subject: =?UTF-8?Q?Voil=C3=A0_une_message?= The Base64 version of the UTF-8 string is: Subject: =?UTF-8?B?Vm9pbMOgIHVuZSBtZXNzYWdl?= " "Raw" non-encoded subjects can work and modern mail clients handle it properly, but I found that at least using utf-8 as encoding, the spam analizers complain stating "BAD HEADER Non-encoded 8-bit data". To prevent this, and taking the info above, I decided to use base64, which at least seems to have specific functions (and because it works, of course). So, one could use the following code: <?php ... $charset='UTF-8'; $subject='Subject with extra chars: áéíóú'; $encoded_subject="=?$charset?B?".base64_encode($subject)."?=\n"; $to=mail@foo.com; $body='This is the body'; $headers="From: ".$from."\n" . "Content-Type: text/plain; charset=$charset; format=flowed\n" . "MIME-Version: 1.0\n" . "Content-Transfer-Encoding: 8bit\n" . "X-Mailer: PHP\n"; mail($to,$encoded_subject, $body,$headers); ?> Of course, this can be "enhanced" by encoding only if there are non-ASCII characters, but I don't think I need it. Maybe the CPU work, used time and results don't deserve it.

« previous php.notes (#153733) next »