一个基于SMTP的邮件发送类

PHP自带的Mail函数使用起来不怎么灵活,而且不支持SMTP。

参考网上的一些资料写了一个SMTP邮件类,花了半天时间,不过可惜不支持基于SSL的GMail,群发、附件及MIME邮件功能接下来继续扩充。

源文件:下载文件 点击下载此文件

[code]

/*
* SMTP 邮件发送类
* Akon @ 2007/02/01
*/

class SMTPMail {

var $_mailFrom;
var $_mailTo;
var $_mailSubject;
var $_mailBody;
var $_host;
var $_user;
var $_pass;
var $_port;
var $_socket;
var $_request;
var $_conn;
var $_debug;
var $_log;

function SMTPMail($host, $user, $pass, $port=25, $debug=false) {
$this->_host = $host;
$this->_user = base64_encode($user);
$this->_pass = base64_encode($pass);
$this->_port = $port;
$this->_debug = $debug;
$this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->_socket) $this->Output(1001);
else $this->Output(2001);
$this->_conn = socket_connect($this->_socket, $this->_host, $this->_port);
if ($this->_conn) $this->Output(1002);
else $this->Output(2002);
$this->Output(1003);
}

function SendMail($mailFrom, $mailTo, $mailSubject, $mailBody) {
if (!$this->CheckMail($mailFrom)) $this->Output(2003);
elseif (empty($mailTo)) $this->Output(2004);
elseif (empty($mailSubject)) $this->Output(2005);
elseif (empty($mailBody)) $this->Output(2006);
else {
$this->_mailFrom = $mailFrom;
$this->_mailTo = $mailTo;
$this->_mailSubject = $mailSubject;
$this->_mailBody = $mailBody;
$SendContent = “From:{$this->_mailFrom}\r\n”;
$SendContent .= “To:{$this->_mailTo}\r\n”;
$SendContent .= “Subject:{$this->_mailSubject}\r\n”;
$SendContent .= $this->_mailBody;
$this->docommand(”EHLO HELO\r\n”);
$this->docommand(”AUTH LOGIN\r\n”);
$this->docommand($this->_user . “\r\n”);
$this->docommand($this->_pass . “\r\n”);
$this->docommand(”MAIL FROM: <" . $this->_mailFrom. “>\r\n”);
$this->docommand(”RCPT TO: <" . $this->_mailTo . “>\r\n”);
$this->docommand(”DATA\r\n”);
$this->docommand($SendContent .”\r\n.\r\n”);
$this->docommand(”QUIT\r\n”);
socket_close($this->_socket);
}
}

function CheckMail($email) {
return (ereg( “^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$”, $email));
}

function docommand($request) {
$this->_request = $request;
socket_write ($this->_socket, $this->_request, strlen ($this->_request));
$this->Output(1004);
$this->Output(1003);
}

function Output($msgcode) {
switch ($msgcode) {
case 1001 :
$msg = “SOCKET Create: ” . socket_strerror(socket_last_error()) . “”;
break;
case 1002 :
$msg = “SOCKET Connection: ” . socket_strerror(socket_last_error()) . “”;
break;
case 1003 :
$msg = “BACK: ” . socket_read ($this->_socket, 1024) . ““;
break;
case 1004 :
$msg = “CWD: ” . htmlentities($this->_request) . ““;
break;
case 2001 :
$msg = “SOKET initialization failure, please check your network connections and parameters.”;
break;
case 2002 :
$msg = “SOKET failure to establish links, please check your network connections and parameters.”;
break;
case 2003 :
$msg = “Error : Mail FROM is invalid mail format.”;
break;
case 2004 :
$msg = “Error : Mail TO is null.”;
break;
case 2005 :
$msg = “Error : Mail Subject is null.”;
break;
case 2006 :
$msg = “Error : Mail Body is null.”;
break;
case 2007 :
$msg = “Error : “;
break;
default :
$msg = $msgcode;
break;
}
if ($this->_debug) $this->_log .= “{$msg}”;
}

}

$SMTPMail = new SMTPMail(”smtp.163.com”, “toale”, '******', 25 ,true);
$SMTPMail->SendMail(”toale@163.com”, “aultoale@126.com”, “this is mail title”, “this is mail body”);
echo nl2br($SMTPMail->_log);
?>[/code]

Theme Brought to you by Directory Journal and Elegant Directory