Display correctly Greek characters at subject of an email

You can use this function when the subject at an email is not displayed correctly.

The idea is to base64_encode the subject header.  Something like

$subject_encoded=”=?UTF-8?B?”.base64_encode($subject).”?=”;

mail(…$subject_encoded);

Main source http://geoland.org/2007/12/utf8-ready-php-mail-function/

function UTF8_mail(
$from,$to,$subject,$message,$cc=”",$bcc=”"){

$from = explode(”<”,$from );

$headers =
“From: =?UTF-8?B?”
.base64_encode($from[0]).”?= <”
. $from[1] . “\r\n”;

$to = explode(”<”,$to );
$to = “=?UTF-8?B?”.base64_encode($to[0])
.”?= <”. $to[1] ;

$subject=”=?UTF-8?B?”
.base64_encode($subject).”?=\n”;

if($cc!=”"){
$cc = explode(”<”,$cc );
$headers .= “Cc: =?UTF-8?B?”
.base64_encode($cc[0]).”?= <”
. $cc[1] . “\r\n”;
}

if($bcc!=”"){
$bcc = explode(”<”,$bcc );
$headers .= “Bcc: =?UTF-8?B?”
.base64_encode($bcc[0]).”?= <”
. $bcc[1] . “\r\n”;
}

$headers .=
“Content-Type: text/plain; ”
. “charset=UTF-8; format=flowed\n”
. “MIME-Version: 1.0\n”
. “Content-Transfer-Encoding: 8bit\n”
. “X-Mailer: PHP\n”;

return mail($to, $subject, $message, $headers);

}

UTF8_mail(
“Κώστας Καρανταπαναθανασόπουλος <my@email.com>”,
“First Last <your@email.com>”,
“Θέμα Subject”,
“Κείμενο Text”,
“”,
“Κουτσός Σκύλος<hidden_friend@email.com>”
);

Leave a comment