前一段时间研究了好一阵子的PHP发邮件程序,PHP自带的mail()函数在许多空间都不起作用,所以我选择了使用邮件提供商的smtp发邮件。网络上这样的文章真不少,可是很多都是不能使用的,不是修改php.ini就是要安装qmail之类的大型软件,这对于我们这些寄人篱下的站长无疑是想入非非,也给我造成了很多误导,曾经发现了一个发送成功的代码,但只能在在新网的空间上实现,而且极其不稳定,不是我想要的,那几天郁闷的我一直在看英文的[RFC 2195]规范……看得我快吐了。但是那个程序再怎么说也通过了,而且有时候还能支持Gmail的服务器,贴出来大家看一下:
c_mail.php
-----------------------------
<?php
/* 邮件发送类 */
require_once dirname(__FILE__)."/c_smtp_client.php";
static $c_smtp_client;
if(!isset($c_smtp_client))
{
$c_smtp_client = & new c_smtp_client($smtp_server['name']);
$c_smtp_client->do_log = false;
$c_smtp_client->need_auth = $smtp_server['need_auth'];
$c_smtp_client->username = $smtp_server['username'];
$c_smtp_client->password = $smtp_server['password'];
}
class c_mail
{
// html格式的Mail信笺
function html_mailer($from_name,$from_mail,$to_name,$to_mail,$subject,$message,$reply_mail)
{
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=gb2312\r\n";
$headers .= "From: ".$from_name."<".$from_mail.">\r\n";
$headers .= "To: ".$to_name."<".$to_mail.">\r\n";
$headers .= "Reply-To: ".$from_name."<".$reply_mail.">\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
$headers .= "X-Mailer: WebCMS MAIL SERV";
// 开始发送邮件
if($smtp_server['name']=='')
{
@mail($to_mail, $subject, $message, $headers);
}
else
{
$c_smtp_client->email($from_mail,$to_mail,$to_name,$headers,$subject,$message);
$c_smtp_client->send();
}
}
}
?>
---------------------------------
c_smtp_client.php
----------------------------------
<?php
/* smtp client class */
class c_smtp_client
{
var $connection;
var $server;
var $elog_fp;
var $log_file='./smtp_client.log';
var $do_log=true;
var $need_auth=true;
var $username;
var $password;
// 构造器
function c_smtp_client($server='')
{
if (!$server)
{
$this->server="localhost";
}
else
{
$this->server=$server;
}
$this->connection = fsockopen($this->server, 25);
if ($this->connection <= 0) return 0;
fputs($this->connection,"HELO xyz\r\n");
}
function email($from_mail, $to_mail, $to_name, $header, $subject, $body)
{
if ($this->connection <= 0) return 0;
// 邮件用户认证
if ($this->need_auth)
{
$this->elog("AUTH LOGIN", 1);
fputs($this->connection,"AUTH LOGIN\r\n");
$this->elog(fgets($this->connection, 1024));
$base64_username=base64_encode($this->username);
$this->elog("$base64_username", 1);
fputs($this->connection,"$base64_username\r\n");
$this->elog(fgets($this->connection, 1024));
$base64_password=base64_encode($this->password);
$this->elog("$base64_password", 1);
fputs($this->connection,"$base64_password\r\n");
$this->elog(fgets($this->connection, 1024));
}
$this->elog("MAIL FROM:$from_mail", 1);
fputs($this->connection,"MAIL FROM:$from_mail\r\n");
$this->elog(fgets($this->connection, 1024));
$this->elog("RCPT TO:$to_mail", 1);
fputs($this->connection, "RCPT TO:$to_mail\r\n");
$this->elog(fgets($this->connection, 1024));
$this->elog("DATA", 1);
fputs($this->connection, "DATA\r\n");
$this->elog(fgets($this->connection, 1024));
$this->elog("Subject: $subject", 1);
$this->elog("To: $to_name", 1);
fputs($this->connection,"Subject: $subject\r\n");
fputs($this->connection,"To: $to_name\r\n");
if ($header)
{
$this->elog($header, 1);
fputs($this->connection, "$header\r\n");
}
$this->elog("", 1);
$this->elog($body, 1);
$this->elog(".", 1);
fputs($this->connection,"\r\n");
fputs($this->connection,"$body \r\n");
fputs($this->connection,".\r\n");
$this->elog(fgets($this->connection, 1024));
return 1;
}
function send()
{
if ($this->connection)
{
fputs($this->connection, "QUIT\r\n");
fclose($this->connection);
$this->connection=0;
}
}
function close()
{
$this->send();
}
function elog($text, $mode=0)
{
if (!$this->do_log) return;
// open file
if (!$this->elog_fp)
{
if (!($this->elog_fp=fopen($this->log_file, 'a'))) return;
fwrite($this->elog_fp, "\n-------------------------------------------\n");
fwrite($this->elog_fp, " Sent " . date("Y-m-d H:i:s") . "\n");
fwrite($this->elog_fp, "-------------------------------------------\n");
}
// write to log
if (!$mode)
{
fwrite($this->elog_fp, " $text\n");
}
else
{
fwrite($this->elog_fp, "$text\n");
}
}
}
?>
------------------------------------
config.inc.php
---------------------------------
<?php
//smtp_server['name']=='';则表示直接使用mail();
//smtp_server['name']=='localhost';表示程序所在主机的smtp服务器
$smtp_server['name'] = "smtp.gmail.com";//这个是你使用的外部服务器,比如你也可以写smtp.163.com
$smtp_server['need_auth'] = true;
$smtp_server['username'] = 'silbo.su';//登陆服务器的用户名
$smtp_server['password'] = '********';//登陆密码
?>
----------------------------------
sendmail.php 运行文件
---------------------------------
<?php
//调用方法
require_once dirname(__FILE__)."/config.inc.php";
require_once dirname(__FILE__)."/c_mail.php";
$c_mail = new c_mail();
$c_mail->html_mailer("发信人姓名","发信人地址","收件人姓名","收件人地址","信件标题","信件内容","回复地址");
?>
--------------------------------------
另外不知道新网的php.ini的配置是如何的,大家可以通过这个地址查看一下新网空间的<phpinfo();>
http://www.f1boat.cn/test.php上面的程序给大家一个参考研究的例子,我想下面的程序可能是大家最想得到的,这也是我这几天来发现的最稳定的PHP利用Smtp发邮件的最稳定的方法。
我测试使用的是126的服务器,鉴于126与163的亲缘关系,163的服务器肯定也可以使用,本地空间已测试没有问题,连接速度很快,也很稳定,但是不支持Gmail服务器,显示错误“530 5.7.0 Must issue a STARTTLS command first 10sm4727451nzo 54”,可能是对话方式不搭配,但国内服务器能稳定发邮件对我来说就足够了,以下就是代码:
<?php
### 本程式由 "中国频道" 提供,请大家视情况修改
///以下将表单的内容写入message中
while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
$message .= "${key}: ${val}
";
}
if ($message == "") $message = "你好,这是测试邮件!";
if ($to == "") $to = "[email]silbo@126.com[/email]"; //如果表单中没有收件人,请设置默认收件人e-mail
if ($from == "") $from = "[email]silbo@126.com[/email]"; //如果表单中没有寄件人,请设置默认寄件人e-mail
if ($subject == "") $subject = "Hello~~~"; //如果表单中没有指定主题,请设置默认主题
if ($end = send22($to,$from,$subject, $message ) ) echo $end;
else echo "发送成功!";
function send22($to,$from,$subject, $message )
{
//使用本函数之前,务必请定义好以下变量#############################################
$smtp = "smtp.126.com"; //您的SMTP 服务器供应商,可以是域名或IP地址
$check = 1; //SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
if ($check) {
$username = "silbo"; //您的email帐号名称
$password = "******"; //您的email密码
}
$s_from = "[email]silbo@126.com[/email]"; //此email 必需是发信服务器上的email
###############################################
//连接服务器
$fp = fsockopen ( $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 "错误信息:".$lastmessage.__LINE__;
//HELO
$yourname = "YOURNAME";
if($check == "1") $lastact="EHLO ".$yourname."
";
else $lastact="HELO ".$yourname."
";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage,0,3) != 220 ) return "错误信息$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-") or (empty($lastmessage)) ) break;
}
//身份验证
if ($check=="1") {
//验证开始
$lastact="AUTH LOGIN"."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($username)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($password)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: $s_from" . "
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: $to" . "
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息$lastmessage".__LINE__;
//DATA
$lastact="DATA
";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512); if (substr($lastmessage,0,3) != 354) return "错误信息$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject
";
$message = $head."
".$message;
//处理From头
$head="From: $from
";
$message = $head.$message;
//处理To头
$head="To: $to
";
$message = $head.$message;
//加上结束串
$message .= "
.
";
//发送信息
fputs($fp, $message);
$lastact="QUIT
";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
?>
此代码可以修改后直接调用,如有什么疑问可以回帖,讨论一下,也欢迎到我的BLog讨论:
http://www.silbo.net 转帖请注明Silbo.net
