首頁  >  文章  >  後端開發  >  使用Socket發送郵件

使用Socket發送郵件

不言
不言原創
2018-04-26 14:40:521993瀏覽

這篇文章是介紹關於使用Socket發送郵件,現在分享給大家,有興趣的朋友可以看一下

之前寫過一篇《使用PHP發送郵件》,方法是利用nette/mail組件發送郵件。

以下內容整理自《PHP核心技術與最佳實務》。
PHP有一個自帶的mail()函數,但要使用SMTP協定發送郵件,需要安裝SMTP伺服器。如果不想安裝,可以使用Socket發送郵件。 SMTP協定建立在TCP協定之上,所以原則上依照SMTP協定的規範,使用Socket跟SMTP伺服器進行互動。

SMTP連線與傳送過程如下:
1)建立TCP連線。
2)客戶端發送HELO指令以標識寄件者自己的身份,客戶端發送MAIL指令。伺服器以OK作為回應,表示準備接收。
3)使用AUTH指令登陸SMTP伺服器,輸入使用者名稱和密碼(注意,使用者名稱和密碼都需要base64加密)。
4)客戶端發送RCPT指令,標識該電子郵件的計畫接收人,可以有多個RCPT行。伺服器以OK作為回應,表示願意為收件者傳送郵件。
5)協商結束後,使用DATA指令發送。
6)以”.”號表示結束,輸入內容一起發送出去,結束此次發送,用QUIT命令退出。
例如,使用Telnet建立SMTP會話,其中S代表伺服器,C代表客戶端,程式碼如下:

C: open smtp.qq.com 25S: 220 esmtp4.qq.com Esmtp QQ Mail ServerC: HELO smtp qq.comS: 250 esmtp4.qq.comC: AUTH loginS: 334 VXNlcm5hbWU6C: 这里输入使用base64加密过的用户名S: 334 UGFzc3dvcmQ6C: 这里输入使用base64加密过的密码
S:235 Authentication successfulC: MAIL FROM:<liexusong@qq.com>S: 250 sender <liexusong@qq.com> OKC: RCPT TO:<liexusong@163.com>S: 250 recipient <liexusong@163.com> OKC: DATAS: 354 Enter mail,end with "." on a line by itselfC: This is example from smtp protocolC:.S: 250 message sentC: QUITS: 221 goodbye

本來想用qq郵件信箱傳送郵件,但是qq信箱開啟SMTP後頁面老是出錯,使用163郵件信箱可以正常傳送郵件,郵件信箱需開啟POP3/SMTP服務。
程式碼:
smtp_mail.php

<?php

class smtp_mail{    private $host;    private $port=25;    private $user;    private $pwd;    private $debug=false;   //是否开启调试模式 默认不开启
    private $sock;          //保存与SMTP服务器连接的句柄
    private $mail_format=0; //邮件格式 0为普通文本 1为HTML邮件

    public function smtp_mail($host,$port,$user,$pwd,$mail_format=0,$debug=false){        $this->host=$host;        $this->port=$port;        $this->user=$user;        $this->pwd=$pwd;        $this->mail_format=$mail_format;        $this->debug=$debug;        //连接SMTP服务器
        /**
         * fsockopen() 初始化一个套接字连接到指定主
         * 最后一个参数为timeout,连接时限
         */
        $this->sock=fsockopen($this->host,$this->port,$errno,$errstr,10);        if (!$this->sock){//连接失败
            exit("Error number: $errno, Error message: $errstr\n");
        }        //取得服务器信息
        $response=fgets($this->sock);        //若包含220,表示已经成功连接到服务器
        if (strstr($response,"220")===false){//首次出现地址|false
            exit("Server error: $response\n");
        }
    }    //将命令发送到服务器,然后取得服务器反馈信息,判断命令是否执行成功
    private function do_command($cmd,$return_code){
        fwrite($this->sock,$cmd);        $response=fgets($this->sock);        if (strstr($response,"$return_code")===false){            $this->show_debug($response);            return false;
        }        return true;
    }    //发送邮件
    public function send_mail($from,$to,$subject,$content){        //判断收发件邮箱地址是否合法
        if (!$this->is_email($from) or !$this->is_email($to)){            $this->show_debug(&#39;Please enter valid from/to email.&#39;);            return false;
        }        //判断主题内容是否为空
        if (empty($subject) or empty($content)){            $this->show_debug(&#39;Please enter subject/content.&#39;);            return false;
        }        //整合邮件信息,发送邮件主体时要以\r\n.\r\n作为结尾
        $detail="From:".$from."\r\n";        $detail.="To:".$to."\r\n";        $detail.="Subject:".$subject."\r\n";        if ($this->mail_format==1){            $detail.="Content-Type: text/html;\r\n";
        }else{            $detail.="Content-Type: text/plain;\r\n";
        }        $detail.="charset=utf-8\r\n\r\n";        $detail.=$content;        //此处应该有判断命令是否执行成功
        $this->do_command("HELO smtp.qq.com\r\n",250);        $this->do_command("AUTH LOGIN\r\n",334);        $this->do_command("$this->user\r\n",334);        $this->do_command("$this->pwd\r\n",235);        $this->do_command("MAIL FROM:<".$from.">\r\n",250);        $this->do_command("RCPT TO:<".$to.">\r\n",250);        $this->do_command("DATA\r\n",354);        $this->do_command($detail."\r\n.\r\n",250);        $this->do_command("QUIT\r\n",221);        return true;
    }    //判断是否为合法邮箱
    private function is_email($emial){        if(filter_var($emial,FILTER_VALIDATE_EMAIL)){            return true;
        }else{            return false;
        }
    }    //显示调试信息
    private function show_debug($message){        if ($this->debug){
            echo "<p>Debug: $message</p><br/>";
        }
    }

}

index.php

<?phpinclude_once "smtp_mail.php";$host="smtp.163.com";$port=25;$user="你的账户名@163.com";$pwd="授权码";$from="你的账户名@163.com";$to="目标邮箱";$subject="Test Smtp Mail";$content="This is example email for you.";$mail=new smtp_mail($host,$port,base64_encode($user),base64_encode($pwd),1,true);$mail->send_mail($from,$to,$subject,$content);

   相關建議:

php實作傳送16進位socket的方法

php實作socket的方法

實例詳解php的socket程式設計


##

以上是使用Socket發送郵件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn