PHP mail function to send UTF-8 encoding Chinese mail when the title garbled solution, _php tutorial

Source: Internet
Author: User
Tags pear

PHP's mail function to send UTF-8 encoding Chinese mail when the title garbled solution,


Recently encountered a problem, is to use the PHP mail function to send UTF-8 encoded Chinese messages when the title garbled phenomenon, and the message body is correct. Originally thought is the page coding problem, found that the page encoding Utf-8 no problem ah, find a half-day reason, finally found the problem lies.

1. Using the PEAR Mail class

PEAR's Mail class allows you to choose to use SendMail or SMTP to send letters, so that the well-packaged interface is very useful, you do not need to reinvent the wheel.

2. Garbled characters of subject in headers

Do not put any other than iso-8859-1 encoding text directly into the subject, the correct way is to let subject inside the text conforms to RFC 2047 specifications, assuming that your subject text encoding is GB2312, then the correct subject this should be the format
$headers [' subject '] = ' =? GB2312? B? '. Base64_encode (' title text '). '?=';
If it is UTF-8, replace the GB2312 at the beginning of the above with UTF-8. It is recommended to use GB2312 encoding at the title, because the MTA is so fragmented that most of the MSPs use their own MTA, and most of the MTA in the country can support GB2312 encoding anyway.

If it is sent to a foreign MSP, it is recommended that you use the Base64 encoded UTF-8 text at subject, as they most likely do not support GB2312 encoding.

3. Be sure to add content-type in headers

e-Mail headers in the Content-type of similar pages, but also Text/plain; charset=gb2312 such a format. This option is mainly for the client to receive the message is meaningful, can avoid garbled in the client, part of the Webmail will also be based on the settings here to encode the content of the message to correct display. You can imagine the situation when you send the GB2312 code to the. com.tw mailbox.

4. Correct configuration of the host you use to send mail

1) must be configured to reverse resolution, because most of the non-fixed IP can not do reverse resolution, some anti-spam policies as a basis for judgment.
2) must be configured MX resolution, the same reason.
3) Make sure your return path is able to receive the return letter, otherwise your mail sending server may enter the blacklist on the receiving side.

First Use function Base64_encode ()-Encode data using MIME base64
The title string is pre-encoded type such as: =? UTF-8? B?
Title string after add:? =
For example:

<?php   $to = "clairelume@qq.com";//change this e-mail address to your own.   $name = $_post[' name '];   $email = $_post[' email '];   $subject = "Mail from a blog reader";   $subject = "=? UTF-8? B? ". Base64_encode ($subject). "? =";   $headers = "mime-version:1.0\r\n";   $headers. = "Content-type:text/plain; Charset=utf-8\r\n ";   $headers. = "content-transfer-encoding:8bit\r\n";   $message = $_post[' message '];   Mail ($to, $subject, $message, "from:". $email. "", $headers);   Echo ' OK ' $name. ', your message has been sent. ';  

In this way, the sent Chinese message header is not garbled.

is not very simple, in fact, the analysis of the problem, all the problems will be solved, I hope this article is helpful to everyone's study.

http://www.bkjia.com/PHPjc/1063232.html www.bkjia.com true http://www.bkjia.com/PHPjc/1063232.html techarticle PHP mail function to send UTF-8 encoding Chinese mail when the title garbled solution, recently encountered a problem, is to use the PHP mail function to send UTF-8 encoded Chinese message when the title appears ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.