PHPMailer solves Chinese garbled characters in emails

Source: Internet
Author: User
We have introduced PHPMailer's usage and instance code. some friends found that when sending a Chinese email, the title and content are garbled characters. how can this problem be solved?

We have introduced PHPMailer's usage and instance code. some friends found that when sending a Chinese email, the title and content are garbled characters. how can this problem be solved?

Let's take a look at the previous example code mail. php file:

Require 'class. phpmailer. php ';

$ Mail = new PHPMailer;

$ Mail-> IsSMTP (); // Set mailer to use SMTP
$ Mail-> Host = 'smtp1 .example.com; smtp2.example.com '; // Specify main and backup server
$ Mail-> SMTPAuth = true; // Enable SMTP authentication
$ Mail-> Username = 'jswanc'; // SMTP username
$ Mail-> Password = 'secret'; // SMTP password
$ Mail-> SMTPSecure = 'tls '; // Enable encryption, 'SSL' also accepted

$ Mail-> From = 'from @ example.com ';
$ Mail-> FromName = 'mailer ';
$ Mail-> AddAddress ('Josh @ example.net ', 'Josh Adams'); // Add a recipient
$ Mail-> AddAddress ('Ellen @ example.com '); // Name is optional
$ Mail-> AddReplyTo ('info @ example.com ', 'information ');
$ Mail-> AddCC ('CC @ example.com ');
$ Mail-> AddBCC ('bcc @ example.com ');

$ Mail-> WordWrap = 50; // Set word wrap to 50 characters
$ Mail-> AddAttachment ('/var/tmp/file.tar.gz'); // Add attachments
$ Mail-> AddAttachment ('/tmp/image.jpg', 'new.jpg '); // Optional name
$ Mail-> IsHTML (true); // Set email format to HTML

$ Mail-> Subject = 'here is the subobject ';
$ Mail-> Body = 'This is the HTML message bodyIn bold!';
$ Mail-> AltBody = 'This is the body in plain text for non-HTML mail clients ';

If (! $ Mail-> Send ()){
Echo 'message cocould not be sent .';
Echo 'mailer Error: '. $ mail-> ErrorInfo;
Exit;
}

Echo 'Message has been sent ';

?>

The solution is simple. add a property in the code:

$ Mail-> CharSet = "UTF-8"; // sets the character set encoding.

If it is gb2312 encoding, it is:

$ Mail-> CharSet = "GB2312 ";

Save the file to the corresponding encoding format.

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.