PHP Mail send the message title Chinese garbled Problem Solving method

Source: Internet
Author: User

When using the following PHP statement to send e-mail, if the encoding and receiving mailbox encoding is not the same, will find the message title is garbled, and the message body is correct, how to make the message title is not garbled?

$subject = stripslashes ($the _post[' Title '));
$headers = "Mime-version:1.0rn";
$headers. = "Content-type:text/plain; Charset=utf-8rn ";
$headers. = "CONTENT-TRANSFER-ENCODING:8BITRN";
$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:

  $subject = "=? UTF-8? B? ". Base64_encode ($subject). "? =";

Add the above sentence to the code, so that the Chinese message headers sent are not garbled.

The code is as follows Copy Code

<?php
$to       = ' junhuibai@gmail.com ';
$subject = Iconv (', ' GB2312 ', ' dear '. $s _user. ', please get your password! ');
$subject = "=? GB2312? B? ". Base64_encode ($subject). "? =";
$message = $s _user. ' Hello! '.
                ' Your new password is: '. $pwd. '.
                ' to ensure the security of your users, Please log in to change your password. '.
                ' This letter is issued by the system, The system does not receive reply, please do not reply directly! ';

$headers = ' from:junhuibai@tom.com '. "RN".
    ' reply-to:junhuibai@tom.com '. "RN".
    ' x-mailer:php/'. phpversion ();

If (Mail ($to, $subject, $message, $headers))
Echo ' OK ';
Else
Echo ' no ';
Phpinfo ()

/tr>

Final Solution

The code is as follows Copy Code

Title garbled:


$subject = "=? UTF-8? B? ". Base64_encode (' Mailbox Verification-'. site_name). "? =";

Text garbled:

Set Header:charset=utf-8 '


$headers = ' from:webmaster@webinno.cn '. "RN".
' Content-type:text/html; Charset=utf-8 '. "RN".
' Reply-to:webmaster@example.com '. "RN".
' x-mailer:php/'. Phpversion ();

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.