A-A+
解决SMTP server response: 536 authentication failed
php的mail函数,在Win下使用较难!用类解决。
服务器上有Smtp环境,但测试发邮件时:
- <?php
- $to = 'aa@21nw.com';
- $subject = 'the subject';
- $message = 'hello';
- $headers = 'From: ad@21pt.com' . "rn" .
- 'Reply-To: ad@21nw.com' . "rn" .
- 'X-Mailer: PHP/' . phpversion();
- mail($to, $subject, $message, $headers);
- ?>
一直提示:
Warning: mail() [function.mail]: SMTP server response: 536 authentication failed.
这里感谢一下OP的「mefee」给了我提示,对应办法,时间紧张,直接测试了个类,测试成功与大家分享:
zhoz:1:07 2008-9-26 此类已更新,zhoz更新版:php在win下Socket方式发邮件类,地址:http://log.zhoz.com/read.php?425,请使用新版本。
- <?php
- /*
- * ZhozPhpSmtpSendMail.php
- * Created on 2008/09/02 zhoz.com
- * php smtp发送邮件类
- * 代替mail():因为它报错,我又不想
- */
- class ZhozPhpSmtpSendMail {
- //您的SMTP 服务器供应商,可以是域名或IP地址
- var $smtp = "";
- //SMTP需要要身份验证设值为 1, 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
- var $check = 1;
- //您的email帐号名称
- var $username = "";
- //您的email密码
- var $password = "";
- //此email 必需是发信服务器上的email
- var $s_from = "";
- /*
- * 功能:发信初始化设置
- * $from 你的发信服务器上的邮箱
- * $password 你的邮箱密码
- * $smtp 您的SMTP 服务器供应商,可以是域名或IP地址
- * $check SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证
- */
- function ZhozPhpSmtpSendMail ( $from, $password, $smtp, $check = 1 ) {
- if( preg_match("/^[^d-_][w-]*[^-_]@[^-][a-zA-Zd-]+[^-](.[^-][a-zA-Zd-]*[^-])*.[a-zA-Z]{2,3}/", $from ) ) {
- $this->username = substr( $from, 0, strpos( $from , "@" ) );
- $this->password = $password;
- $this->smtp = $smtp ? $smtp : $this->smtp;
- $this->check = $check;
- $this->s_from = $from;
- }
- }
- /*
- * 功能:发送邮件
- * $to 目标邮箱
- * $from 来源邮箱
- * $subject 邮件标题
- * $message 邮件内容
- */
- function send ( $to, $from, $subject, $message ) {
- //连接服务器
- $fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60);
- if (!$fp ) return "联接服务器失败".__LINE__;
- set_socket_blocking($fp, true );
- $lastmessage=fgets($fp,512);
- if ( substr($lastmessage,0,3) != 220 ) return "错误信息1:$lastmessage".__LINE__;
- //HELO
- $yourname = "YOURNAME";
- if($this->check == "1") $lastact="EHLO ".$yourname."rn";
- else $lastact="HELO ".$yourname."rn";
- fputs($fp, $lastact);
- $lastmessage == fgets($fp,512);
- if (substr($lastmessage,0,3) != 220 ) return "错误信息2:$lastmessage".__LINE__;
- while (true) {
- $lastmessage = fgets($fp,512);
- if ( (substr($lastmessage,3,1) != "-") or (emptyempty($lastmessage)) )
- break;
- }
- //身份验证
- if ($this->check=="1") {
- //验证开始
- $lastact="AUTH LOGIN"."rn";
- fputs( $fp, $lastact);
- $lastmessage = fgets ($fp,512);
- if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__;
- //用户姓名
- $lastact=base64_encode($this->username)."rn";
- fputs( $fp, $lastact);
- $lastmessage = fgets ($fp,512);
- if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__;
- //用户密码
- $lastact=base64_encode($this->password)."rn";
- fputs( $fp, $lastact);
- $lastmessage = fgets ($fp,512);
- if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__;
- }
- //FROM:
- $lastact="MAIL FROM: <". $this->s_from . ">rn";
- fputs( $fp, $lastact);
- $lastmessage = fgets ($fp,512);
- if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__;
- //TO:
- $lastact="RCPT TO: <". $to ."> rn";
- fputs( $fp, $lastact);
- $lastmessage = fgets ($fp,512);
- if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__;
- //DATA
- $lastact="DATArn";
- fputs($fp, $lastact);
- $lastmessage = fgets ($fp,512);
- if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__;
- //处理Subject头
- $head="Subject: $subjectrn";
- $message = $head."rn".$message;
- //处理From头
- $head="From: $fromrn";
- $message = $head.$message;
- //处理To头
- $head="To: $torn";
- $message = $head.$message;
- //加上结束串
- $message .= "rn.rn";
- //发送信息
- fputs($fp, $message);
- $lastact="QUITrn";
- fclose($fp);
- return 0;
- }
- }
- ?>
测试方法很简单了: zhoz_test_mail.php
- <?php
- set_time_limit(9999); < /li>
- include "ZhozPhpSmtpSendMail.php";
- $sm = new ZhozPhpSmtpSendMail( "ad@21nw.com", "zhoz", "smtp.21nw.com" );
- $end = $sm->send("ad@21nw.com", "ad@21pt.com", "subject is ad", "body is 21nw.com");
- if( $end ) {
- echo $end;
- echo "no---------";
- } else {
- echo "send ok-----------------!";
- die;
- }
- // 结果当然是:send ok-----------------!
- ?>
作者:zhoz@Everyday NetLog
地址:http://log.zhoz.com/read.php?407
Tags: 主机配置 , qmail , 软件测试 , 网络协议