php mailer类调用远程SMTP服务器发送邮件实现方法
作者:纳木错 时间:2023-08-16 16:09:18
本文实例讲述了php mailer类调用远程SMTP服务器发送邮件实现方法。分享给大家供大家参考,具体如下:
php mailer 是一款很好用的php电子邮件发送类模块,可以调用本地的smtp发送电子邮件,也可以调用远程的smtp发送电子邮件,但是使用时需要注意一些事项,否则就会造成发送失败,或者根本不能调用的情况,本文就我在使用这个类时,遇到的问题和解决办法进行展开,简要说明一下php mailer的用法,及注意事项。
首先下载phpmailer类库文件,在这里下载,只需一个资源分。
下载之后,将这个文件,即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,mailer,发送邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
linux下利用Docker安装mysql的步骤
2024-01-25 01:17:56
详解微信小程序网络请求接口封装实例
2024-06-12 04:08:54
电商网站的购买按钮
2011-07-04 12:18:59
Python爬取肯德基官网ajax的post请求实现过程
2021-03-31 13:54:00
![](https://img.aspxhome.com/file/2023/5/104815_0s.jpg)
Mysql自带profiling性能分析工具使用分享
2024-01-14 17:48:24
![](https://img.aspxhome.com/file/2023/0/71040_0s.jpg)
Jupyter安装拓展nbextensions及解决官网下载慢的问题
2023-11-24 20:50:54
![](https://img.aspxhome.com/file/2023/8/133898_0s.jpg)
深入理解JavaScript作用域和作用域链
2024-04-10 13:54:17
![](https://img.aspxhome.com/file/2023/7/136947_0s.jpg)
text-indent 隐藏文字时出现的 outline问题
2007-12-02 17:31:00
django框架模板中定义变量(set variable in django template)的方法分析
2021-11-18 03:28:04
SQL Server性能调优之缓存
2024-01-25 04:30:08
对跨多个表格的数据组合时需要用到的SQL
2009-01-06 11:18:00
asp解决fso.ReadAll提示输入超出了文件尾的错误
2008-01-30 21:40:00
浅析Python是如何实现集合的
2022-05-16 03:38:58
在Centos 5.5 上编译安装mysql 5.5.9
2024-01-19 02:56:39
Vue3新属性之css中使用v-bind的方法(v-bind in css)
2024-05-28 16:01:07
![](https://img.aspxhome.com/file/2023/1/123081_0s.jpg)
Python实现学生成绩管理系统
2023-08-13 09:51:17
![](https://img.aspxhome.com/file/2023/1/62301_0s.jpg)
详解python做UI界面的方法
2023-05-24 08:07:41
![](https://img.aspxhome.com/file/2023/0/105590_0s.png)
PHP之mysql位运算案例讲解
2023-06-13 06:16:19
![](https://img.aspxhome.com/file/2023/2/55552_0s.png)
python如何利用Mitmproxy抓包
2022-06-15 13:18:16
Mysql视图和触发器使用过程
2024-01-15 22:15:46
![](https://img.aspxhome.com/file/2023/9/103129_0s.png)