note 62275 deleted from function.mail by bobby
| From: | bobby@php.net | Date: | Tue, 10 Oct 2006 20:52:38 +0000 |
| Subject: | note 62275 deleted from function.mail by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118368@lists.php.net to get a copy of this message | ||
Note Submitter: lg83 at free dot fr
----
If you would like to send an email with non-ASCII characters in the subject (or any other header),
this function will convert it into a 7 bit QP-encoded string that works with almost all mail clients
(you are NOT allowed to send raw 8 bit data in a header, and even if you did there would be no way
to know which charset was used).
This function takes a UTF-8 string in $s. If your PHP pages are in ISO-8859-1, just replace
"=?UTF-8?Q?" with "=?ISO-8859-1?Q?" below.
<?php
function encodeMimeSubject($s) {
$lastspace=-1;
$r="";
$buff="";
$mode=1;
for ($i=0; $i<strlen($s); $i++) {
$c=substr($s,$i,1);
if ($mode==1) {
$n=ord($c);
if ($n & 128) {
$r.="=?UTF-8?Q?";
$i=$lastspace;
$mode=2;
} else {
$buff.=$c;
if ($c==" ") {
$r.=$buff;
$buff="";
$lastspace=$i;
}
}
} else if ($mode==2) {
$r.=qpchar($c);
}
}
if ($mode==2) $r.="?=";
return $r;
}
function qpchar($c) {
$n=ord($c);
if ($c==" ") return "_";
if ($n>=48 && $n<=57) return $c;
if ($n>=65 && $n<=90) return $c;
if ($n>=97 && $n<=122) return $c;
return "=".($n<16 ? "0" : "").strtoupper(dechex($n));
}
?>