程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php win下Socket方式發郵件類

php win下Socket方式發郵件類

編輯:PHP綜合

復制代碼 代碼如下:
<?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 = '[email protected]'; // 你的發信服務器上的郵箱
$password = 'zhoz'; // 你的郵箱密碼
$smtp = 'smtp.zhoz.com'; // 您的SMTP 服務器供應商,可以是域名或IP地址
// 類初始化
$zhoz_obj = new ZhozPhpSmtpSendMail($from, $password, $smtp);
// 發送開始
$status = $zhoz_obj->zhoz_send("[email protected]", "[email protected]", "subject is zhoz", "body is zhoz.com");
if($status) {
echo $status;
echo "no---------";
} else {
echo "send ok-----------------!";
die;
}
// 結果當然是:send ok-----------------!
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved