最近一個項目需要用到SMTP發送郵件,之前的庫類不存在了,又不喜歡安裝pear或者使用pear的net/smtp類,感覺太復雜了。就直接從discuz中抽取出核心稍微修改了下。
從協議分析網上,查找到SMTP協議的命令和應答,SMTP協議在發送SMTP和接收SMTP之間的會話是靠發送SMTP的SMTP命令和接收SMTP反饋的應答來完成的。常用的命令如下:
HELLO<domain><CRLF>識別發送方到接收SMTP的一個HELLO命令
MAIL FROM:<reverse-path><CRLF><reverse-path>為發送者地址。此命令告訴接收方一個新郵件發送的開始,並對所有的狀態和緩沖區進行初始化。此命令開始一個郵件傳輸處理,最終完成將郵件數據傳送到一個或多個郵箱中。
RCPT TO:<forward-path><CRLF><forward-path>標識各個郵件接收者的地址
DATA<CRLF>
接收SMTP將把其後的行為看作郵件數據去處理,以<CRLF>.<CRLF>標識數據的結尾。
REST<CRLF>退出/復位當前的郵件傳輸
NOOP<CRLF>要求接收SMTP僅做OK應答。(用於測試)
QUIT<CRLF>要求接收SMTP返回一個OK應答並關閉傳輸。
VRFY<string><CRLF>驗證指定的郵箱是否存在,由於安全因素,服務器多禁止此命令。
EXPN<string><CRLF>驗證給定的郵箱列表是否存在,擴充郵箱列表,也常禁止使用。
HELP<CRLF>查詢服務器支持什麼命令
注:<CRLF>為回車、換行,ASCII碼分別為13、10(十進制)。
另外,可以在command下,使用telnet來進行簡單的手工使用SMTP。
比如:
telnet smtp.263.net 25
Trying 211.150.96.25...
Connected to smtp.263.net.
Escape character is '^]'.
220 Welcome to coremail System(With Anti-Spam) 2.1 for 263(040326)
HELO
[email protected] 250 smtp.263.net
mail from:
[email protected] 250 Ok
rcpt to:
[email protected] 250 Ok
data
354 End data with <CR><LF>.<CR><LF>
haha
.
250 Ok: queued as B9E452FF3E
quit
221 Bye
Connection closed by foreign host.
在此基礎上就可以寫出一個簡單的SMTP類了。
<?
class stmp{
private $mailcfg=array();
private $error_msg='';
function __construct($mailcfg){
$this->mailcfg=$mailcfg;
}
public function send($mail){
$mailcfg=$this->mailcfg;
if(!$fp = fsockopen($mailcfg['server'], $mailcfg['port'], $errno, $errstr, 30)) {
return $this->error("($mailcfg[server]:$mailcfg[port]) CONNECT - Unable to connect to the SMTP server, please check your \"mail_config.php\".");
}
stream_set_blocking($fp, true);
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != '220') {
return $this->error("$mailcfg[server]:$mailcfg[port] CONNECT - $lastmessage");
}
fputs($fp, ($mailcfg['auth'] ? 'EHLO' : 'HELO')." ".$mailcfg['auth_username']."\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
return $this->error("($mailcfg[server]:$mailcfg[port]) HELO/EHLO - $lastmessage");
}
while(1) {
if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
break;
}
$lastmessage = fgets($fp, 512);
}
if($mailcfg['auth']) {
fputs($fp, "AUTH LOGIN\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
return $this->error("($mailcfg[server]:$mailcfg[port]) AUTH LOGIN - $lastmessage");
}
fputs($fp, base64_encode($mailcfg['auth_username'])."\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
return $this->error("($mailcfg[server]:$mailcfg[port]) USERNAME - $lastmessage");
}
fputs($fp, base64_encode($mailcfg['auth_password'])."\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 235) {
return $this->error("($mailcfg[server]:$mailcfg[port]) PASSWORD - $lastmessage");
}
$email_from = $mailcfg['from'];
}
fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
return $this->error("($mailcfg[server]:$mailcfg[port]) MAIL FROM - $lastmessage");
}
}
$email_to=$mail['to'];
foreach(explode(',', $email_to) as $touser) {
$touser = trim($touser);
if($touser) {
fputs($fp, "RCPT TO: <$touser>\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp, "RCPT TO: <$touser>\r\n");
$lastmessage = fgets($fp, 512);
return $this->error("($mailcfg[server]:$mailcfg[port]) RCPT TO - $lastmessage");
}
}
}
fputs($fp, "DATA\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 354) {
return $this->error("($mailcfg[server]:$mailcfg[port]) DATA - $lastmessage");
}
$str="To: $email_to\r\nFrom: $email_from\r\nSubject: ".$mail['subject']."\r\n\r\n".$mail['content']."\r\n.\r\n";
fputs($fp, $str);
fputs($fp, "QUIT\r\n");
return true;
}
public function get_error(){
return $this->error_msg;
}
private function error($msg){
$this->error_msg.=$msg;
return false;
}
}
?>
簡單的調用例子:
<?
$mailcfg['server'] = 'smtp.163.com';
$mailcfg['port'] = '25';
$mailcfg['auth'] = 1;
$mailcfg['from'] = 'test <
[email protected]>';
$mailcfg['auth_username'] = 'test';
$mailcfg['auth_password'] = 'password';
$stmp=new stmp($mailcfg);
$mail=array('to'=>'
[email protected]','subject'=>'測試標題','content'=>'郵件內容<a href="http://www.phpobject.net">PHP面向對象</a>');
if(!$stmp->send($mail)){
echo $stmp->get_error();
}else{
echo 'mail succ!';
}
?>
如果發送成功,你就可以去郵箱查看郵件了。^_^