[学习笔记]Pear::Mail发送中文邮件

这里使用的是smtp发送。本来想mail()函数的,但服务气iis的smtp服务器一直没设置好。在网上找到mail的pear包,学习一下。笔记如下:
除了要安装pear::mail包还安装了pear::Mail_Mime(以便对中文支持)

<?php
include('mail.php');'
 //Mail_Mime provides classes to create mime messages.
include('mail/mime.php');

$text = '这是文本内容';
$html = '这是网页格式的内容.测试文档';
$file = 'textfile.txt';
$crlf = "\n";
$hdrs = array(
'from' => 'xu',
'to' => 'test@gmail.com, test@163.com',
'subject' => '中文邮件 utf-8编码'
);

$mime = new Mail_mime($crlf); //生成Mail_Mime实例

$mime->setTxtBody($text);
//$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain', 'textfile.txt', true, 'quoted-printable'); //获得附件基于文本附件使用quoted-printable编码

//$mime->get的数组参数$get_param
$get_param["text_encoding"] = '7bit'; //设置文本编码方式
$get_param["html_encoding"] = 'quoted-printable';//设置Html编码方式
$get_param["7bit_wrap"] = '998'; //行最大字符数
$get_param["head_charset"] = 'UTF-8';//设置邮件头部字符集
$get_param["text_charset"] = 'UTF-8';
$get_param["html_charset"] = 'UTF-8';

$body = $mime->get($get_param); //返回邮件body信息
$hdrs = $mime->headers($hdrs); //返回array width the mime headers and the additional headers

$params['host'] = 'smtp.163.com';
$params['auth'] = true;
$params['username'] = 'test_smtp@163.com';
$params['password'] = 'secret';
$recipients = array('test@gmail.com', 'test@163.com');

$send = @$mail =& mail::factory('smtp', $params); //生成mail的smtp实例
$mail->send($recipients, $hdrs, $body);
if(pear::iserror($send)) {
echo($send->getmessage());
} else {
echo("send successfully");
}
echo($_server['php_self']);
?>