Heim > php教程 > PHP源码 > Hauptteil

php socket 基础型发邮件类

PHP中文网
Freigeben: 2016-05-25 17:06:48
Original
978 Leute haben es durchsucht

php socket 基础型发邮件类 ,   fsocketopen pfsocketopen 一般空间服务商都会禁用。 
此时可自已动手 利用SMTP协议 用socket_create,socket_connect 与邮件服务器进行连接、通信。 
关于smtp 可去 搜索 了解一个 发送邮件 的格式 。 
在写此代码时注意到的一个问题,跟邮件服务发命令时,不要使用 单引号 如 '   要使用双引号 " 
不然邮件服务器 不识别命令。比如命令 "EHLO HELO\r\n",因为每条命令 都要加  \r\n    如果用 单引号包含 \r\n会被当做 字符发出去 而不会 形成  回车符 换行符。 
此类已经 集成到phpx(自己写的PHP轻型框架中)框架中, 

有兴趣的同学 可以拿到本机 测试 ,运行时 会显示 与邮件服务器通信时 邮件服务器返回的状态码与说明 

关于smtp状态码与说明 http://my.oschina.net/apeng/blog/119627 

<?php
final class mail{
    private $from=null,$pass=null,$to=null;
    private $smtp=null,$port=null;
    private $socket=null;
    private $data=null;
     
    public function __construct($array=null){
        $this->smtp=$array[0];
        $this->port=$array[1];
        $this->from=$array[2];
        $this->pass=$array[3];
        $this->to=$array[4];
    }
     
    public function send($header=null,$body=null){
        $this->socket=socket_create(AF_INET,SOCK_STREAM,getprotobyname(&#39;tcp&#39;));
        if(!$this->socket){
            $this->log(&#39;创建socket失败&#39;,true);
        }
         
        if(socket_connect($this->socket,$this->smtp,$this->port)){
            $this->log(&#39;服务器连接应答:&#39;.socket_strerror(socket_last_error()));
        }
        else{
            $this->log(&#39;socket连接失败&#39;,true);
        }
         
        $this->data="EHLO HELO\r\n";
        $this->do_send();
         
        $this->data="AUTH LOGIN\r\n";
        $this->do_send();
         
        $this->data=base64_encode($this->from)."\r\n";
        $this->do_send();
         
        $this->data=base64_encode($this->pass)."\r\n";
        $this->do_send();
         
        $this->data="MAIL FROM:<".$this->from.">\r\n";
        $this->do_send();
         
        $this->data="RCPT TO:<".$this->to.">\r\n";
        $this->do_send();
         
        $this->data="DATA\r\n";
        $this->do_send();
         
        $this->data="From:匿名<".$this->from.">\r\n";
        $this->data.="Subject:".$header."\r\n\r\n";
        $this->data.=$body."\r\n.\r\n";
        $this->do_send();
         
        $this->data="QUIT\r\n";
        $this->do_send();
         
        socket_close($this->socket);
    }
     
    public function do_send(){
        socket_write($this->socket,$this->data,strlen($this->data));
        $this->log(&#39;客户端发送:&#39;.$this->data);
        $this->log(&#39;服务器应答:&#39;.socket_read($this->socket,1024)).&#39;<br>&#39;;
    }
     
    public function log($args=null,$exit=false){
        echo $args.&#39;<br>&#39;;
                if($exit==true){exit;}
    }
}
?>
Nach dem Login kopieren


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!