php a simple smtp class
更新時間:2007年11月26日 15:20:22 作者:
smtp.class.php
<?php
define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
define('SMTP_STATUS_CONNECTED', 2, TRUE);
class smtp
{
var $connection;
var $recipients;
var $headers;
var $timeout;
var $errors;
var $status;
var $body;
var $from;
var $host;
var $port;
var $helo;
var $auth;
var $user;
var $pass;
var $debug;
/**
* 參數(shù)為一個數(shù)組
* host SMTP 服務(wù)器的主機(jī) 默認(rèn):localhost
* port SMTP 服務(wù)器的端口 默認(rèn):25
* helo 發(fā)送HELO命令的名稱 默認(rèn):localhost
* user SMTP 服務(wù)器的用戶名 默認(rèn):空值
* pass SMTP 服務(wù)器的登陸密碼 默認(rèn):空值
* timeout 連接超時的時間 默認(rèn):5
* @return bool
*/
function smtp($params = array())
{
if(!defined('CRLF')) define('CRLF', “\r\n”, TRUE);
$this->timeout = 5;
$this->status = SMTP_STATUS_NOT_CONNECTED;
$this->host = ‘localhost';
$this->port = 25;
$this->auth = FALSE;
$this->user = ”;
$this->pass = ”;
$this->errors = array();
$this->debug = false;
foreach($params as $key => $value)
{
$this->$key = $value;
}
$this->helo = $this->host;
// 如果沒有設(shè)置用戶名則不驗(yàn)證
$this->auth = (” == $this->user) ? FALSE : TRUE;
}
function connect($params = array())
{
if(!isset($this->status))
{
$obj = new smtp($params);
if($obj->connect())
{
$obj->status = SMTP_STATUS_CONNECTED;
}
return $obj;
}
else
{
$this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
socket_set_timeout($this->connection, 0, 250000);
$greeting = $this->get_data();
if(is_resource($this->connection))
{
$this->status = 2;
return $this->auth ? $this->ehlo() : $this->helo();
}
else
{
$this->errors[] = ‘Failed to connect to server: ‘.$errstr;
return FALSE;
}
}
}
/**
* 參數(shù)為數(shù)組
* recipients 接收人的數(shù)組
* from 發(fā)件人的地址,也將作為回復(fù)地址
* headers 頭部信息的數(shù)組
* body 郵件的主體
*/
function send($params = array())
{
foreach($params as $key => $value)
{
$this->set($key, $value);
}
if($this->is_connected())
{
// 服務(wù)器是否需要驗(yàn)證
if($this->auth)
{
if(!$this->auth()) return FALSE;
}
$this->mail($this->from);
if(is_array($this->recipients))
{
foreach($this->recipients as $value)
{
$this->rcpt($value);
}
}
else
{
$this->rcpt($this->recipients);
}
if(!$this->data()) return FALSE;
$headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
$body = str_replace(CRLF.'.', CRLF.'..', $this->body);
$body = $body[0] == ‘.' ? ‘.'.$body : $body;
$this->send_data($headers);
$this->send_data(”);
$this->send_data($body);
$this->send_data('.');
return (substr(trim($this->get_data()), 0, 3) === ‘250′);
}
else
{
$this->errors[] = ‘Not connected!';
return FALSE;
}
}
function helo()
{
if(is_resource($this->connection)
AND $this->send_data('HELO ‘.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === ‘250′ )
{
return TRUE;
}
else
{
$this->errors[] = ‘HELO command failed, output: ‘ . trim(substr(trim($error),3));
return FALSE;
}
}
function ehlo()
{
if(is_resource($this->connection)
AND $this->send_data('EHLO ‘.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === ‘250′ )
{
return TRUE;
}
else
{
$this->errors[] = ‘EHLO command failed, output: ‘ . trim(substr(trim($error),3));
return FALSE;
}
}
function auth()
{
if(is_resource($this->connection)
AND $this->send_data('AUTH LOGIN')
AND substr(trim($error = $this->get_data()), 0, 3) === ‘334′
AND $this->send_data(base64_encode($this->user)) // Send username
AND substr(trim($error = $this->get_data()),0,3) === ‘334′
AND $this->send_data(base64_encode($this->pass)) // Send password
AND substr(trim($error = $this->get_data()),0,3) === ‘235′ )
{
return TRUE;
}
else
{
$this->errors[] = ‘AUTH command failed: ‘ . trim(substr(trim($error),3));
return FALSE;
}
}
function mail($from)
{
if($this->is_connected()
AND $this->send_data('MAIL FROM:<'.$from.'>')
AND substr(trim($this->get_data()), 0, 2) === ‘250′ )
{
return TRUE;
}
else
{
return FALSE;
}
}
function rcpt($to)
{
if($this->is_connected()
AND $this->send_data('RCPT TO:<'.$to.'>')
AND substr(trim($error = $this->get_data()), 0, 2) === ‘25′ )
{
return TRUE;
}
else
{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
function data()
{
if($this->is_connected()
AND $this->send_data('DATA')
AND substr(trim($error = $this->get_data()), 0, 3) === ‘354′ )
{
return TRUE;
}
else
{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
function is_connected()
{
return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
}
function send_data($data)
{
if(is_resource($this->connection))
{
if($this->debug)
echo nl2br($data.CRLF);
return fwrite($this->connection, $data.CRLF, strlen($data)+2);
}
else
{
return FALSE;
}
}
function &get_data()
{
$return = ”;
$line = ”;
if(is_resource($this->connection))
{
while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ‘ ‘)
{
$line = fgets($this->connection, 512);
$return .= $line;
}
if($this->debug===true)
echo nl2br($return.CRLF);
return $return;
}
else
{
return FALSE;
}
}
function set($var, $value)
{
$this->$var = $value;
return TRUE;
}
} // End of class
?>
復(fù)制代碼 代碼如下:
<?php
define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
define('SMTP_STATUS_CONNECTED', 2, TRUE);
class smtp
{
var $connection;
var $recipients;
var $headers;
var $timeout;
var $errors;
var $status;
var $body;
var $from;
var $host;
var $port;
var $helo;
var $auth;
var $user;
var $pass;
var $debug;
/**
* 參數(shù)為一個數(shù)組
* host SMTP 服務(wù)器的主機(jī) 默認(rèn):localhost
* port SMTP 服務(wù)器的端口 默認(rèn):25
* helo 發(fā)送HELO命令的名稱 默認(rèn):localhost
* user SMTP 服務(wù)器的用戶名 默認(rèn):空值
* pass SMTP 服務(wù)器的登陸密碼 默認(rèn):空值
* timeout 連接超時的時間 默認(rèn):5
* @return bool
*/
function smtp($params = array())
{
if(!defined('CRLF')) define('CRLF', “\r\n”, TRUE);
$this->timeout = 5;
$this->status = SMTP_STATUS_NOT_CONNECTED;
$this->host = ‘localhost';
$this->port = 25;
$this->auth = FALSE;
$this->user = ”;
$this->pass = ”;
$this->errors = array();
$this->debug = false;
foreach($params as $key => $value)
{
$this->$key = $value;
}
$this->helo = $this->host;
// 如果沒有設(shè)置用戶名則不驗(yàn)證
$this->auth = (” == $this->user) ? FALSE : TRUE;
}
function connect($params = array())
{
if(!isset($this->status))
{
$obj = new smtp($params);
if($obj->connect())
{
$obj->status = SMTP_STATUS_CONNECTED;
}
return $obj;
}
else
{
$this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
socket_set_timeout($this->connection, 0, 250000);
$greeting = $this->get_data();
if(is_resource($this->connection))
{
$this->status = 2;
return $this->auth ? $this->ehlo() : $this->helo();
}
else
{
$this->errors[] = ‘Failed to connect to server: ‘.$errstr;
return FALSE;
}
}
}
/**
* 參數(shù)為數(shù)組
* recipients 接收人的數(shù)組
* from 發(fā)件人的地址,也將作為回復(fù)地址
* headers 頭部信息的數(shù)組
* body 郵件的主體
*/
function send($params = array())
{
foreach($params as $key => $value)
{
$this->set($key, $value);
}
if($this->is_connected())
{
// 服務(wù)器是否需要驗(yàn)證
if($this->auth)
{
if(!$this->auth()) return FALSE;
}
$this->mail($this->from);
if(is_array($this->recipients))
{
foreach($this->recipients as $value)
{
$this->rcpt($value);
}
}
else
{
$this->rcpt($this->recipients);
}
if(!$this->data()) return FALSE;
$headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
$body = str_replace(CRLF.'.', CRLF.'..', $this->body);
$body = $body[0] == ‘.' ? ‘.'.$body : $body;
$this->send_data($headers);
$this->send_data(”);
$this->send_data($body);
$this->send_data('.');
return (substr(trim($this->get_data()), 0, 3) === ‘250′);
}
else
{
$this->errors[] = ‘Not connected!';
return FALSE;
}
}
function helo()
{
if(is_resource($this->connection)
AND $this->send_data('HELO ‘.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === ‘250′ )
{
return TRUE;
}
else
{
$this->errors[] = ‘HELO command failed, output: ‘ . trim(substr(trim($error),3));
return FALSE;
}
}
function ehlo()
{
if(is_resource($this->connection)
AND $this->send_data('EHLO ‘.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === ‘250′ )
{
return TRUE;
}
else
{
$this->errors[] = ‘EHLO command failed, output: ‘ . trim(substr(trim($error),3));
return FALSE;
}
}
function auth()
{
if(is_resource($this->connection)
AND $this->send_data('AUTH LOGIN')
AND substr(trim($error = $this->get_data()), 0, 3) === ‘334′
AND $this->send_data(base64_encode($this->user)) // Send username
AND substr(trim($error = $this->get_data()),0,3) === ‘334′
AND $this->send_data(base64_encode($this->pass)) // Send password
AND substr(trim($error = $this->get_data()),0,3) === ‘235′ )
{
return TRUE;
}
else
{
$this->errors[] = ‘AUTH command failed: ‘ . trim(substr(trim($error),3));
return FALSE;
}
}
function mail($from)
{
if($this->is_connected()
AND $this->send_data('MAIL FROM:<'.$from.'>')
AND substr(trim($this->get_data()), 0, 2) === ‘250′ )
{
return TRUE;
}
else
{
return FALSE;
}
}
function rcpt($to)
{
if($this->is_connected()
AND $this->send_data('RCPT TO:<'.$to.'>')
AND substr(trim($error = $this->get_data()), 0, 2) === ‘25′ )
{
return TRUE;
}
else
{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
function data()
{
if($this->is_connected()
AND $this->send_data('DATA')
AND substr(trim($error = $this->get_data()), 0, 3) === ‘354′ )
{
return TRUE;
}
else
{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
function is_connected()
{
return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
}
function send_data($data)
{
if(is_resource($this->connection))
{
if($this->debug)
echo nl2br($data.CRLF);
return fwrite($this->connection, $data.CRLF, strlen($data)+2);
}
else
{
return FALSE;
}
}
function &get_data()
{
$return = ”;
$line = ”;
if(is_resource($this->connection))
{
while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ‘ ‘)
{
$line = fgets($this->connection, 512);
$return .= $line;
}
if($this->debug===true)
echo nl2br($return.CRLF);
return $return;
}
else
{
return FALSE;
}
}
function set($var, $value)
{
$this->$var = $value;
return TRUE;
}
} // End of class
?>
您可能感興趣的文章:
- php使用pear_smtp發(fā)送郵件
- php mailer類調(diào)用遠(yuǎn)程SMTP服務(wù)器發(fā)送郵件實(shí)現(xiàn)方法
- php利用smtp類實(shí)現(xiàn)電子郵件發(fā)送
- php基于socket實(shí)現(xiàn)SMTP發(fā)送郵件的方法
- PHP實(shí)現(xiàn)支持SSL連接的SMTP郵件發(fā)送類
- php使用smtp發(fā)送支持附件的郵件示例
- php中通過curl smtp發(fā)送郵件
- PHP mail 通過Windows的SMTP發(fā)送郵件失敗的解決方案
- PHPMailer郵件類利用smtp.163.com發(fā)送郵件方法
- php下使用SMTP發(fā)郵件的代碼
- php中通過smtp發(fā)郵件的類,測試通過
- PHP的一個完整SMTP類(解決郵件服務(wù)器需要驗(yàn)證時的問題)
- php通過smtp郵件驗(yàn)證登陸的方法
相關(guān)文章
php的mkdir()函數(shù)創(chuàng)建文件夾比較安全的權(quán)限設(shè)置方法
這篇文章主要介紹了php的mkdir()函數(shù)創(chuàng)建文件夾比較安全的權(quán)限設(shè)置方法,遇到的情況是系統(tǒng)umask影響了mkdir的指定權(quán)限參數(shù)比期望要小,使用chmod函數(shù)則沒有這個問題,需要的朋友可以參考下2014-07-07Yii框架創(chuàng)建cronjob定時任務(wù)的方法分析
這篇文章主要介紹了Yii框架創(chuàng)建cronjob定時任務(wù)的方法,結(jié)合具體實(shí)例形式分析了Yii定時任務(wù)相關(guān)配置、實(shí)現(xiàn)步驟與注意事項(xiàng),需要的朋友可以參考下2017-05-05laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫示例
這篇文章主要介紹了laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫,結(jié)合實(shí)例形式分析了Laravel數(shù)據(jù)庫的基本配置與操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-10-10CI框架驗(yàn)證碼CAPTCHA輔助函數(shù)用法實(shí)例
這篇文章主要介紹了CI框架驗(yàn)證碼CAPTCHA輔助函數(shù)用法,以實(shí)例形式詳細(xì)講述了基于CAPTCHA輔助函數(shù)實(shí)現(xiàn)驗(yàn)證碼的方法,需要的朋友可以參考下2014-11-11ThinkPHP中Widget擴(kuò)展的兩種寫法及調(diào)用方法詳解
這篇文章主要介紹了ThinkPHP中Widget擴(kuò)展的兩種寫法及調(diào)用方法,詳細(xì)分析了Widget擴(kuò)展的寫法及相應(yīng)的調(diào)用技巧,需要的朋友可以參考下2017-05-05ThinkPHP連接數(shù)據(jù)庫及主從數(shù)據(jù)庫的設(shè)置教程
這篇文章主要介紹了ThinkPHP連接數(shù)據(jù)庫及主從數(shù)據(jù)庫的設(shè)置方法,是進(jìn)行大型web項(xiàng)目開發(fā)十分有用的技巧,需要的朋友可以參考下2014-08-08詳解Laravel視圖間共享數(shù)據(jù)與視圖Composer
視圖的基本使用很簡單,可查看視圖文檔了解詳情,今天這里我們演示兩個使用示例:在視圖間共享數(shù)據(jù)和視圖Composer。下面一起來看看。2016-08-08