- /*請尊重別人的勞動成功,請保留此版權訊息,謝謝!
- 作者:小露珠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);
- $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 = "伺服器回應:".socket_read ($this->socket, 1024)."";
- $this->debug_show($ this->result_str);
-
- }
- function debug_show($str)
- {
- if($this->debug)
- {
- echo $str."rn";
- }
- }
- 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:from.">rn";
- $All .= "To:to.">rn";
- $All .= "Subject:".$this->subject."rnrn";
- $All .= $this->body;
- /*
- 如過把$All的內容再加處理,就可以實現發送MIME郵件了
- 不過還需要加很多程序
- */
-
- //以下是和伺服器會話
- $this->in = "EHLO HELOrn";
- $this->docommand();
-
- $this->in = "AUTH LOGINrn";
- $this->docommand();
-
- $this->in = $this->user."rn";
- $this->docommand();
-
- $this->in = $this->pass."rn";
- $this->docommand();
-
- // $this->in = "MAIL FROM:". $this->from."rn";
- $this->in = "MAIL FROM:from.">rn"; //揚帆修改
- $this->docommand( );
-
- // $this->in = "RCPT TO:".$this->to."rn";
- $this->in = "RCPT TO:to.">rn"; //揚帆修改
- $this->docommand();
-
- $this->in = "DATArn";
- $this->docommand() ;
-
- $this->in = $All."rn.rn";
- $this->docommand();
-
- $this->in = "QUITrn";
- $this->docommand();
-
- //結束,關閉連線
-
- }
- function docommand()
- {
- socket_write ($this-this>socket , $this->in, strlen ($this->in));
- $this->debug_show("客戶機指令:".$this->in);
- $this->result_str = "伺服器回應:".socket_read ($this->socket, 1024)."";
- $this->debug_show($this->result_str);
- }
- }
?>
複製程式碼
php程式碼
-
//測試頁
- include "smtp_mail.php";
- //測試頁
- include "smtp_mail.php";
-
-
-
-
-
//你用這個類別的時候你修改成你自己的信箱就可以了 $smtp=new smtp_mail("smtp.qq.com","25","yourmail@qq.com"," Your password",true);//如果你需要顯示會話訊息,請將上面的修改成 //$smtp = new smtp_mail("smtp.qq.com","25","你的qq.com的帳號","你的密碼",true); $smtp->send("yourmail@qq.com","yourmail@qq.com","你好","測試郵件"); | ?>