编写一个用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. mysql 复习与学习(二)数据库及表结构的创建删除

    mysql -h localhost -uroot -p123456 //连接数据库 show databases; //查看数据库 create database if not exists db_ ...

  2. php中12个魔术方法

    本文列举了php面向对象当中12个魔术方法,并对此进行一一详细介绍,希望对新手有所帮助. 1.构造方法: __construct() 参数:自定义 触发时机:new的一瞬间自动调用 作用:初始化成员属 ...

  3. 异常处理与调试4 - 零基础入门学习Delphi53

    调试(Debug) 让编程改变世界 Change the world by program 调试(Debug) 在应用程序开发中检测.处理程序中的错误是一个非常重要的环节.在Delphi的集成开发环境 ...

  4. ASP.NET Ajax

    代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- ...

  5. UESTC_邱老师看电影 2015 UESTC Training for Dynamic Programming<Problem F>

    F - 邱老师看电影 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  6. UVA10487(二分)

    Given is a set of integers and then a sequence of queries. A query gives you a number and asks to fin ...

  7. java Socket使用注意

    Socket s = new Socket(ia, port); BufferedOutputStream bufOut = new BufferedOutputStream(s.getOutputS ...

  8. java编译相关问题总结

    参考:http://jingyan.baidu.com/article/5bbb5a1b080f6113eba179f0.html 1.在linux下生成的class文件/jar包,拿到windows ...

  9. 程序猿必备的10款web前端开发插件一

    1.CSS3实现的火柴燃烧Loading加载动画 这次我们要给大家分享一款非常特别的CSS3 Loading加载动画,整个Loading加载动画就好像是火柴在燃烧一样,不足的是火苗并没有那么真实,比较 ...

  10. base_local_planner vs. dwa_planner

    http://answers.ros.org/question/10718/dwa_planner-vs-base_local_planner/ The dwa_local_planner suppo ...