下载pear
http://pear.php.net/~cweiske/1.9.5/go-pear.phar
拷贝到php目录下PEAR文件夹D:\wamp\bin\php\php5.5.12\PEAR\
在CMD命令行下输入命令php -d phar.require_hash=0 PEAR/go-pear.phar
然后一直按回车,到would you like to alter php.ini时选Y,然后继续回车结束。
然后安装Mail Mail_Mime Net/SMTP
在php5.5.12目录下执行CMD命令
pear install Mail
pear install Mail_Mime
pear install Net/SMTP
然后修改php.ini文件
; PHP's default setting for include_path is ".;/path/to/php/pear"
; http://php.net/include-path
include_path=".;D:\wamp\bin\php\php5.5.12\PEAR"
最后重启apache,在php程序里就可以使用pear mail发送邮件了。
下面是一个示例:
<?php
require_once("Mail.php");
require_once("Mail/mime.php");
require_once("Net/SMTP.php");
$smtpinfo=array();
$smtpinfo["host"]="smtp.163.com";
$smtpinfo["port"]="25";
$smtpinfo["username"]="username@163.com";
$smtpinfo["password"]="*****";
$smtpinfo["timeout"]=10;
$smtpinfo["auth"]=true;//登录验证
//$smtpinfo["debug"]=true;//调试模式
$mailAddr=array("destination@qq.com");
$from="Name<username@163.com>";
$to=implode(",",$mailAddr);
$subject="this is a test";
$content="test is success";
$contentType="text/html;charset=utf-8";
$crlf="\r\n";//换行符
$mime=new Mail_mime($crlf);
$mime->setHTMLBody($content);
$param["text_charset"]="utf-8";
$param["html_charset"]="utf-8";
$param["head-charset"]="utf-8";
$body=$mime->get($param);
$headers=array();
$headers["From"]=$from;
$headers["To"]=$to;
$headers["Subject"]=$subject;
$headers["Content-Type"]=$contentType;
$headers=$mime->headers($headers);
$smtp=& Mail::factory("smtp",$smtpinfo);
$mail=$smtp->send($mailAddr,$headers,$body);
$smtp->disconnect();
if (PEAR::isError($mail)) {
//发送失败
echo 'Email sending failed: ' . $mail->getMessage()."\n";
}
else{
//发送成功
echo "success!\n";
}
?>