複製程式碼 程式碼如下:
/*
* php smtp寄電子郵件Scoket類別
* ZhozPhpSmtpSendMail.php
* Created on 2008/09/02 zhoz.com
* Created on 2008/09/02 zhoz.com
* Created on 2008/09/02 zhoz.com
* Created on 2008/09/02 zhoz.com
* Created on 2008/09/02 zhoz.com
* Created on 2008/09/02 zhoz.com
* Created on 2008/09/02 zhoz.com
* Created on 2008/09/02 zhoz> author zhoz
* v 1.72 update 2008/09/26 zhoz.Com
*/
class ZhozPhpSmtpSendMail {
//您的SMTP 伺服器供應商,可以是網域名稱或IPs//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-Zd-]+[^-](.[^-][a-zA-Zd-] *[^-])*.[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 來源信箱
* $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."rn";
else $lastact="HELO " .$yourname."rn";
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) != "-") 或 (emptyempty($lastmessage)) )
break;
}
//驗證
if ($this->check=="1") {
//驗證開始
$lastact="AUTH LOGIN"."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤訊息3:$lastmessage".__LINE__;
//使用者姓名
$lastact=base64_encode($this->username). "rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤訊息4:$lastmessage".__LINE__;
//使用者密碼
$lastact=base64_encode($this->password)."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "錯誤訊息5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: s_from . ">rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($ fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤訊息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0 ,3) != 250) return "錯誤訊息7:$lastmessage".__LINE__;
//DATA
$lastact="DATArn";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "錯誤訊息8:$lastmessage".__LINE__;
//處理Subject頭
$head="Subject: $subjectrn";
$message = $head."rn".$message;
//處理From頭
$head="From: $fromrn";
$message = $head.$message;
//處理To頭
$head="To: $torn";
$message = $head.$message;
//加上結束字串
$message .= "rn.rn";
//發送訊息
fputs($fp, $message);
$lastact="QUITrn";
fclose( $fp);
error_log("send over------->ok!!", 0);
return true;
}
}
?>
/*
* php smtp寄信Scoket類別
* ZhozPhpSmtpSendMail.php * Created on 2008/09/02 zhoz.com * Created on 2008/09/02 zhoz.com * 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-Zd-]+[^-](.[^-][a-zA-Zd-]*[^ -])*.[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) {
//連接伺服器
$openfp = fsock ( $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."rn";
else $lastact="HELO ".$yourname ."rn";
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) ! = "-") 或 (empty($lastmessage)) )
break;
}
//驗證
if ($this->check=="1") {
//驗證開始
$lastact="AUTH LOGIN"."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if ( substr($lastmessage,0,3) != 334) return "錯誤訊息3:$lastmessage".__LINE__;
//使用者姓名
$lastact=base64_encode($this->username)."rn" ;
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤訊息4 :$lastmessage".__LINE__;
//使用者密碼
$lastact=base64_encode($this->password)."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "錯誤訊息5:$lastmessage".__LINE__;
}
// FROM:
$lastact="MAIL FROM: s_from . ">rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512 );
if (substr($lastmessage,0,3) != 250) return "錯誤訊息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤訊息7:$lastmessage".__LINE__;
//DATA
$lastact="DATArn";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "錯誤訊息8:$lastmessage".__LINE__;
//處理Subject頭
$ head="Subject: $subjectrn";
$message = $head."rn".$message;
//處理From頭
$head="From: $fromrn";
$ message = $head.$message;
//處理To頭
$head="To: $torn";
$message = $head.$message;
//加上結束串
$message .= "rn.rn";
//發送訊息
fputs($fp, $message);
$lastact="QUITrn";
fclose($fp) ;
error_log("send over------->ok!!", 0);
return true;
}
}
?>
測試方法一樣。
運行程式碼查看程式碼複製程式碼列印關於
/**
* ZhozPhpSmtpSendMail.php
* Created on 2008/09/02 zhoz.com
* php smtp發送郵件類別
* 代替mail():因為它報錯,我又不想
*/
// 這裡配置發信伺服器資訊,如果不改也可以正常使用:
$from = 'zhoz@zhoz.com'; // 你的寄信伺服器上的信箱
$password = 'zhoz'; // 你的郵件伺服器上的信箱
$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----------- ------!
?>