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 ();
|