phpmailer使用gmail SMTP的方法
终于能够通过phpmailer使用gmail账号发送邮件了
phpmailer(现在的版本是1.73)是一个很好用的工具,可以很方便的使用php语言发送邮件,支持smtp及验证,我们一直都用它。
但是,由于gmail的smtp采用了ssl连接:
Outgoing Mail (SMTP) Server – requires TLS: smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587
使用phpmailer就无法正常连接gmail的发信服务器了,并且这个问题一直没有得到phpmailer的官方解决,不过在sf.net上面的讨论里倒是找到了一点资料,采用下面这种方法就可以连接gmail了。
修改class.smtp.php,第101行,把
$this->smtp_conn = fsockopen($host, # the host of the server
改成
$this->smtp_conn = fsockopen(’ssl://’ . $host, # the host of the server
这样就可以了,就是制定使用ssl协议连接主机,当然php的openssl模块必须打开:
extension=php_openssl.dll
但是这样就不能使用非ssl的主机了,我也不像在include目录下面放两份phpmailer,于是,又琢磨出了一种更好的方式:
打开class.phpmailer.php,在大概543行附近,函数SmtpConnect()中,找到:
$this->smtp->do_debug = $this->SMTPDebug;
$hosts = explode(”;”, $this->Host);
$index = 0;
$connection = ($this->smtp->Connected());
// Retry while there is no connection
while($index Port;
}
这一段的部分功能就是,如果你输入的主机名中带有端口号,自动的识别出来,设想虽好,但就是这部分让我们无法直接在调用的时候使用ssl格式的主机 名,因为“ssl://xxx”的形式会被误认为是主机:端口号的形式,另外端口号一般都比较固定,我们手工设置好了,也不必一定要和主机一起赋值,所以 在上面的代码后面添加:
//Modify by Fwolf @ 2006-4-14, to enable ssl mail connection
$host = $this->Host;
$port = $this->Port;
就可以了,使用正常smtp主机的时候,用法不变,使用gmail的时候,使用ssl://smtp.gmail.com作为主机名就可以了,唯一稍微麻烦一些的就是端口需要手工指定——其实也不麻烦。
按照上面的配置更改后,使用gmail账号发送邮件时还会有一条警告信息:
Warning: fgets(): SSL: fatal protocol error in H:\php_includes\phpmailer_ssl\cla
ss.smtp.php on line 1024
这各警告信息在php的帮助里面有,好像是在使用ssl连接的时候,读文件读到文件尾的时候出现的问题,需要手工屏蔽,或者人为控制读的长度,我们用最简 单的方式禁止警告信息显示就可以了,找到class.smtp.php的1024行,在fgets($this->smtp_conn,515)函 数前面加上个@就可以了。
下面是一个完整的使用phpmailer发送邮件的函数:
function send_mail($to_address, $to_name ,$subject, $body, $attach = ‘’)
{
//使用phpmailer发送邮件
require_once(”phpmailer/class.phpmailer.php”);
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->CharSet = ‘utf-8′;
$mail->Encoding = ‘base64′;
$mail->From = ‘fwolf.mailagent@gmail.com’;
$mail->FromName = ‘Fwolf’;
//$mail->Sender = ‘fwolf.mailagent@gmail.com’;
//$mail->ConfirmReadingTo = ‘fwolf.mailagent@gmail.com’; //回执?
$mail->Host = ’ssl://smtp.gmail.com’;
$mail->Port = 465; //default is 25, gmail is 465 or 587
$mail->SMTPAuth = true;
$mail->Username = “fwolf.mailagent@gmail.com”;
$mail->Password = “xxx”;
$mail->>ddAddress($to_address, $to_name);
//$mail->AddReplyTo(’fwolf.aide@gmail.com’, “Fwolf”); //针对gmail无用,gmail是In-Reply-To:,phpmailer默认生成的是Reply-to:
$mail->WordWrap = 50;
if (!empty($attach))
$mail->AddAttachment($attach);
$mail->IsHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = “This is the body in plain text for non-HTML mail clients”;
if(!$mail->Send())
{
echo “Mail send failed.\r\n”;
echo “Error message: ” . $mail->ErrorInfo . “\r\n”;
return false;
}
else
{
echo(”Send $attach to $to_name successed.\r\n”);
return true;
}
//echo “Message has been sent”;
//
}
phpmailer使用gmail SMTP的方法的更多相关文章
- PHPmailer群发Gmail的常见问题
博主小白一枚,phpmailer只会一些基本的用法,就这样一个邮件的群发功能也难住了我一周,下面把我遇到的问题给大家总结一下 1.Could not authenticate 首先,如果你没有使用循环 ...
- WordPress使用PHPMailer发送gmail邮件
wordpress使用phpmailer发送gmail邮件 0.保证用于gmail账号已经开启imap服务,且你能正常访问到gmail的smtp服务.(需要climb over the wall) 1 ...
- Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference
Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify ...
- Linux 上使用 Gmail SMTP 服务器发送邮件通知
导读 假定你想配置一个 Linux 应用,用于从你的服务器或桌面客户端发送邮件信息.邮件信息可能是邮件简报.状态更新(如 Cachet).监控警报(如 Monit).磁盘时间(如 RAID mdadm ...
- Spring通过Gmail SMTP服务器MailSender发送电子邮件
Spring提供了一个有用的“org.springframework.mail.javamail.JavaMailSenderImpl”类,通过JavaMail API 简化邮件发送过程.这里有一个项 ...
- linux下phpmailer发送邮件出现SMTP ERROR: Failed to connect to server: (0)错误
转自:https://www.cnblogs.com/raincowl/p/8875647.html //Create a new PHPMailer instance $mail = new PHP ...
- java mail 使用 gmail smtp 发送邮件
smtp 服务器:smtp.gmail.com 使用ssl的端口:465 用户名:username@gmail.com 密码:password** 基本配置没有问题,关键在于Google对安全性要求非 ...
- 用phpmailer发送邮件提示SMTP Error: Could not connect to SMTP host解决办法
之前做项目的时候做了一个用phpmailer发送邮件的功能<CI框架结合PHPmailer发送邮件>,昨天步署上线(刚开始用新浪云,嫌贵,换成阿里了),测试的时候,发送邮件却意外报错了.. ...
- Linux phpmailer发送邮件失败的解决方法
(本地windows phpmailer发送ok 放到linux发送失败) 原因:linux 通过465端口进行更安全的SMTPS协议发送邮件 windows 是基于smtp 25端口的 因此 可 ...
随机推荐
- table 表头固定
<html> <head> <title>Test</title> <style type="text/css"> .d ...
- [wikioi]合并果子
http://wikioi.com/problem/1063/ 这题是贪心+堆.主要想练习一下堆的写法.算法导论里的方法名是heapify()等,但大家经常用更直观的down(), up()方法(向上 ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-004-使用AspectJ’s pointcut expression language定义Pointcut
一. 1.在Spring中,pointcut是通过AspectJ’s pointcut expression language来定义的,但spring只支持它的一部分,如果超出范围就会报Illegal ...
- [Hadoop源码解读](四)MapReduce篇之Counter相关类
当我们定义一个Counter时,我们首先要定义一枚举类型: public static enum MY_COUNTER{ CORRUPTED_DATA_COUNTER, NORMAL_DATA_COU ...
- SCOI2005互不侵犯King
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1499 Solved: 872[Submit][S ...
- 【转】ubuntu11.10 64bit 环境android编译错误
原文网址:http://blog.csdn.net/lmhgen/article/details/7326083 错误1: 注意:external/protobuf/java/src/main/jav ...
- 使用Chrome测试页面响应性
如今我们都知道 响应式 的意思.作为开发者,我们常常开发一些响应式的应用.有很多工具可以帮助我们完成这个目的.某些人甚至使用付费扩展.然而,我用了一个东西,它就是 Google Chrome 浏览器. ...
- net 内存泄露和内存溢出
一直以来都对内存泄露和内存溢出理解的不是很深刻.在网上看到了几篇文章,于是整理了一下自己对内存泄露和内存溢出的理解. 一.概念 内存溢出:指程序在运行的过程中,程序对内存的需求超过了超过了计算机分配给 ...
- CodeForces 370A Rook, Bishop and King
此题看似很简单,但实际上有不少细节,WA点不少.分情况处理即可. #include<cmath> #include<cstdio> #include<string> ...
- 未能找到类型或命名空间“Compare”
在用vs2012 .net Framework4.5 Mvc3做一个MVCMusicStore 的例子时,遇到这样一个问题 解决办法: 具体原因也不是很清楚,据说是引用Compare做验证时会有二 ...