本文实例讲述了php mailer类调用远程smtp服务器发送邮件实现方法。分享给大家供大家参考,具体如下:
php mailer 是一款很好用的php电子邮件发送类模块,可以调用本地的smtp发送电子邮件,也可以调用远程的smtp发送电子邮件,但是使用时需要注意一些事项,否则就会造成发送失败,或者根本不能调用的情况,本文就我在使用这个类时,遇到的问题和解决办法进行展开,简要说明一下php mailer的用法,及注意事项。
首先下载phpmailer类库文件,在这里下载,只需一个资源分。 下载地址: http://www.jb51.net/codes/27188.html
下载之后,将这个文件,即class.phpmailer.php 放到你的工程的某个目录下,在需要发送邮件的地方这样写:
<?php
require 'class.phpmailer.php';
try {
$mail = new phpmailer(true);
$body = file_get_contents('contents.html'); //邮件的内容写到contents.html页面里了
$body = preg_replace('//////','', $body); //strip backslashes
$mail->issmtp(); // tell the class to use smtp
$mail->smtpauth = true; // enable smtp authentication
$mail->port = 25; // set the smtp server port
$mail->host = "mail.yourdomain.com"; // 远程smtp服务器
$mail->username = "yourname@yourdomain.com"; // 远程smtp 服务器上的用户名
$mail->password = "yourpassword"; // 你的远程smtp 服务器上用户对应的密码
//$mail->issendmail(); //告诉这个类使用sendmail组件,使用的时候如果没有sendmail组建就要把这个注释掉,否则会有
$mail->addreplyto("yourname@yourdomain.com","first last");
$mail->from = "fromname@yourdomain.com";
$mail->fromname = "first last";
$to = "toname@domain.com";
$mail->addaddress($to);
$mail->subject = "first phpmailer message";
$mail->altbody = "to view the message, please use an html compatible email viewer!"; // optional, comment out and test
$mail->wordwrap = 80; // set word wrap
$mail->msghtml($body);
$mail->ishtml(true); // send as html
$mail->send();
echo 'message has been sent.';
} catch (phpmailerexception $e) {
echo $e->errormessage();
}
?>
注意:上面那个$mail->issendmail(); 需要注释掉,否则如果没有sendmail组件的话,会提示 “could not execute: /var/qmail/bin/sendmail ”的错误!
更多关于php相关内容感兴趣的读者可查看本站专题:《php针对xml文件操作技巧总结》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!