<?* 名稱:用Socket寄送電子郵件 * 描述:本類實現了直接使用需要驗證的SMTP伺服器直接發送郵件,參考文章《用Socket寄送電子郵件》作者:limodou * 此文章比較早,他是用不用驗證SMTP伺服器發送郵件,現在基本上SMTP伺服器都需要驗證了,所以這個文章裡的類 意義也不是很大!同時參考了[RFC 1869]和PHP手冊!!和上文還有不同的是我用的不是fsockopen()函數 具體你自己看吧!!我剛剛測試通過了,很爽!! 其實把這個類再改寫一下就可以直接發送帶附件的郵件了,期待。。。我過幾天給大家寫出來! 我剛剛調試通過,如果你不惡意去捉弄這個程式,他還是很聽話的,過些日子我會對他完善加上發送附件的功能!!即使你的虛擬機器主機不支援MAIL函數,現在也不怕了!快快試試這個吧! 請尊重別人的勞動成功,請保留此著作權資訊,謝謝! 作者:小露珠3.3 ,歡迎和我聯絡交流PHP QQ6550382 MAIL:cleverzhang@tom.com */ 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> "; } } 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." "; $All .= "To:".$this->to." "; $All .= "Subject:".$this->subject." "; $All .= $this->body; /* 如過把$All的內容再加處理,就可以實現發送MIME郵件了 不過還需要加很多程式 */ //以下是和伺服器會話 $this->in = "EHLO HELO "; $this->docommand(); $this->in = "AUTH LOGIN "; $this->docommand(); $this->in = $this->user." "; $this->docommand(); $this->in = $this->pass." "; $this->docommand(); $this->in = "MAIL FROM:".$this->from." "; $this->docommand(); $this->in = "RCPT TO:".$this->to." "; $this->docommand(); $this->in = "DATA "; $this->docommand(); $this->in = $All." . "; $this->docommand(); $this->in = "QUIT "; $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); } } //這個是我做的測試,我用的是smtp.163.com,那你的信箱也必須是163.com的,要不人家不讓你發!!//你用這個類的時候你修改成你自己的信箱就可以了 $smtp = new smtp_mail("smtp.163.com","25","你的163.com的帳號","你的密碼"); //如果你需要顯示會話資訊,請將上面的修改成 //$smtp = new smtp_mail("smtp.163.com","25","你的163.com的帳號","你的密碼",true); $smtp->send("你的163.com的帳號@163.com","目標地址","你好","你好"); ?>