PHP mail messing up Russian characters

Posted on 2008-11-17
Last Modified: 2012-05-05
I have a client that uses PHP's mail() function to send mail from a contact form. The problem is, if a Russian user uses it, the mail that is received by the client has messed up characters instead of the Russian characters.

When he tests it on the development server it works, but when on the live server it does that.

Any ideas other than "rather use this PHP mailer script"?
Question by:DarkHill
    LVL 9

    Accepted Solution

    I'm by no means an expert with the PHP's mail() command and prefer writing simple SMTP communication scripts with authentication support and such, but I'm quessing the character set can be messed up by either the production server's PHP configuration or the SMTP server. You could try different approaches based on whether the message is sent in text/plain or text/html.

    If in text/plain, be sure to use the set_locale() command to override the server's default character set and specify the mail header:
    Content-type: text/plain; charset=utf-8

    If sending text/html, you could always htmlentities() the entire message to be double sure, do the previous tricks and also include a HTML meta tag
    <meta http-equiv="Content-Type" content="text/html charset=utf-8" />
    of course the mail header should then be:
    Content-type: text/html; charset=utf-8

    Hope this'll get you closer to a solution.
    LVL 11

    Assisted Solution

    You need to convert all chars in header to an hexadecimal.
    Maybe this will work for you or will give you a clue to solve your trouble

    	function charsToHex($string) {
    		return preg_replace("#([ãáàâäéèêëíìîïõóòôöúùûüçñ])#ie",'"=". dechex(ord("\\1"))' .'"="',$string);
    $subject = "=?iso-8859-1?Q?" . charsToHex($subject) . "?=";

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now