- <?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('tcp'));
- if(!$this->socket){
- exit('创建socket失败');
- }
- if(socket_connect($this->socket,$this->smtp,$this->port)){
- $this->debug('服务器连接应答:'.socket_strerror(socket_last_error()));
- }
- else{
- exit('socket连接失败');
- }
- $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->debug('客户端发送:'.$this->data);
- $this->debug('服务器应答:'.socket_read($this->socket,1024)).'<br>';
- }
- public function debug($args=null){
- echo $args.'<br>';
- }
- }
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/241020136663.html
来源: http://www.codesnippet.cn/detail/241020136663.html