欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

socket方式发送邮件的类

程序员文章站 2022-05-03 19:57:39
...
class MailCls{
    private $mEmails;
    
    function setEmailInfo( $emails ){
        if( is_array( $emails ) ){
            $this->mEmails['email_to'] = $emails['email_to'];
            #对邮件标题进行处理
            if( isset( $emails['email_subject'] ) || !empty($emails['email_subject'])) {
                eval("$email_subject = "".$emails['email_subject']."";");
                $email_subject = str_replace(" ", '', str_replace(" ", '', $emails['email_subject'] ));
                $this->mEmails['email_subject'] = $emails['email_subject'];
            }
            
            #对邮件内容进行处理
            if( isset( $emails['email_message']) || !empty($emails['email_message'])) {
                eval("$email_message = "".$emails['email_message']."";");
                $emails['email_message'] = str_replace(" .", "  ..", str_replace(" ", " ", str_replace(" ", " ", str_replace(" ", " ", str_replace(" ", " ", $emails['email_message'])))));
                $this->mEmails['email_message'] = $emails['email_message'];
            }
            
            #对邮件来源进行处理
            if( isset($emails['email_from']) || !empty($emails['email_from']))
                $this->mEmails['email_from'] = $emails['email_from'];
            else
                $this->mEmails['email_from'] = '';
        }
    }
    
    function getEmails( $elements  ){
        if( isset( $elements ) || !empty( $elements ))
            return $this->mEmails[$elements];
        else
        return $this->mEmails;
    }
    
    function __destruct(){
        unset( $this );
    }
}


class sendMail extends MailCls{
    private $mMailCfg;
    
    function __construct( $mailcfg ){
        if(is_array( $mailcfg ))
            $this->mMailCfg = $mailcfg;
    }
    
    #发送邮件函数
    function sendMails( ){
        if(!$fp = fsockopen($this->mMailCfg['server'], $this->mMailCfg['port'], $errno, $errstr, 30)) {
                errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) CONNECT - Unable to connect to the SMTP server, please check your "mail_config.php".", 0);
            }
         stream_set_blocking($fp, true);

        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != '220') {
            errorlog('SMTP', "$this->mMailCfg[server]:$this->mMailCfg[port] CONNECT - $lastmessage", 0);
        }

        fputs($fp, ($this->mMailCfg['auth'] ? 'EHLO' : 'HELO')." hoodong ");
        $lastmessage = fgets($fp, 512);
        
        if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
            errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) HELO/EHLO - $lastmessage", 0);
        }

        while(1) {
            $lastmessage = fgets($fp, 512);
            if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
                 break;
             }
        } 

        if($this->mMailCfg['auth']) {
            fputs($fp, "AUTH LOGIN ");
            $lastmessage = fgets($fp, 512);
            
            if(substr($lastmessage, 0, 3) != 334) {
                errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) AUTH LOGIN - $lastmessage", 0);
        }

        fputs($fp, base64_encode($this->mMailCfg['auth_username'])." ");
        $lastmessage = fgets($fp, 512);
        
        if(substr($lastmessage, 0, 3) != 334) {
            errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) USERNAME - $lastmessage", 0);
        }

        fputs($fp, base64_encode($this->mMailCfg['auth_password'])." ");
        $lastmessage = fgets($fp, 512);
        
        if(substr($lastmessage, 0, 3) != 235) {
            errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) PASSWORD - $lastmessage", 0);
        }

        $email_from = $this->mMailCfg['from'];
    }

    fputs($fp, "MAIL FROM: ".preg_replace("/.*.*/", "\1", $this->getEmails('email_from'))." ");
    $lastmessage = fgets($fp, 512);
    
    if(substr($lastmessage, 0, 3) != 250) {
        fputs($fp, "MAIL FROM: getEmails('email_from'))."> ");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 250) {
            errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) MAIL FROM - $lastmessage", 0);
        }
    }

    foreach(explode(',', $this->getEmails('email_to')) as $to_user) {
        $to_user = trim( $to_user );
        if( $to_user ) {
            fputs( $fp, "RCPT TO: $to_user " );
            $lastmessage = fgets( $fp, 512 );
            if(substr($lastmessage, 0, 3) != 250) {
                fputs($fp, "RCPT TO:  ");
                $lastmessage = fgets($fp, 512);
                errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) RCPT TO - $lastmessage", 0);
            }
        }
    }

    fputs($fp, "DATA ");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 354) {
        errorlog('SMTP', "($this->mMailCfg[server]:$this->mMailCfg[port]) DATA - $lastmessage", 0);
    }

     fputs($fp, "To: ".$this->getEmails('email_to')." From: ".$this->getEmails('email_from')." Subject: ".str_replace(" ", ' ', $this->getEmails('email_subject'))." ".$this->getEmails('email_message')." . "); 
    fputs($fp, "QUIT ");
    }
    
    function getEmailCfg( $elements ){
        if(isset( $elements ) || !empty( $elements ))
            return $this->mMailCfg[$elements];
        else 
            return $this->mMailCfg;
    }
    
    function __destruct(){
        unset($fp);
        unset($this);
    }
}


/*@调用演示.请设置下面信息@*/
#######################################################################

$emails = array(
'email_to' => 'injection.mail@gmail.com',                        #收件人地址
'email_subject' => 'test',                                #邮件标题
'email_message' => 'ksadasdasdasdsadasfafdasfdsaff',    #邮件正文
'email_from' => '',            #邮件来源
);


    
##########################################################################    
   
$mailcfg['server'] = 'smtp.gmail.com';            #smtp服务器地址    
    $mailcfg['port'] = '25';                            #smtp端口
    $mailcfg['auth'] = 1;                                #显示作者
    $mailcfg['from'] = 'gmail';    #发件人地址
    $mailcfg['auth_username'] = 'injection_at@hotmail.com';    #发件人邮箱
    $mailcfg['auth_password'] = '12345677';                #发件人邮箱密码
#调用:
$sm = new sendMail( $mailcfg );
$sm->setEmailInfo( $emails );
$sm->sendMails();