UTF8 ready php mail function

I have been struggling for some time now to create a php mail function that can handle UTF8 characters across most email readers. I think I have finally accomplished this and it might be useful to others so here it is.

The problem with the php mail is that it does not encode the names and subjects and they could get lost in the transport or be misinterpreted from the email readers. This function actually does the proper encoding and overcomes the php mail deficiency.

-----------------------------
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>"
);
-----------------------------

All this function is accomplishing is to encode each

Name<email@domain.com>

to

 =?UTF-8?B?zpzOuc+HzrHOu863z4I=?= <email@domain.com>

The emails themselves don't need to be encoded since an email conventionally can only consist of of latin characters but, we could also confuse the mail server if we did encode them.

It has not been extensively tested, therefore I can't guarantee that it would work with all possible mail readers. So please do send in your bug reports or any other comments.

Category: 

29 Comments

What you are saying Olaf might be correct and you might not need this if the input is indeed passed from a utf8 encoded form (have not tried it)

This was mostly created for one to send an email that did not involve a user form/input.

Yes I did need it for a website to send notification to the owner for some actions.

I would not have noticed that I needed such a function if he did not want his messages in yahoo.gr.

The real problem was only the FROM and the SUBJECT fields were always displaying characters like this "ΓιωΟÂ?Ξ³ΞÎ?Ο‚"

Other email clients such as gmail and hotmail were decoding the FROM and the SUBJECT correctly and the body of the message was always encoded/decoded correctly from all the clients tested.

The FROM field was actually hardcoded in the php file but I don't think it would have made a difference even if I got it from a UTF8 encoded field of a database.

I doubt that it was only a problem with yahoo.gr or the local yahoo mail because I did check with a few mail clients and they do encode their addresses like this
=?UTF-8?B?zpzOuc+HzrHOu863z4I=?=
when sending an email with UTF8 characters in it

I did check (just now) sending input from a UTF-8 encoded form


accept-charset="UTF-8" enctype="application/x-www-form-urlencoded"

and still yahoo.gr does not display FROM and SUBJECT correctly

i had so many problems with the utf8 so far (i started as a web developer in 1999) that i always try to convince my bosses not to use it, however they always want to _save_ development time by using it whatever platform we were using, either java or php or vignette.
for various reasons we always ended up with a longer dev-times and a lot of frustration.
so i don't like utf8, even though it _would be_ nice to have a universal encoding. but i think , well actually i hope :-( that utf8 is not the final solution for this task. what do you guys think?

Your function is great and very useful. Thanks.
I had the same problem with name and subject in greek.

There is just a formatting problem in the code.
The CR LF don't have the slashes, so it doesn't work when you copy/paste the code.

Thank you very much! It saved the day for me.

Gustavo (from Argentina)

Gustavo
thanks for the comments and the missing characters warning

I was trying just now to use either CODE tag or the PRE tag or both but I had no success.

I finally inserted the code as plain text in the wordpress editor, hope all the characters are still preserved.

I forgot once again to write the encoding of thus the emails where not shown in the example call to UTF8_mail

Check again

variables to from cc and bcc contain the name and email of recipients of this email

TO holds the recipient FROM holds the sender.

Thanks loads! Been wrestling with sending Chinese mail, and this sorted it.
Two minor things: had to change the \r\n to \n, and copying/pasting the code gave mangled double-quotes, but all rather easy.
Thanks again!

Hello!

Nice for the solution. Is there any way that the email sent with mail function doesn't ends in hotmail spam folders?

Thank you!

the example is in gibberish,
can you please send me a english one...
thank you in advance
francisco

hi, i cant find the inclined " on my keyboard. Does this have a shortcut (ALT + xxx)? I am having problem testing the code . Thanks for the code by the way. Ü I'm so excited that I already posted on this forum. thanks much!!

thanks much! I will test it once I get back on office. Its just that it won't show up properly on VI editor. Ill just replace it with quotes if that is the case. Thank you again GiorgosK .. Ü

This owns.

I have been struggling for quite some while now trying to make Entourage Mail Client show subjects containing characters like å, ä and ö. As it worked in Thunderbird and Microsoft Mail, I was devastated why the UTF-8 encoding, both set in header and of the HTML-mail, would not work.

The fix?

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

mail(...$subject_encoded);

-----

Thank you!

just done a quick try and DRSK's solution seems to work like a charm (pc + mac) with gereman umlaut's in the subject...

my post data comes from utf-8 so i simply
utf8_decode($_POST['body_text']) for the body...

greetings from berlin!

Hi,

first: thanks for the work. But I've got a problem with this function: when I insert "\n" to the message what I send it won't make a new line, just show the "\n" string in the recipient's mailbox. I also tried with "\r\n", it makes the same.

Could you please help?

indeed a very simple fix

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

you gotta love it... thx mate