美文网首页
PHP使用socket发送email!

PHP使用socket发送email!

作者: DragonersLi | 来源:发表于2017-08-06 13:13 被阅读15次
    //封装发送
    function _genHeader($to,$from,$subject){
        $header = "MIME-Version:1.0\r\n";//协议版本
        $header.= "Content-Type:text/html;charset='utf-8'\r\n";//编码类型
        $header.= "Subject:".$subject."\r\n";//邮件主题
        $header.= "From:Dragoners.Li<".$from.">\r\n";//发件人
        $header.= "To:".$to."\r\n";//收件人
        $header.= "Date:".time()."\r\n";//发送时间
        return $header;
    }
    
    $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
     
    $conn = socket_connect($socket,'smtp.163.com','25');
    $msg = '';
    if($conn){
        $msg .= socket_read($socket,1024)."<br>";
                socket_write($socket,"HELO 123  \r\n");
        
        $msg .= socket_read($socket,1024)."<br>";
                socket_write($socket,"AUTH LOGIN ".base64_encode('l1623283744@163.com')."\r\n");
        $msg .= socket_read($socket,1024)."<br>";
                socket_write($socket, base64_encode('Li122400')."\r\n");
        $msg .= socket_read($socket,1024)."<br>";
                socket_write($socket,"MAIL FROM:<l1623283744@163.com> \r\n");
        $msg .= socket_read($socket,1024)."<br>";
                socket_write($socket,"RCPT TO:<865196792@qq.com> \r\n");
        $msg .= socket_read($socket,1024)."<br>";
                socket_write($socket,"DATA\r\n");
        $msg .= socket_read($socket,1024)."<br>";
        $to = "865196792@qq.com";
        $from = "l1623283744@163.com";
        $subject = " hello,world";
        $body = "这是一封测试邮件!test by Dragoners.Li~";
        $data = _genHeader($to,$from,$subject)."\r\n".$body;
                socket_write($socket,$data);
                socket_write($socket,"\r\n.\r\n");//.表示内容结束 
        $msg .= socket_read($socket,1024)."<br>";
                socket_write($socket,"QUIT\r\n");//.表示内容结束 
        $msg .= socket_read($socket,1024)."<br>";
                
    }else{
        $msg = 'socket connect failed!';
    }
    echo $msg;
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:PHP使用socket发送email!

          本文链接:https://www.haomeiwen.com/subject/rgdglxtx.html