编写一个用php socket 发送邮件的类,简单好用,当用到php程序发送邮件时,

而在163服务器中,可以在RCPT命令中还可以验证163邮箱是否存在,还有很多用处,

我现在暂时还没想到。

记录下,有需要的朋友可以参考下

<?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!';
    }

}

$conf = array(
    'host'     => 'smtp.163.com',
    'port'     => '25',
    'user'     => 'z1298703836@163.com',
    'password' => 'zeiwei123',
     );

$rcpt = array(
    'z1298703836@sina.com',
    'wei.zen@baisonmail.com'
    );

$header = array(
    'to_name'   => 'willstang',
    'from_name' => 'sinawill',
    'subject'   => 'tst of reo ',
    );

$body = '<h1 style="color:red;">sfsfsd</h1>';

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

$mail->send_mail();

echo '<pre>';
print_r($mail->msg);

熟悉邮件发送的过程,它的stmp协议,

还有一些发送的细节,如:发送的内容分为两部分, 一部分在头部,另一部分是email的正文,

php 邮件类的更多相关文章

  1. PHPMailer邮件类使用错误分析

    PHPMailer配置清单如下: require_once ‘class.phpmailer.php‘; $receiver = ”; $mail =  new PHPMailer ( ); $mai ...

  2. Android调用系统邮件类应用的正确实现方法

    Android应用开发中,很多情况下免不了要调用手机上的邮件类应用,实现邮件发送的功能,这一般是通过调用系统已有的Intent来实现的.看到网上很多邮件发送都是调用action为android.con ...

  3. c++封装的发邮件类CSendMail

    项目需要做发邮件的功能,在网上找了一下代码,比较出名的SMailer编译不过(把那个Base64的encode拉到MailSender中实现就能过,但我搞不懂原来出错的原因,就不想用),另外找到了一个 ...

  4. phpmailer邮件类

    <?php/** * 邮件类 * Enter description here ... * @author df * Mail::getMail()->sendMail(); * */cl ...

  5. ASP.NET Boilerplate 邮件类使用

    在系统我们自定一个 MySettingProvider,并添加到配置集合中,定义一些邮件参数覆盖默认参数,然后通过IOC容器得到SmtpEmailSender实例,调用send方法就实现了,实现代码如 ...

  6. php使用PHPMailer邮件类发送邮件

    PHPMailer是一个用于发送电子邮件的PHP函数包.它提供的功能包括:*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址*.支持多种邮件编码包括:8bit,base64,binary和qu ...

  7. tp3.2 新增邮件类

    1.新建方法   调用发送邮件,我的目录在/admin下 2.新增邮件方法 类的发送配置功能 文件地址: 网站根目录\项目目录\Admin\Common\ 文件 名   :function.php   ...

  8. C# 发邮件类可发送附件

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...

  9. phpmailer邮件类下载(转)

    PHPMailer是一个用于发送电子邮件的PHP函数包.它提供的功能包括:*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址*.支持多种邮件编码包括:8bit,base64,binary和qu ...

随机推荐

  1. hibernate update部分更新

    hibernate update Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to ...

  2. requirejs学习之-- 初始化(一)

    为了规范在项目中使用的javascript代码,我们使用了requirejs框架. 初始阶段,我们在按钮的点击事件中调用创建的模块,代码如下: function button_click() { _t ...

  3. Python学习(六) Python数据类型:字典(重要)

    字典dict: 字典其实就相当于java里面的Map,用来存储键值对的.其中存储的数据时无序的. 假如有这样的数据: t1=['name','age','sex'] t2=['tom',30,'mal ...

  4. CodeForces 25E Test KMP

    Description Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tes ...

  5. css清除浮动解决方案

    清除浮动包括清除子元素的浮动和清除上级元素的浮动,其中清除上级元素的浮动,只需设置clear为both就可以了,而清除子元素的浮动则可以用空标签法.clearfix方法或overflow方法.因清除上 ...

  6. QWidget可以设置QStyle,它可以绘制很多东西(具体内容没研究,待续)

    QStyle * QWidget::style() const See also QWidget::setStyle(), QApplication::setStyle(), and QApplica ...

  7. ant的入门 配置与安装

    最近需要用ant来生成文件,java类.我才开始了解了这个工具.仔细看了一下,感觉这个小工具的强大功能. 博主也是初学者,在网上收集了资料,尝试了配置:感觉有些高手写得不错变引用之. 配置如下: 以上 ...

  8. Java 舍入模式 数字的格式化

    舍入模式: UP向远离0的方向舍入 始终对非零舍弃部分前面的数字加 1.此舍入模式始终不会减少计算值的绝对值. 例如:1.6 → 2      -1.6 → -2      1.1 → 2      ...

  9. 借贷宝注册送现金疯转 新闻PS图背后真相

    动动手指,20元人民币立即到手:http://www.cnblogs.com/mfryf/p/4754384.html 近日,九鼎投资旗下投资平台借贷宝开展的“拉上好友抢红包,轻轻松松玩出钱”引起市场 ...

  10. Robot Framework 安装AutoItLibrary

    1. 下载AutoItLibrary-1.1_x64包,http://code.google.com/p/robotframework-autoitlibrary/ 2. 安装pywin32库,htt ...