GeoLand.org


Web design, development, marketing, SEO, consulting

UTF8 ready php mail function

December 8th, 2007 by GiorgosK · 25 Comments

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.

Tags: programming · php · web development

25 responses so far ↓

  • 1 Olaf // Dec 20, 2007 at 11:30 am

    Nice code example, but is it not enough to post the email message through a utf-8 encoded form?

  • 2 GiorgosK // Dec 20, 2007 at 11:55 am

    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.

  • 3 Olaf // Dec 20, 2007 at 11:57 am

    … like sending data from script with data from a database? How is this data stored? (I’m sure you wrote this example because you needed it on some website :))

  • 4 GiorgosK // Dec 20, 2007 at 12:42 pm

    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

  • 5 GiorgosK // Dec 20, 2007 at 12:49 pm

    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

  • 6 just another web developer guy // Jan 10, 2008 at 6:19 pm

    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?

  • 7 Gustavo // Jan 13, 2008 at 2:05 am

    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)

  • 8 GiorgosK // Jan 15, 2008 at 2:44 pm

    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.

  • 9 Amoo // Mar 11, 2008 at 11:00 pm

    Hi. I just wonder where I shoul modify the sender and reciever e-mail addresses???

  • 10 GiorgosK // Mar 11, 2008 at 11:43 pm

    I forgot once again to write the encoding of < and > 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.

  • 11 dpdhunt // Apr 26, 2008 at 9:42 am

    Thanks for that info

  • 12 Bob the Fish // Jun 18, 2008 at 3:32 pm

    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!

  • 13 osbti // Jun 30, 2008 at 3:19 am

    Good stuff. interesting.

  • 14 Nikos Dimitrakopoulos // Sep 16, 2008 at 7:38 pm

    Great! That was really helpful! I hate the way PHP handles (not) non-latin characters. I hope that all these issues will get fixed in PHP 6. Until then, thanks a lot for this handy script :)

  • 15 xoxo // Oct 19, 2008 at 2:19 am

    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!

  • 16 francisco // Dec 29, 2008 at 2:40 pm

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

  • 17 GiorgosK // Dec 29, 2008 at 2:55 pm

    fransisco you can put anything you want withing the quotes “” …

  • 18 John Moore // Mar 2, 2009 at 7:54 pm

    Thanks so much. This fixed a problem I have been working on, off and on, for about a year. Nice job and thanks for sharing it. www.freeburmarangers.org can now send Thai reports with actual correctly formed subject lines.

  • 19 dhabz // Mar 8, 2009 at 9:58 am

    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!!

  • 20 GiorgosK // Mar 9, 2009 at 6:45 pm

    Its does not have to be an inclined ” (quote) its just the way my theme shows the quotes

  • 21 dhabz // Mar 11, 2009 at 3:52 am

    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 .. Ü

  • 22 MilanC // Mar 11, 2009 at 11:57 am

    Hi, thank you for working example.

  • 23 kutt // Mar 18, 2009 at 1:40 pm

    thnks from estonia! and thank you, google :)

  • 24 toskydao // Jun 1, 2009 at 2:24 pm

    Thanks a lot, I appreciate your work !

  • 25 DRSK // Sep 4, 2009 at 11:20 am

    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!

Leave a Comment