一个通过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);
        }
}
?>


TAG: 网站

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar