Here is a simple UTF-8 mail sender function. This function also encode subject and plain-text message to UTF-8. If you need HTML mail sender, change the code in line 4 from text/plain to text/html, but this function is usable the most cases without any modification. I've used this function for webform and other web based mail notification.
<?php
function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);
}
?>You can copy & paste or download it:
mail_utf8.php [274 byte]
Blog categories: Programming
Comments
Submitted by GiorgosK (not verified) on December 7, 2007 - 1:14pm Permalink
How would you encode the
Thank, nice piece of code,
But how do you encode the Headers "From:" field if it contains Unicode characters ?
Submitted by Gwouite (not verified) on December 22, 2007 - 9:54pm Permalink
Here is your solution !
For the from field, like the subject, you have to base64_encode it !
Example :
And you can do the same for CC header !
Well done ;)
Best regards. Guillaume.
Submitted by Tomas (not verified) on September 19, 2010 - 1:59pm Permalink
Hello I have the same
Hello I have the same problem. I have form from which you can send email with attachment. When I send it without attachment everything works but when I add one the headers are encoded correctly but the email is screwed. It arrives like so:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_x6dd57d3a9e00235dcd8208baf5d169f9x"
This is a multi-part message in MIME format.
--==Multipart_Boundary_x6dd57d3a9e00235dcd8208baf5d169f9x
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit
Guru Pitka
Mariska Hargitay
--==Multipart_Boundary_x6dd57d3a9e00235dcd8208baf5d169f9x
Content-Type: image/jpeg;
name="=?utf-8?B?ZnJhdS3FocSbxI3FmcW+w73DocOtw6kuanBn?="
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAgAAZABkAAD//gASQWRvYmUgSW1hZ2VSZWFkef/sABFEdWNreQABAAQAAAA8
AAD/7gAOQWRvYmUAZMAAAAAB/9sAhAAGBAQEBQQGBQUGCQYFBgkLCAYGCAsMCgoLCgoMEAwMDAwM
DBAMDg8QDw4MExMUFBMTHBsbGxwfHx8fHx8fHx8fAQcHBw0MDRgQEBgaFREVGh8fHx8fHx8fHx8f
.....
My code is:
Can you tell me what I am doing wrong.
Submitted by reishi (not verified) on September 1, 2008 - 7:26pm Permalink
Trailing "?="
You need a trailing "?=" after the base64-encoded string.
"=?UTF-8?B?".base64_encode($subject)."?="Best regards.
Submitted by Thomas on September 27, 2008 - 3:01am Permalink
Thank you!
I've fixed it. Thank you!
Submitted by Visitor (not verified) on January 21, 2009 - 10:42am Permalink
you need to fix it in the
you need to fix it in the file as well
Submitted by Thomas on January 22, 2009 - 9:43am Permalink
Thanks
Thank you, your comment. I have fixed it also.
Submitted by Robbie (not verified) on December 22, 2008 - 5:13pm Permalink
Thanks 365 days later...
Exactly a year later, I found this information extremely useful. Solved my problem with strange characters in the FROM portion. Thanks!
Submitted by Visitor (not verified) on February 15, 2009 - 2:11am Permalink
Using the function instead of 'mail'
To use this function instead of standard PHP's 'mail', you can modify function like this:
Then, in your main code you will be able to check if subroutine mail had sent the mail successfully.
Althogh, I'm not a pro in PHP, but it works for me ;)
Submitted by Another Visitor (not verified) on September 18, 2009 - 2:05pm Permalink
simpler
Or just like this!:
(you dont have to first assign the result of mail to $send_message, and then check if $send_message is true (and if so return true) or false (and if so return false))
Submitted by boediger (not verified) on February 15, 2009 - 8:46pm Permalink
Exactly a year later, I
Exactly a year later, I found this information extremely useful. Solved my problem with strange characters in the FROM portion. Thanks!
Submitted by Hrvoje (not verified) on April 24, 2009 - 6:18pm Permalink
Thanks!! I've Googled
Thanks!!
I've Googled several hours for solution for my (same) problem and your function helped.
Cheers from Croatia :)
Submitted by blackspot (not verified) on April 28, 2009 - 11:49am Permalink
After 1.5 years from its
After 1.5 years from its posting it has served me well. Perfect soution for my Japanese Subject and From fields! Thank you so much!
Submitted by eliezer (not verified) on May 7, 2009 - 9:40pm Permalink
Dude you must be a deep source of knowledge
How did you come up with this solution? It worked GREAT!
Submitted by Robin (not verified) on July 14, 2009 - 1:48pm Permalink
Great
It worked great for sending sweedish characters, thanks a million!
Submitted by Visitor (not verified) on July 15, 2009 - 1:17pm Permalink
utf8 doesn't show properly in subject
Hi I tried your code but it doesn't show the utf8 code in the subject. Can you help. Thanks.
Submitted by Pierrick (not verified) on July 31, 2009 - 12:16am Permalink
Thank you
Thank you for the solution. It was very useful. My forum members were complaining about missing characters in the new reply notification emails.
Submitted by TheKing (not verified) on August 25, 2009 - 1:42am Permalink
Newbiiieeeeee
Hi guys !
Im really new in php but I got a script that I want to work with utf-8.. Is there anybody who knows what I need to change in this code !? .. thanks and sorry for the noob question..
Submitted by Hotaka (not verified) on August 28, 2009 - 1:35am Permalink
This seems to work okay when
This seems to work okay when the email arrives in my GMail inbox, but when it arrives in my Outlook all the special characters are completely stripped out.
Submitted by Victor Ocampo (not verified) on January 12, 2010 - 6:25pm Permalink
Great! Working on php 5, 2 years and a half afer this post!
Thanks alot!!
In the same way you can add UTF8 to sender name, receiver name, and subject, thanks!
Submitted by Blutarsky (not verified) on July 7, 2010 - 12:08am Permalink
What about the body?
I see you encode the subject only. Will it work also on the body content?
Submitted by Wiyono (not verified) on July 9, 2010 - 9:16pm Permalink
I have code like this
Hallo,
I have code like this how can i put that:
$sendTo = "wiyono4@msn.com";
$subject = "hm-wortart Website kontakt";
$headers = "From: " . $_POST["vorname"] ." ". $_POST["nachname"] . "";
$headers .= "nE-Mail: " . $_POST["email"];
$message .= "Name: " . $_POST["vorname"];
$message .= "\nName: " . $_POST["nachname"];
$message .= "\nE-Mail: " . $_POST["email"];
$message .= "\nBetreff: " . $_POST["subject"];
$message .= "\nMessage: " . $_POST["nachricht"];
mail($sendTo, $subject, $message, $headers);
echo ("Vielen Dank, Ihr Beitrag wurde gesendet!");
?>
Can you send me email, when you have solution?
Thank you so much..
Submitted by venoel (not verified) on July 18, 2010 - 8:03pm Permalink
Great. But body of email
Great. But body of email still broken
Submitted by Zane (not verified) on September 9, 2010 - 12:53am Permalink
Thanks!
Those f*** "È" (Italian) in my subjects were productivity sinkholes.... thanks for your tip!
Submitted by Drugelis (not verified) on October 5, 2010 - 2:04pm Permalink
Thanks! This code is simply
Thanks! This code is simply great! Cudos to the author.
Submitted by flashfs (not verified) on December 9, 2010 - 2:38pm Permalink
It helped med
My message was good, except for the subject, that showed strange characters. Your code helped me solve this.
Add new comment