微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php Socket发送邮件验证邮箱的真实有效性而非格式

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

<?PHP

/*请尊重别人的劳动成功,请保留此版权信息,谢谢!
作者:小露珠3.3
扬帆修正一点东西:在代码中已经用注释注明,本代码现在向qq发信没问题~
*/
set_time_limit(120);

class smtp_mail
{
    var $host;           //主机
    var $port;           //端口 一般为25
    var $user;           //SMTP认证的帐号
    var $pass;           //认证密码
    var $debug = false;   //是否显示和服务器会话信息?
    var $conn;
    var $result_str;       //结果
    var $in;           //客户机发送的命令
    var $from;           //源信箱
    var $to;           //目标信箱
    var $subject;         //主题
    var $body;           //内容
    function smtp_mail($host,$port,$user,$pass,$debug=false)
    {
       $this->host   = $host;
       $this->port   = $port;
       $this->user   = base64_encode($user);
       $this->pass   = base64_encode($pass);
       $this->debug   = $debug;
       $this->socket = socket_create (AF_INET,SOCK_STREAM,SOL_TCP);   //具体用法请参考手册
       if($this->socket)
       {
       $this->result_str   =   "创建SOCKET:".socket_strerror(socket_last_error());
       $this->debug_show($this->result_str);
       }
       else
       {
       exit("初始化失败,请检查您的网络连接和参数");
       }
       $this->conn = socket_connect($this->socket,$this->host,$this->port);
       if($this->conn)
       {
       $this->result_str   =   "创建SOCKET连接:".socket_strerror(socket_last_error());
       $this->debug_show($this->result_str);
       }
       else
       {
       exit("初始化失败,请检查您的网络连接和参数");
       }
       $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket,1024)."</font>";
       $this->debug_show($this->result_str);

    }
    function debug_show($str)
    {
       if($this->debug)
       {
       echo $str."<p>\r\n";
       }
    }
    function send($from,$to,$subject,$body)
    {
       if($from == "" || $to == "")
       {
       exit("请输入信箱地址");
       }
       if($subject == "") $sebject = "无标题";
       if($body     == "") $body     = "无内容";
       $this->from     =   $from;
       $this->to       =   $to;
       $this->subject   =   $subject;
       $this->body     =   $body;

           //扬帆修改部分代码
       $All           = "From:<".$this->from.">\r\n";
       $All           .= "To:<".$this->to.">\r\n";
       $All           .= "Subject:".$this->subject."\r\n\r\n";
       $All           .= $this->body;
       /*
       如过把$All的内容再加处理,就可以实现发送MIME邮件了
       不过还需要加很多程序
       */

       //以下是和服务器会话
       $this->in       =   "EHLO HELO\r\n";
       $this->docommand();

       $this->in       =   "AUTH LOGIN\r\n";
       $this->docommand();

       $this->in       =   $this->user."\r\n";
       $this->docommand();

       $this->in       =   $this->pass."\r\n";
       $this->docommand();

    // $this->in       =   "MAIL FROM:".$this->from."\r\n";
       $this->in       =   "MAIL FROM:<".$this->from.">\r\n";   //扬帆修改
       $this->docommand();

    // $this->in       =   "RCPT TO:".$this->to."\r\n";
       $this->in       =   "RCPT TO:<".$this->to.">\r\n";     //扬帆修改
       $this->docommand();

       $this->in       =   "DATA\r\n";
       $this->docommand();

         $this->in       =   $All."\r\n.\r\n";
       $this->docommand();

       $this->in       =   "QUIT\r\n";
       $this->docommand();

       //结束,关闭连接

    }
    function docommand()
    {
       socket_write ($this->socket,$this->in,strlen ($this->in));
       $this->debug_show("客户机命令:".$this->in);
       $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket,1024)."</font>";
       $this->debug_show($this->result_str);
    }
}
?>

<?PHP
//测试页面
include "smtp_mail.PHP";

//你用这个类的时候你修改成你自己的信箱就可以了
$smtp=new smtp_mail("smtp.qq.com","25","[email protected]","Your password",true);
//如果你需要显示会话信息,请将上面的修改成
//$smtp   =   new smtp_mail("smtp.qq.com","你的qq.com的帐号","你的密码",true);
$smtp->send("[email protected]","[email protected]","你好","测试邮件");
?> 

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐