複製代碼 代碼如下:<?php
/*
* php smtp發送郵件Scoket類
* ZhozPhpSmtpSendMail.php
* Created on 2008/09/02 zhoz.com
* @author zhoz
* 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-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[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) {
//串連伺服器
$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."\r\n";
else $lastact="HELO ".$yourname."\r\n";
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) != "-") or (emptyempty($lastmessage)) )
break;
}
//身分識別驗證
if ($this->check=="1") {
//驗證開始
$lastact="AUTH LOGIN"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤資訊3:$lastmessage".__LINE__;
//使用者姓名
$lastact=base64_encode($this->username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤資訊4:$lastmessage".__LINE__;
//使用者密碼
$lastact=base64_encode($this->password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "錯誤資訊5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤資訊6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: <". $to ."> \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤資訊7:$lastmessage".__LINE__;
//DATA
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "錯誤資訊8:$lastmessage".__LINE__;
//處理Subject頭
$head="Subject: $subject\r\n";
$message = $head."\r\n".$message;
//處理From頭
$head="From: $from\r\n";
$message = $head.$message;
//處理To頭
$head="To: $to\r\n";
$message = $head.$message;
//加上結束串
$message .= "\r\n.\r\n";
//發送資訊
fputs($fp, $message);
$lastact="QUIT\r\n";
fclose($fp);
error_log("send over------->ok!!", 0);
return true;
}
}
?>
<?php
/*
* php smtp發送郵件Scoket類
* ZhozPhpSmtpSendMail.php
* Created on 2008/09/02 zhoz.com
* @author zhoz
* 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-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[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) {
//串連伺服器
$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."\r\n";
else $lastact="HELO ".$yourname."\r\n";
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) != "-") or (empty($lastmessage)) )
break;
}
//身分識別驗證
if ($this->check=="1") {
//驗證開始
$lastact="AUTH LOGIN"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤資訊3:$lastmessage".__LINE__;
//使用者姓名
$lastact=base64_encode($this->username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "錯誤資訊4:$lastmessage".__LINE__;
//使用者密碼
$lastact=base64_encode($this->password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "錯誤資訊5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤資訊6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: <". $to ."> \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "錯誤資訊7:$lastmessage".__LINE__;
//DATA
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "錯誤資訊8:$lastmessage".__LINE__;
//處理Subject頭
$head="Subject: $subject\r\n";
$message = $head."\r\n".$message;
//處理From頭
$head="From: $from\r\n";
$message = $head.$message;
//處理To頭
$head="To: $to\r\n";
$message = $head.$message;
//加上結束串
$message .= "\r\n.\r\n";
//發送資訊
fputs($fp, $message);
$lastact="QUIT\r\n";
fclose($fp);
error_log("send over------->ok!!", 0);
return true;
}
}
?>
測試方法一樣。
運行代碼查看代碼複製代碼列印關於
<?php
/**
* ZhozPhpSmtpSendMail.php
* Created on 2008/09/02 zhoz.com
* php smtp發送郵件類
* 代替mail():因為它報錯,我又不想
*/
// 這裡配置發信伺服器資訊,如果不改也可以正常使用:
$from = 'zhoz@zhoz.com'; // 你的發信伺服器上的郵箱
$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-----------------!";
die;
}
// 結果當然是:send ok-----------------!
?>