一个通过SMTP发mail的Class
上一篇 / 下一篇 2007-01-19 14:11:07 / 个人分类:网站
ad下.www.514200.cn
<?php
/**
* SMTP Class
* Author:lazy
* Mail:o0lazy0o at gmail dot com
* Page:[url=http://www.ourmind.cn/]http://www.ourmind.cn/[/url]
* Version: 0.1 beta
* Function: only text/html mail no support attachment
* Copyright:BSD.
* Welcome To:[url=http://www.52radio.net/]http://www.52radio.net/[/url]
*
example
$SMTP=new smtp();
$SMTP->Debug=true;
$SMTP->MailType="TEXT";//TEXT or HTMl
$SMTP->Hostname="mail.52radio.net";
$SMTP->Username="[email=sender@52radio.net]sender@52radio.net[/email]";
$SMTP->Password="password";
$SMTP->From="[email=sender@52radio.net]sender@52radio.net[/email]";
$SMTP->FromName="Sender at 52radio.net";
$SMTP->MailTo[]='[email=mail_1@52radio.net]mail_1@52radio.net[/email]';
$SMTP->MailTo[]='[email=mail_2@52radio.net]mail_2@52radio.net[/email]';
$SMTP->MailTo[]='[email=mail_3@52radio.net]mail_3@52radio.net[/email]';
$SMTP->Subject="The Test Mail Subject";
$SMTP->Message="The Test Mail Body Message.";
$SMTP->send();
*/
class smtp{
var $Hostname="localhost";
var $Port=25;
var $Auth=true;
var $Username="mailer";
var $Password="password";
var $MailTo=array();
var $From="";
var $FromName;
var $MailType="HTML";
var $CRLF="\r\n";
var $Debug=false;
var $Socket;
var $Subject;
var $Message;
var $Charset='UTF-8';
var $Response;
function smtp(){
require_once('./abend.php');
}
function connect(){
$this->Socket=fsockopen($this->Hostname,$this->;Port,$Errno,$Errstr,30);
if(!$this->Socket){
abend::stop("Connect failure:$Errno--$Errstr","SMTP");
}
$this->response()!="220"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
return $Return;
}
/**
* send helo command
*
* @return ture or false
*/
function helo(){
empty($_SERVER['SERVER_NAME'])?$Name=md5('localhost'):$Name=md5($_SERVER['SERVER_NAME']);
fwrite($this->Socket,"HELO {$Name}{$this->CRLF}");
$this->response()!="250"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
return $Return;
}
/**
* send auth login command
*
* @return ture or false
*/
function auth(){
$Username=base64_encode($this->Username);
$Password=base64_encode($this->;Password);
//send AUTH LOGIN commend
fwrite($this->Socket,"AUTH LOGIN {$this->CRLF}");
$this->response()!="334"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
//send username
fwrite($this->Socket,"$Username{$this->CRLF}");
$this->response()!="334"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
//send password
fwrite($this->Socket,"$Password{$this->CRLF}");
$this->response()!="235"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
return $Return;
}
/**
* send mail command
*
* @return true or false
*/
function mail(){
fwrite($this->Socket,"MAIL FROM:<{$this->From}>{$this->CRLF}");
$this->response()!="250"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
return $Return;
}
/**
* send rcpt command
*
* @return true or false
*/
function rcpt(){
foreach($this->MailTo as $To){
fwrite($this->Socket,"RCPT TO:<{$To}>{$this->CRLF}");
$this->response()!="250"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
}
return $Return;
}
/**
* send data command
*
* @return true or false
*/
function data(){
fwrite($this->Socket,"DATA".$this->CRLF);
$this->response()!="354"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
return $Return;
}
/**
* proces the mail header and mail message body
*
* @return true or false
*/
function proces(){
$C=0;
$_C=count($this->MailTo)-1;
foreach($this->MailTo as $_V){
$_C==$C?$To.="$_V":$To.="$_V,";
$C++;
}
$Hostname=explode('@',$this->From);
$this->MailType=='HTML'?$MailType="text/html":$MailType="text/plain";
//Disposition-Notification-To:< [email=mail@addr.doma]mail@addr.doma[/email]in >
$Header="Return-Path: ".trim($this->FromName)."\n".
"Return-Path: ".trim($this->From)."\n".
"Message-ID: <".md5(time())."@".$Hostname[1].">\n".
"From: \"{$this->FromName}\" <".$this->From.">\n".
"Reply-To: \"{$this->FromName}\" <".$this->From.">\n".
"To: ".$To."\n".
"Subject: ".$this->Subject."\n".
"Date: ".date("r",time())."\n".
"MIME-Version: 1.0\n".
"Content-Type: {$MailType}; charset={$this->Charset}\n".
"Content-Transfer-Encoding: base64\n".
"X-Priority: 3\n".
"X-MSMail-Priority: Normal\n".
"X-Mailer: PHP SMTP Class Mailer\n".
"X-MimeOLE: Produced By Lazy 0.1 Beta\n\n";
$BodyArray=explode('\n',$this->Message);
foreach($BodyArray as $_V){
$this->Message=base64_encode($_V)."\n";
}
$Body=$Header.$this->Message."{$this->CRLF}.{$this->CRLF}";
fwrite($this->Socket,$Body);
$this->response()!="250"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
return $Return;
}
/**
* send mail
*
* @return true or false
*/
function send(){
$this->connect();
$this->helo();
$this->auth();
$this->mail();
$this->rcpt();
$this->data();
$this->proces();
$this->quit();
}
/**
* quit the session
*
* @return true or false
*/
function quit(){
fwrite($this->Socket,"QUIT ".$this->CRLF);
$this->response()!="221"?$Return=abend::stop("Error:{$this->Response}","SMTP"):$Return=true;
return $Return;
}
/**
* get the smtp response
*
* @return true or false
*/
function response(){
while($this->Response=fgets($this->Socket,512)){
$Return.=$this->Response;
if($this->Debug){
echo $Return;
}
if(substr($this->Response,3,1)==" "){
break;
}
}
return substr($Return,0,3);
}
}
?>
相关阅读:
- 【网站收藏】波波三国战纪World国内最权威之三国战纪专题站 (亲密飞翔, 2006-12-26)
- 巴中在线 (crazybird, 2006-12-27)
- 巴中论坛 (crazybird, 2006-12-27)
- 鱼儿居 (bbk4980, 2006-12-28)
- 我们的外语通 (bbk4980, 2006-12-28)
- 感谢SS和DZ为我提供这么好的平台 (meizhouliyu, 2007-1-08)
- 地方站交换连接. (52radio, 2007-1-14)
- 接吻感觉真好 (52radio, 2007-1-17)
- ECSHOP最大的免费…… (阿诗玛, 2007-1-18)
TAG: 网站
标题搜索
我的存档
数据统计
- 访问量: 131938
- 日志数: 219
- 文件数: 20
- 书签数: 43
- 建立时间: 2006-05-16
- 更新时间: 2008-08-03
