php-socket发送邮箱

<?php
error_reporting(11);
/**
* 
*/
class smtp_mail 
{
    private $host; //保存要链接的SMTP服务器
    private $port=25;  //要绑定的端口,默认为25
    private $user;     //要登录SMTP的服务器的用户和密码 
    private $pass;         
    private $debug=false;  //标识是否开始调试模式,默认为关闭
    private $sock;             //保存与SMTP服务端链接的句柄
    private $mail_format=0; //标志使用什么格式发送邮件 0,为普通文件,1为HTML邮件

    /*
        初始化:
        $param $host 主机名
        $param $port 端口
        $param $user 登录账号
        $param $pass 登录密码
        $param $format 类型格式 0文本 1Html 
        $param $debug 是否开启调试
     */
    public function __construct($host,$port,$user,$pass,$format=1,$debug=0)
    {
        $this->host=$host;
        $this->port=$port;
        $this->user=base64_encode($user);
        $this->pass=base64_encode($pass);
        $this->mail_format=$format;
        $this->debug=$debug;
        $this->sock=fsockopen($this->host,$this->port,&$errno,&$errstr,3);
        if(!$this->sock)
        {
            exit("Error number: $errno,Error message: $errstr\n");
        }
        $response=fgets($this->sock);
        if(strstr($response,"220")===false)
        {
            exit("server error: $response\n");
        }

    }
    /*
        调试类。打印错误信息
     */
    private function show_debug($message)
    {
        if($this->debug)
        {
            echo "<p>Debug: $message\n";
        }
    }
    /*
        沿着通道写信息
     */
    private function do_command($cmd,$return_code)
    {
        fwrite($this->sock,$cmd);
        $response=fgets($this->sock);
        // echo $response."+".$return_code."----------";
        if(strstr($response,"$return_code")===false)
        {
            $this->show_debug($response);
            return false;
        }
        return true;
    }
    /*
        判断是否是邮件
     */
    private function is_email($email)
    {
        $pattren="/^[^_][\w]*@[\w.]+[\w]*[^_]$/";
        if (preg_match($pattren,$email,$matches)) {
            return true;
        }else
        {
            return false;
        }
    }
    /*
        发送邮件
     */
    public function send_mail($from,$to,$subject,$body)
    {

        //检查收|发人邮箱是否正确
        if(!$this->is_email($from)OR !$this->is_email($to))
        {
            $this->show_debug("Please enter vaild from/to email");
            return false;
        }

        //检查
        if (empty($subject) OR empty($body)) 
        {
            $this->show_debug("Please enter subject/content");
            return false;
        }

        $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.=$body;

        $this->do_command("HELO ".$this->host."\r\n",250);
        $this->do_command("AUTH LOGIN\r\n",334);
        $this->do_command($this->user."\r\n",334);
        $this->do_command($this->pass."\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;

    }
}

$host="smtp.qq.com";
$port=25;
$user="648462920@qq.com";
$pass="648462920jin";
$from="648462920@qq.com";
$to="295517247@qq.com";
$subject="HELO body ----php socket";
$content="大神有没有收到我的邮件";
$mail=new smtp_mail($host,$port,$user,$pass);
$mail->send_mail($from,$to,$subject,$content);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值