通过phpmailer使用gmail账号发送邮件了【转】
2011-10-11 08:38
956 查看
163邮箱 |
POP3服务器:POP.163.COM SMTP服务器:SMTP.163.COM |
yahoo邮箱 |
注意:yahoo在foxmail 4.1以上的版本设置如下:( outlook 不行哟~) |
POP3服务器:pop.mail.yahoo.com.cn SMTP服务器:smtp.mail.yahoo.com.cn |
sohu邮箱 |
POP3服务器:pop3.sohu.com SMTP服务器:smtp.sohu.com |
Gmail邮箱 |
第一步:要手动配置: POP3服务器是pop.gmail.com SMTP服务器是 ssl://smtp.gmail.com 第二步:使用自动配置文件 http://toolbar.google.com/gmail-helper/GmailConfig.exe 这样就OK喽~ 可以发送和接收 Gmail 邮件了。 |
QQ邮箱 |
POP3服务器:pop.qq.com SMTP服务器:smtp.qq.com SMTP服务器需要身份验证。 |
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 < count($hosts) && $connection == false)
{
if(strstr($hosts[$index], ":"))
list($host, $port) = explode(":", $hosts[$index]);
else
{
$host = $hosts[$index];
$port = $this->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.mailagent@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 <$to_address> successed.\r\n”);
return true;
}
//echo “Message has been sent”;
//
}
update @ 2006-11-3
感谢RainChen提供的fgets出错提示的更好解决办法(位置大概在class.smtp.php的1024行):将:
fgets($this->smtp_conn,515)
改为:
!feof($this->smtp_conn) && $str = fgets($this->smtp_conn,515)
这样的代码就规范多了,极力不提倡使用@来屏蔽错误信息。
http://www.fwolf.com/blog/post/176 ---- phpmailer支持中文名称的附件
http://hi.baidu.com/keepsky/blog/item/3ff9eb13144c470f5baf5318.html
http://www.jiucool.com/phpmailer-php-email/
相关文章推荐
- 使用C#通过GMAIL发送邮件.
- 使用C#通过GMAIL发送邮件.
- PHPMailer通过Gmail和Office365的SMTP账号发送邮件设置
- phplist(及phpmailer)通过gmail发送邮件的配置方法
- python - 如何通过GMail发送邮件:smtplib的使用
- python使用smtplib模块通过gmail实现邮件发送的方法
- 使用CakePHP自带Email组件通过gmail发送邮件
- phplist(及phpmailer)通过gmail发送邮件的配置方法
- phplist(及phpmailer)通过gmail发送邮件的配置方法
- python使用smtplib模块通过gmail实现邮件发送的方法
- phplist及phpmailer(组合使用)通过gmail发送邮件的配置方法
- phplist及phpmailer(组合使用)通过gmail发送邮件的配置方法
- 使用indy 9发送邮件支持SSL选项,gmail测试通过
- python - 如何通过GMail发送邮件:smtplib的使用
- PHP_thinkphp框架使用PHPMailer实现发送邮件的功能,以及各种问题解析
- Thinkphp中使用PHPmailer发送邮件
- 使用PHPMailer发送邮件
- 如何在 Laravel 中使用 SMTP 发送邮件(适用于 163、QQ、Gmail 等)
- zabbix使用外部邮箱账号发送报警邮件设置
- 在.net中使用CDO发送邮件(已调试通过)