Submitted by Giorgos on
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.
29 Comments
Submitted by Olaf (not verified) on
Nice code example, but is it not enough to post the email message through a utf-8 encoded form?
Submitted by Giorgos on
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.
Submitted by Olaf (not verified) on
... 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 :))
Submitted by Giorgos on
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
Submitted by Giorgos on
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
Submitted by just another we... (not verified) on
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?
Submitted by Gustavo (not verified) on
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)
Submitted by Giorgos on
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.
Submitted by Amoo (not verified) on
Hi. I just wonder where I shoul modify the sender and reciever e-mail addresses???
Submitted by Giorgos on
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.
Submitted by dpdhunt (not verified) on
Thanks for that info
Submitted by Bob the Fish (not verified) on
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!
Submitted by osbti (not verified) on
Good stuff. interesting.
Submitted by Nikos Dimitrako... (not verified) on
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 :)
Submitted by xoxo (not verified) on
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!
Submitted by francisco (not verified) on
the example is in gibberish,
can you please send me a english one...
thank you in advance
francisco
Submitted by Giorgos on
fransisco you can put anything you want withing the quotes "" ...
Submitted by John Moore (not verified) on
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.
Submitted by dhabz (not verified) on
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!!
Submitted by Giorgos on
Its does not have to be an inclined " (quote) its just the way my theme shows the quotes
Submitted by dhabz (not verified) on
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 .. Ü
Submitted by MilanC (not verified) on
Hi, thank you for working example.
Submitted by kutt (not verified) on
thnks from estonia! and thank you, google :)
Submitted by toskydao (not verified) on
Thanks a lot, I appreciate your work !
Submitted by DRSK (not verified) on
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!
Submitted by Jonathan Foucher (not verified) on
This does not seem work if there are multiple recipients... But as my $to field did not contain any utf8 characters, I simply deleted it from the function, but just so you know...
Submitted by Akeem (not verified) on
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!
Submitted by somebody (not verified) on
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?
Submitted by Gyuri (not verified) on
indeed a very simple fix
$subject = "=?UTF-8?B?".base64_encode($subject)."?=\n";
you gotta love it... thx mate