本地没有发邮件的服务器,借助现成的smtp服务器发送邮件是个不错的选择,这里使用到的工具是phpmailer ( version 5.2.0),smtp服务器就选gmail和163。
1. 使用gmail发送的脚本
include("class.phpmailer.php");
include("class.smtp.php");
//获取一个外部文件的内容
$mail = new phpmailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[]",'',$body);
//设置smtp参数
$mail->issmtp();
$mail->smtpauth = true;
$mail->smtpkeepalive = true;
$mail->smtpsecure = "ssl";
$mail->host = "smtp.gmail.com";
$mail->port = 465;
//填写你的gmail账号和密码
$mail->username = "yourname@gmail.com";
$mail->password = "password";
//设置发送方,最好不要伪造地址
$mail->from = "yourname@gmail.com";
$mail->fromname = "webmaster";
$mail->subject = "this is the subject";
$mail->altbody = $body;
$mail->wordwrap = 50; // set word wrap
$mail->msghtml($body);
//设置回复地址
$mail->addreplyto("yourname@gmail.com","webmaster");
//添加附件,此处附件与脚本位于相同目录下
//否则填写完整路径
$mail->addattachment("attachment.jpg");
$mail->addattachment("attachment.zip");
//设置邮件接收方的邮箱和姓名
$mail->addaddress("toname@gmail.com","firstname lastname");
//使用html格式发送邮件
$mail->ishtml(true);
//通过send方法发送邮件
//根据发送结果做相应处理
if(!$mail->send()) {
echo "mailer error: " . $mail->errorinfo;
} else {
echo "message has been sent";
}
2.使用163发送邮件的脚本
只需要更改smtp配置和账户密码即可,smtp配置如下
//设置smtp参数 //注意这里不需要ssl协议 $mail->issmtp(); $mail->smtpauth = true; $mail->smtpkeepalive = true; $mail->host = "smtp.163.com"; $mail->port = 25;
在本地wampserver环境下测试通过,需要开启php_openssl 扩展。
以上所述就是本文的全部内容了,希望大家能够喜欢。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!