首页 >php教程 >PHP源码 >一个邮件发送类..

一个邮件发送类..

PHP中文网
PHP中文网原创
2016-05-25 17:12:161248浏览

一个邮件发送类..

class emailui
{
 static function runlog($mode = 'SMTP',$b = '',$c = '',$d='') { 
          
    }
 static function sendmail($toemail, $subject, $message, $from='',$cfg = array(), $sitename='') {
    global $db_wwwname,$mail_port,$mail_id,$mail_server,$mail_pw,$mail_type,$db_charset,$version,$smtpfrom;
         
    $sitename = $sitename ? $sitename : $db_wwwname;
    $adminemail = $mail_id ? $mail_id : 'admin@54ui.com';
     
    if($cfg && is_array($cfg)) {
        $adminemail = $cfg['from'];
        $mail_type = $cfg['mail_type'];  //邮件发送模式
        $mail = $cfg;       
    } else {
        $adminemail = $mail_id;   
        $mail_type  = $mail_type; //邮件发送模式
        #端口,邮件头的分隔符,收件人地址中包含用户名
        $mail= Array (
            'mailsend' => 2,
            'maildelimiter' => 1,
            'mailusername' => 1,
            'server' => $mail_server,       #服务器
            'port' => $mail_port,           #端口
            'auth' => $mail_id,             #作者
            'from' => $mail_id,             #发信者
            'auth_username' => $mail_id,    #账号
            'auth_password' => $mail_pw     #密码
        );      
    }
    //mail 发送模式
    if(!$mail_type) {
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset='.$db_charset.'' . "\r\n";
        $headers .= &#39;From: &#39;.$sitename.&#39; <&#39;.$from.&#39;>&#39; . "\r\n";
        mail($toemail, $subject, $message, $headers);
        return true;
    }
    //邮件头的分隔符
    $maildelimiter = $mail[&#39;maildelimiter&#39;] == 1 ? "\r\n" : ($mail[&#39;maildelimiter&#39;] == 2 ? "\r" : "\n");
    //收件人地址中包含用户名
    $mailusername = isset($mail[&#39;mailusername&#39;]) ? $mail[&#39;mailusername&#39;] : 1;
    //端口
    $mail[&#39;port&#39;] = $mail[&#39;port&#39;] ? $mail[&#39;port&#39;] : 25;
    $mail[&#39;mailsend&#39;] = $mail[&#39;mailsend&#39;] ? $mail[&#39;mailsend&#39;] : 1;  
    //发信者
    $email_from = $from == &#39;&#39; ? &#39;=?&#39;.$db_charset.&#39;?B?&#39;.base64_encode($sitename)."?= <".$adminemail.">" :
     (preg_match(&#39;/^(.+?) \<(.+?)\>$/&#39;,$from, $mats) ? &#39;=?&#39;.$db_charset.&#39;?B?&#39;.base64_encode($mats[1])."?= 
     <$mats[2]>" : $from);
    //收信人
    $email_to = preg_match(&#39;/^(.+?) \<(.+?)\>$/&#39;,$toemail, $mats) ? ($mailusername ? &#39;=?&#39;.$db_charset.&#39;?B?&#39;.base64_encode($mats[1])."?= <$mats[2]>" : $mats[2]) : $toemail;;
    //邮件标题
    $email_subject = &#39;=?&#39;.$db_charset.&#39;?B?&#39;.base64_encode(preg_replace("/[\r|\n]/", &#39;&#39;,
     $subject.&#39;--&#39;.$db_wwwname)).&#39;?=&#39;;
    //邮件内容
    $message = $message."<span style=display:none>Published at  ".gmdate("Y-m-d h:i:s",mktime()+8*3600)." ,
     Powered By uicms ".$version." (service.54ui.com)</span>";
    $email_message = chunk_split(base64_encode(str_replace("\n", "\r\n", str_replace("\r", "\n", 
    str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message))))));
     
    $headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: abaoei cms 
    {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html; charset=".$db_charset."
    {$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
             
    if(!$fp = fsockopen($mail[&#39;server&#39;], $mail[&#39;port&#39;], $errno, $errstr, 30)) {
        self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) CONNECT - Unable to connect to the SMTP server", 0);
        return false;
    }
    stream_set_blocking($fp, true);
 
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != &#39;220&#39;) {
        self::runlog(&#39;SMTP&#39;, "$mail[server]:$mail[port] CONNECT - $lastmessage", 0);
        return false;
    }
 
    fputs($fp, ($mail[&#39;auth&#39;] ? &#39;EHLO&#39; : &#39;HELO&#39;)." uchome\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
        self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) HELO/EHLO - $lastmessage", 0);
        return false;
    }
 
    while(1) {
        if(substr($lastmessage, 3, 1) != &#39;-&#39; || empty($lastmessage)) {
            break;
        }
        $lastmessage = fgets($fp, 512);
    }
 
    if($mail[&#39;auth&#39;]) {
        fputs($fp, "AUTH LOGIN\r\n");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334) {
            self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) AUTH LOGIN - $lastmessage", 0);
            return false;
        }
 
        fputs($fp, base64_encode($mail[&#39;auth_username&#39;])."\r\n");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334) {
            self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) USERNAME - $lastmessage", 0);
            return false;
        }
 
        fputs($fp, base64_encode($mail[&#39;auth_password&#39;])."\r\n");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 235) {
            self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) PASSWORD - $lastmessage", 0);
            return false;
        }
 
        $email_from = $mail[&#39;from&#39;];
    }
 
    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) {
            self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) MAIL FROM - $lastmessage", 0);
            return false;
        }
    }
 
    fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $toemail).">\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250) {
        fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $toemail).">\r\n");
        $lastmessage = fgets($fp, 512);
        self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) RCPT TO - $lastmessage", 0);
        return false;
    }
 
    fputs($fp, "DATA\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 354) {
        self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) DATA - $lastmessage", 0);
        return false;
    }
 
    $headers .= &#39;Message-ID: <&#39;.gmdate(&#39;YmdHs&#39;).&#39;.&#39;.substr(md5($email_message.microtime()), 0, 6).
    rand(100000, 999999).&#39;@&#39;.$_SERVER[&#39;HTTP_HOST&#39;].">{$maildelimiter}";
 
    fputs($fp, "Date: ".gmdate(&#39;r&#39;)."\r\n");
    fputs($fp, "To: ".$email_to."\r\n");
    fputs($fp, "Subject: ".$email_subject."\r\n");
    fputs($fp, $headers."\r\n");
    fputs($fp, "\r\n\r\n");
    fputs($fp, "$email_message\r\n.\r\n");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250) {
        self::runlog(&#39;SMTP&#39;, "($mail[server]:$mail[port]) END - $lastmessage", 0);
    }
    fputs($fp, "QUIT\r\n");
    return true;
  }
}
//emailui::sendmail(&#39;admin@54ui.com&#39;, &#39;你好哦&#39;, &#39;非常感谢&#39;);

                   

 以上就是一个邮件发送类..的内容,更多相关内容请关注PHP中文网(www.php.cn)!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn