订阅博客
收藏博客
微博分享
QQ空间分享

php用smtp发送邮件

频道:网站技术 标签: 时间:2019年08月24日 浏览:293次 评论:0条
demo.php
date_default_timezone_set('Asia/Shanghai');  // 'Asia/Chongqing' or 'PRC'
require_once ('mail.class.php'); 
 
$smtpserver = "smtp.163.com";//SMTP服务器 
$smtpserverport =25;//SMTP服务器端口 
$smtpusermail = "test@163.com";//SMTP服务器的用户邮箱 
$smtpemailto = "test@qq.com";//发送给谁 
$smtpuser = "test@163.com";//SMTP服务器的用户帐号 
$smtppass = "******";//SMTP服务器的用户密码,此例为163邮箱的第三方授权登录密码 
$mailsubject = "PHP100测试邮件系统";//邮件主题 
$mailbody = "测试邮箱发送信息";//邮件内容 
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 
 
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 
$smtp->debug = false;//是否显示发送的调试信息
$res=$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 
var_dump($res);//boolean (true or false)

mail.class.php

class smtp 
{ 
        /* Public Variables */ 
        public $smtp_port; 
        public $time_out; 
        public $host_name; 
        public $log_file; 
        public $relay_host; 
        public $debug; 
        public $auth; 
        public $user; 
        public $pass; 
         
        /* Private Variables */ 
        private $sock; 
     
        /* Constractor */ 
		// function __construct($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) //与下作用相同
        function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
        { 
            $this->debug = FALSE; 
            $this->smtp_port = $smtp_port; 
            $this->relay_host = $relay_host; 
            $this->time_out = 30; //is used in fsockopen() 
            # 
            $this->auth = $auth;//auth 
            $this->user = $user; 
            $this->pass = $pass; 
            # 
            $this->host_name = "localhost"; //is used in HELO command 
            $this->log_file =""; 
             
            $this->sock = FALSE; 
        } 
     
        /* Main Function */ 
        function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 
        { 
			$header = '';
            $mail_from = $this->get_address($this->strip_comment($from)); 
            $body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body); 
            $header .= "MIME-Version:1.0\r\n"; 
            if($mailtype=="HTML"){ 
                $header .= "Content-Type:text/html\r\n"; 
            } 
                $header .= "To: ".$to."\r\n"; 
            if ($cc != "") { 
                $header .= "Cc: ".$cc."\r\n"; 
            } 
            $header .= "From: $from<".$from.">\r\n"; 
            $header .= "Subject: ".$subject."\r\n"; 
            $header .= $additional_headers; 
            $header .= "Date: ".date("r")."\r\n"; 
            $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 
            list($msec, $sec) = explode(" ", microtime()); 
            $header .= "Message-ID: <".date("ymdhis", to="explode(","," this-="">strip_comment($to)); 
         
            if ($cc != "") { 
            $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 
            } 
             
            if ($bcc != "") { 
            $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 
            } 
         
            $sent = TRUE; 
            foreach ($TO as $rcpt_to) { 
                $rcpt_to = $this->get_address($rcpt_to); 
                if (!$this->smtp_sockopen($rcpt_to)) { 
                    $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 
                    $sent = FALSE; 
                    continue; 
                } 
                if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
                    $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 
                } else { 
                    $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 
                    $sent = FALSE; 
                } 
                fclose($this->sock); 
                $this->log_write("Disconnected from remote host\n"); 
            } 
            echo "
"; 
            echo $header; //
            return $sent; 
        } 
     
        /* Private Functions */ 
         
        function smtp_send($helo, $from, $to, $header, $body = "") 
        { 
            if (!$this->smtp_putcmd("HELO", $helo)) { 
                return $this->smtp_error("sending HELO command"); 
            } 
			
            #auth 
            if($this->auth){ 
                if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 
                    return $this->smtp_error("sending HELO command"); 
                } 
                if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
                    return $this->smtp_error("sending HELO command"); 
                } 
            } 
            # 
            if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 
                return $this->smtp_error("sending MAIL FROM command"); 
            } 
         
            if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 
                return $this->smtp_error("sending RCPT TO command"); 
            } 
         
            if (!$this->smtp_putcmd("DATA")) { 
                return $this->smtp_error("sending DATA command"); 
            } 
             
            if (!$this->smtp_message($header, $body)) { 
                return $this->smtp_error("sending message"); 
            } 
    
            if (!$this->smtp_eom()) { 
                return $this->smtp_error("sending

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。