Php mail function to send UTF-8 encoding Chinese mail Title garbled solution

Source: Internet
Author: User
This article mainly introduces the php mail function to send UTF-8 encoding Chinese mail Title garbled solution, need friends can refer to recently encountered a problem, the title of a UTF-8-encoded Chinese email is garbled while the mail body is correct. I initially thought it was a page encoding problem and found that there was no problem with page encoding UTF-8. I found the cause for half a day and finally found the problem.

1. use the PEAR Mail class

The PEAR Mail class allows you to send emails using sendmail or SMTP. This encapsulated interface is very useful and you do not need to re-invent the wheel on your own.

2. garbled text about subject in headers

Do not put any text except the ISO-8859-1 code directly into the subject, the correct way is to make the text in the subject conform to RFC 2047, assuming that the text at your subject is encoded as GB2312, soCorrect subject It should be in this format
$ Headers ['subobject'] = '=? GB2312? B? '. Base64_encode ('title text ').'? = ';
If it is UTF-8, replace the above beginning of GB2312 with a UTF-8. We recommend that you use GB2312 encoding in the title. because MTA is very poorly divided, most MSPs use self-developed MTA, I believe that most of the Chinese MTA can support GB2312 encoding in any case.

If it is sent to a foreign MSP, we recommend that you use base64 encoded UTF-8 text at the subject, because they probably do not support GB2312 encoding.

3. content-type must be added to headers.

Content-type in the headers of an email is similar to a webpage in the format of text/plain; charset = gb2312. This option is mainly meaningful to the client that receives the mail, so as to avoid garbled characters on the client. some Webmail will also convert the encoding of the Mail content according to the settings here for correct display. You can imagine the situation when you sent the email address to .com.tw in gb2312 encoding.

4. correctly configure the host you use to send emails

1) reverse resolution must be configured, because most non-fixed IP addresses cannot do reverse resolution, and some anti-spam policies use this as the judgment basis.
2) MX resolution must be configured. The principle is the same as above.
3) make sure that your return path can receive the bounce message. Otherwise, your email sending server may be blacklisted at the receiving end.

$subject = stripslashes($the_post['Title']); $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 = stripslashes(strip_tags($the_post['Content'])); mail($to, $subject, $message, $headers); 

First use the function base64_encode ()-encode the data using MIME base64
The header string is prefixed with the encoding type, for example: =? UTF-8? B?
The title string is followed :? =
For example:

<? Php $ to = "clairelume@qq.com"; // change this email address to your own. $ Name = $ _ POST ['name']; $ email = $ _ POST ['email ']; $ subject = "emails from blog readers "; $ 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 email has been sent. ';?>

In this way, the title of the Chinese email is not garbled.

Is it very simple? in fact, I have thoroughly analyzed the problem, and all the problems will be solved. I hope this article will be helpful for everyone's learning.

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.