一个简单的邮件发送

先发一个php发邮件类的源码。

<?php
class mail {

    /**
    * 这些即可发送email
    */
    public $host;
    public $port;
    public $user;
    public $password;
    public $mail_form;
    public $rcpt;

    public $body;

    /**
    * 附属在 header 中信息
    */
    public $to_name;
    public $from_name;
    public $subject;
    public $html;

    public $connection;

    public $msg = array();


    // 进行参数初始化
    public function __construct($conf, $rcpt, $header, $body, $html = true) {

        try {

            $this->host = $conf['host'];
            $this->port = $conf['port'];
            $this->user = $conf['user'];
            $this->password = $conf['password'];

            $this->rcpt = $rcpt;

            $this->to_name   = $header['to_name'];
            $this->from_name = $header['from_name'];
            $this->subject   = $header['subject'];
            $this->html      = $html;

            $this->body = base64_encode($body);

        } catch (Exception $e) {
            throw new Exception($e->getMessage());            
        }

    }



    public function connect() {
        $this->connection = @fsockopen($this->host, '25', $errno, $errstr, 10);
        if (!$this->connection) {            
            throw new Exception('connect failed!');
        }
        $greet = $this->callback_code();

    }

    public function helo() {
        if($this->is_connect() &&
            $this->send_data('HELO ' . $this->host) &&
            $this->callback_code() == 250
            ) {
             return true;
        } else {
            throw new Exception($this->get_callback_msg_lastest());

        }
    }

    public function send_data($cmd) {
        if (is_resource($this->connection)) {
            return @fwrite($this->connection, $cmd."\r\n", strlen($cmd) + 2);
        }
    }

    public function auth(){
        if ($this->is_connect() &&
            $this->send_data('AUTH LOGIN') && $this->callback_code() == 334 &&
            $this->send_data(base64_encode($this->user)) && $this->callback_code() == 334 &&
            $this->send_data(base64_encode($this->password)) && $this->callback_code() == 235 ) {    
            return true;
        } else {
            throw new Exception($this->get_callback_msg_lastest());            
        }
    }

   public function Mail() {

        if ($this->is_connect() &&
            $this->send_data("MAIL FROM:<$this->user>") &&
            $this->callback_code() == 250
            ) {
            return true;
        } else  {
           throw new Exception($this->get_callback_msg_lastest());            
        }
   }

    public function is_connect() {
        return is_resource($this->connection);
    }

    public function callback_code() {
        if ($this->is_connect()) {
            $msg = fgets($this->connection);
            if (!empty($msg)) {
                $code = substr($msg, 0, strpos($msg, ' '));
            } else {
                return '';
            }
            $this->msg[] = $msg;
            return $code;
        }
    }

    public function get_callback_msg_lastest() {
        return end($this->msg);
    }

    public function rcpt($rcpt) {
        if ($this->is_connect() &&
            $this->send_data("RCPT TO:<$rcpt>") &&
            $this->callback_code() == 250
            ) {
            return true;
        } else  {
            throw new Exception($this->get_callback_msg_lastest());            
        }
    }

    public function data() {
        if ($this->is_connect() &&
            $this->send_data('DATA') &&
            $this->callback_code() == 354) {
            return true;
        } else {
            throw new Exception($this->get_callback_msg_lastest());
        }
    }


    public function send_mail() {

        try {

            $this->connect();
            $this->helo();
            $this->auth();
            $this->Mail();

            if (is_array($this->rcpt)) {
                foreach ($this->rcpt as $rcpt) {
                    $this->rcpt($rcpt);
                }

            } else {
                $this->rcpt($this->rcpt);
            }

            $this->data();

            $header = str_replace("\r\n" . '.', "\r\n" . '..', trim(implode("\r\n", $this->get_header())));
            $body   = str_replace("\r\n" . '.', "\r\n" . '..', $this->body);
            $body   = substr($body, 0, 1) == '.' ? '.' . $body : $body;

            $this->send_data($header);
            $this->send_data('');
            $this->send_data($body);            
            $this->send_data('.');

            if ($this->callback_code()  != 250) {
                throw new Exception('send mail falied!');                
            }
            return true;
        } catch (Exception $e) {
            throw new Exception($e->getMessage());            
        }


    }



    //只能检测同一邮件服务器上email address
    public  function is_email_exist() {




    }


    public function get_header() {

        $header = array();
        $content_type = $this->html ? 'text/html;' : 'text/plain;' ;

        $headers [] = 'Date: ' . gmdate('D, j M Y H:i:s') . ' +0000';
        $to_mail = is_array($this->rcpt) ? implode(',', $this->rcpt) : $this->rcpt;
        $headers [] = 'To: "' . '=? utf8?B?' . base64_encode($this->to_name) . '?=' . '" <' . $to_mail . '>';
        $headers [] = 'From: "' . '=?utf8?B?' . base64_encode($this->from_name) . '?=' . '" <' . $this->user . '>';
        $headers [] = 'Subject: ' . '=?utf8?B?' . base64_encode($this->subject) . '?=';
        $headers [] = 'Content-Type:'.$content_type . ' charset=utf8; format=flowed';
        $headers [] = 'Content-Transfer-Encoding: base64';
        $headers [] = 'Content-Disposition: inline';

        return $headers;
    }



}


/**
* 用函数封装类
*/
function send_mail($conf, $rcpt, $header, $body) {

    $mail = new mail($conf, $rcpt, $header, $body);

    if ($mail->send_mail()) {
        echo 'send email successfully!';
    } else {
        echo 'falied!';
    }


}

先创建一个php文件,把邮件类的代码放进去。例如:email.php

这个类相对于别的邮件类,虽然不太全面,但是基本的一些功能全都具备。

然后再我们要用的地方调用邮件类

 include 'email.php';

下面是我们发邮件时要设置的参数:

$conf = array(
    'host'     => '你的SMTP',
    'port'     => 'SMTP端口',
    'user'     => '你的163邮箱',
    'password' => '授权码',
     );

$rcpt = array(
    '接收人邮箱',
    );

$header = array(
    'to_name'   => '邮件名称',
    'from_name' => '发件人姓名',
    'subject'   => '邮件标题',
    );

$body = '邮件内容';

好了参数设置完,我们要进行最后的几步。

实例化邮件类:

$mail = new mail($conf, $rcpt, $header, $body);

执行发送

$mail->send_mail();

好了,一个简单的邮件发送就完成了。
还有很多不足,请前辈们多多指教。
仅供参考

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值