下载phpmailer ,在程序里包含class.phpmailer.php 类  ,这里有中文乱码的解决方法

实例代码如下

<html>
<head>
<title>PHPMailer - Mail() basic test</title>
</head>
<body> <?php
//header( "Content-type: text/html; charset=UTF-8" );    //设置本地编码
//setlocale( LC_ALL, 'GBK' );
//error_reporting(E_ALL);
error_reporting( E_STRICT ); date_default_timezone_set( 'America/Toronto' ); require_once('class.phpmailer.php');    //必须包含的文件
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded $mail = new PHPMailer();
$mail->CharSet = "UTF-8";        //中文环境下需要设置编码
// $body = file_get_contents( 'contents.html' );    //包含网页的使用方法
//$body = eregi_replace( "[\]", '', $body );
$body = '测试邮件,附上中文乱码解决方法:' . "<br>" .'123'; //设置邮件内容 使用 <br> 换行
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.163.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.163.com"; // 邮箱服务器地址
$mail->Port = 25; // 邮箱服务器端口
$mail->Username = "[email protected]"; // 你的邮箱用户名
$mail->Password = "abcdefg@0"; // 你的邮箱密码 $mail->SetFrom( '[email protected]', '小----洋 ' );  //发送人 //$mail->AddReplyTo( "[email protected]", "亲" );    //接收方 $mail->Subject = "=?utf-8?B?" . base64_encode( "我是标题" ) . "?=";      //解决中文标题乱码问题 设置标题 $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML( $body );
$address = "[email protected]";
$mail->AddAddress( $address, "John Doe" ); //$mail->AddAttachment( "附件1" ); // attachment      //这里可以添加附件
// $mail->AddAttachment( "附件2" ); // attachment       if ( !$mail->Send() ) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?> </body>
</html>
04-23 04:28