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 ht
tp://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($su
bject)."?=n";
$to=mail foo.com;
$body='This is the body';
$headers="From: ".$from."n"
. "Content-Type: text/plain; charset=$charset;
format=flowedn"
. "MIME-Version: 1.0n"
. "Content-Transfer-Encoding: 8bitn"
. "X-Mailer: PHPn";
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.
----
Server IP: 80.64.47.13
Probable Submitter: 80.38.114.98
----
Manual Page -- http:/
/www.php.net/manual/en/function.mail.php
Edit -- https://master
.php.net/note/edit/77458
Del: integrated -- h
ttps://master.php.net/note/delete/77458/integrated
Del: useless -- http
s://master.php.net/note/delete/77458/useless
Del: bad code -- htt
ps://master.php.net/note/delete/77458/bad+code
Del: spam -- https:/
/master.php.net/note/delete/77458/spam
Del: non-english --
https://master.php.net/note/delete/77458/non-english
Del: in docs -- http
s://master.php.net/note/delete/77458/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/77458
Reject -- https://mast
er.php.net/note/reject/77458
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|