php win下Socket方式發郵件類

來源:互聯網
上載者:User

複製代碼 代碼如下:<?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-----------------!
?>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.