欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php win下Socket方式发邮件类

程序员文章站 2023-11-30 20:06:34
复制代码 代码如下:

复制代码 代码如下:

<?php
/*
* php smtp发送邮件scoket类
* zhozphpsmtpsendmail.php
* created on 2008/09/02 zhoz.com
* @author zhoz
* v 1.72 update 2008/09/26 zhoz.com
*/
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-z\d\-]+[^\-](\.[^\-][a-za-z\d\-]*[^\-])*\.[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 zhoz_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."\r\n";
else $lastact="helo ".$yourname."\r\n";
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"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__line__;
//用户姓名
$lastact=base64_encode($this->username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__line__;
//用户密码
$lastact=base64_encode($this->password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__line__;
}
//from:
$lastact="mail from: <". $this->s_from . ">\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__line__;
//to:
$lastact="rcpt to: <". $to ."> \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__line__;
//data
$lastact="data\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__line__;
//处理subject头
$head="subject: $subject\r\n";
$message = $head."\r\n".$message;
//处理from头
$head="from: $from\r\n";
$message = $head.$message;
//处理to头
$head="to: $to\r\n";
$message = $head.$message;
//加上结束串
$message .= "\r\n.\r\n";
//发送信息
fputs($fp, $message);
$lastact="quit\r\n";
fclose($fp);
error_log("send over------->ok!!", 0);
return true;
}
}
?>
<?php
/*
* php smtp发送邮件scoket类
* zhozphpsmtpsendmail.php
* created on 2008/09/02 zhoz.com
* @author zhoz
* v 1.72 update 2008/09/26 zhoz.com
*/
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-z\d\-]+[^\-](\.[^\-][a-za-z\d\-]*[^\-])*\.[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 zhoz_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."\r\n";
else $lastact="helo ".$yourname."\r\n";
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 (empty($lastmessage)) )
break;
}
//身份验证
if ($this->check=="1") {
//验证开始
$lastact="auth login"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__line__;
//用户姓名
$lastact=base64_encode($this->username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__line__;
//用户密码
$lastact=base64_encode($this->password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__line__;
}
//from:
$lastact="mail from: <". $this->s_from . ">\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__line__;
//to:
$lastact="rcpt to: <". $to ."> \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__line__;
//data
$lastact="data\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__line__;
//处理subject头
$head="subject: $subject\r\n";
$message = $head."\r\n".$message;
//处理from头
$head="from: $from\r\n";
$message = $head.$message;
//处理to头
$head="to: $to\r\n";
$message = $head.$message;
//加上结束串
$message .= "\r\n.\r\n";
//发送信息
fputs($fp, $message);
$lastact="quit\r\n";
fclose($fp);
error_log("send over------->ok!!", 0);
return true;
}
}
?>
测试方法一样。
运行代码查看代码复制代码打印关于
<?php
/**
* zhozphpsmtpsendmail.php
* created on 2008/09/02 zhoz.com
* php smtp发送邮件类
* 代替mail():因为它报错,我又不想
*/
// 这里配置发信服务器信息,如果不改也可以正常使用:
$from = 'zhoz@zhoz.com'; // 你的发信服务器上的邮箱
$password = 'zhoz'; // 你的邮箱密码
$smtp = 'smtp.zhoz.com'; // 您的smtp 服务器供应商,可以是域名或ip地址
// 类初始化
$zhoz_obj = new zhozphpsmtpsendmail($from, $password, $smtp);
// 发送开始
$status = $zhoz_obj->zhoz_send("zhoz008@126.com", "zhoz@zhoz.com", "subject is zhoz", "body is zhoz.com");
if($status) {
echo $status;
echo "no---------";
} else {
echo "send ok-----------------!";
die;
}
// 结果当然是:send ok-----------------!
?>