Recently encountered a problem, is the use of PHP mail function to send utf-8 encoded in Chinese mail when the title garbled phenomenon, and the message is the correct text. Originally thought is the page coding problem, found that the page coding Utf-8 no problem ah, looking for a half-day reason, finally found the problem.
1. Mail class with PEAR
PEAR's Mail class allows you to choose to use the SendMail or SMTP way to send a letter, such a packaged interface is good, you do not need to reinvent the wheel.
2. About headers in the subject of garbled
Do not put any other than the Iso-8859-1 code directly into the subject, the correct way is to subject the text in accordance with RFC 2047 specification, 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 that you use GB2312 encoding in the header, because the MTA is so fragmented that most of the MSP uses its own developed MTA, and most of the MTA in China is capable of supporting GB2312 encoding anyway.
If the MSP is sent to a foreign country, it is recommended that you use the Base64 encoded UTF-8 text at subject because they probably do not support GB2312 encoding.
3. Be sure to add content-type in headers
e-Mail headers in the Content-type place similar to the Web page, is also Text/plain; charset=gb2312 such a format. This option is mainly for the client to receive mail meaningful, you can avoid in the client garbled, part of the Webmail will also be based on the contents of the message to encode the conversion to correct display. You can imagine the situation where you use the GB2312 code to send to. com.tw's mailbox.
4. Properly configure the host you use to send the message
1 A certain configuration of the reverse resolution, because most of the non-fixed IP can not do reverse analysis, part of the anti-spam strategy as a basis for judging.
2) A certain configuration MX resolution, the same truth.
3 Ensure that your return path is able to receive a refund, or your mail server may go to the receiving end of the blacklist.
$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 function Base64_encode ()-Encode data using MIME base64
The header string is encoded before the type example: =? UTF-8? B?
After the title string is added:? =
For example:
<?php
$to = "clairelume@qq.com";//change this email 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, send the Chinese message title is not garbled.
is not very simple, in fact, the problem analysis through, all the problems will be solved, I hope this article for everyone's learning help.