socket 发送邮件

  1. //原代码如下:
  2. set_time_limit(120);
  3. class smtp_mail
  4. {
  5. var $host; //主机
  6. var $port; //端口 一般为25
  7. var $user; //smtp认证的帐号
  8. var $pass; //认证密码
  9. var $debug = false; //是否显示和服务器会话信息?
  10. var $conn;
  11. var $result_str; //结果
  12. var $in; //客户机发送的命令
  13. var $from; //源信箱
  14. var $to; //目标信箱
  15. var $subject; //主题
  16. var $body; //内容
  17. function smtp_mail($host,$port,$user,$pass,$debug=false)
  18. {
  19. $this->host = $host;
  20. $this->port = $port;
  21. $this->user = base64_encode($user);
  22. $this->pass = base64_encode($pass);
  23. $this->debug = $debug;
  24. $this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); //具体用法请参考手册
  25. if($this->socket)
  26. {
  27. $this->result_str = "创建socket:".socket_strerror(socket_last_error());
  28. $this->debug_show($this->result_str);
  29. }
  30. else
  31. {
  32. exit("初始化失败,请检查您的网络连接和参数");
  33. }
  34. $this->conn = socket_connect($this->socket,$this->host,$this->port);
  35. if($this->conn)
  36. {
  37. $this->result_str = "创建socket连接:".socket_strerror(socket_last_error());
  38. $this->debug_show($this->result_str);
  39. }
  40. else
  41. {
  42. exit("初始化失败,请检查您的网络连接和参数");
  43. }
  44. $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
  45. $this->debug_show($this->result_str);
  46. }
  47. function debug_show($str)
  48. {
  49. if($this->debug)
  50. {
  51. echo $str."<p>rn";
  52. }
  53. }
  54. function send($from,$to,$subject,$body)
  55. {
  56. if($from == "" || $to == "")
  57. {
  58. exit("请输入信箱地址");
  59. }
  60. if($subject == "") $sebject = "无标题";
  61. if($body == "") $body = "无内容";
  62. $this->from = $from;
  63. $this->to = $to;
  64. $this->subject = $subject;
  65. $this->body = $body;
  66. $all = "from:".$this->from."n";
  67. $all .= "to:".$this->to."n";
  68. $all .= "subject:".$this->subject."n";
  69. $all .= $this->body;
  70. /*
  71. 如过把$all的内容再加处理,就可以实现发送mime邮件了
  72. 不过还需要加很多程序
  73. */
  74. //以下是和服务器会话
  75. $this->in = "ehlo helorn";
  76. $this->docommand();
  77. $this->in = "auth loginrn";
  78. $this->docommand();
  79. $this->in = $this->user."rn";
  80. $this->docommand();
  81. $this->in = $this->pass."rn";
  82. $this->docommand();
  83. $this->in = "mail from:".$this->from."rn";
  84. $this->docommand();
  85. $this->in = "rcpt to:".$this->to."rn";
  86. $this->docommand();
  87. $this->in = "datarn";
  88. $this->docommand();
  89. $this->in = $all."rn.rn";
  90. $this->docommand();
  91. $this->in = "quitrn";
  92. $this->docommand();
  93. //结束,关闭连接
  94. }
  95. function docommand()
  96. {
  97. socket_write ($this->socket, $this->in, strlen ($this->in));
  98. $this->debug_show("客户机命令:".$this->in);
  99. $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
  100. $this->debug_show($this->result_str);
  101. }
  102. }
  103. //这个是我做的测试,我用的是smtp.163.com,那你的信箱也必须是163.com的,要不人家不让你发!!
  104. //你用这个类的时候你修改成你自己的信箱就可以了
  105. $smtp = new smtp_mail("smtp.163.com","25","t_design","000000",true);
  106. //如果你需要显示会话信息,请将上面的修改成
  107. //$smtp = new smtp_mail("smtp.163.com","25","你的163.com的帐号","你的密码",true);
  108. $smtp->send("t_design@163.com","t_beijing@yahoo.com.cn","你好","你好");
  109. ?>