网站地图    收藏   

主页 > 后端 > PHP语言 >

自己写的一个邮件发送类,可加附件

来源:未知    时间:2014-12-02 09:38 作者:xxadmin 阅读:次

[导读] 自己写的一个邮件发送类,可加附件 这个类集成在我写的一个框架里,但是为了松耦合性所有的LIBRARY可以独立拿来立刻使用 里面的邮件发送类拿来分享给大家 最后还要说一下 暂时还没支...

自己写的一个邮件发送类,可加附件
这个类集成在我写的一个框架里,但是为了松耦合性所有的LIBRARY可以独立拿来立刻使用
里面的邮件发送类拿来分享给大家

最后还要说一下 暂时还没支持SSL 目前够用了  以后要用再说...
分享下代码

  1. <?php
  2. // +------------------------------------------------
  3. // | Travel - 1.1
  4. // +------------------------------------------------
  5. // | Copyright (c) 2014 All rights reserved.        
  6. // +------------------------------------------------
  7. // | Author: XiaoZhi <491434308@qq.com>     
  8. // +------------------------------------------------
  9.  
  10. // +-----------------------
  11. // | 电子邮件类
  12. // +-----------------------
  13.  
  14. /**
  15. * 使用方法:
  16. *         $test = $this->email->envelope($rc)     //填写信封,三个参数,第一个收件人必选,必须数组形式支持多个 2,3可选 数组形式支持多个  2-抄送       3-密送
  1. // +------------------------------------------------
  2. // | Travel - 1.0 Beat                              
  3. // +------------------------------------------------
  4. // | Copyright (c) 2014 All rights reserved.        
  5. // +------------------------------------------------
  6. // | Author: XiaoZhi <travelphp@163.com>     
  7. // +------------------------------------------------
  8.  
  9. // +-----------------------
  10. // | 电子邮件类
  11. // +-----------------------
  12.  
  13. /**
  14. * 使用方法:
  15. * 
  16. */
  17.  
  18. class Email{
  19.    
  20.     /*发送配置信息*/
  21.     public $smtp;           //SMTP地址
  22.     public $port;           //SMTP端口
  23.     public $username;       //发送者邮箱账号    
  24.     public $password;       //发送者邮箱密码
  25.     public $sender;         //发送者邮箱
  26.     public $sendname;       //发送者名称
  27.     public $charset;
  28.     /*接收配置信息*/
  29.     public $rc;             //邮件接收者 数组
  30.     public $cc;             //抄送 数组
  31.     public $bcc;            //密送 数组
  32.     public $subject;        //标题
  33.     public $content;        //内容
  34.     public $append;         //附件
  35.     public $content_mime;   //内容类型
  36.     
  37.     
  38.     /*相关属性*/
  39.     private $rcpt;          //接收者并集,用于设置RCPT
  40.     private $handle;        //socket连接句柄
  41.     private $request;       //请求命令行
  42.     private $errno;         //错误号
  43.     private $errstr;        //错误描述
  44.     
  45.     
  46.     /**
  47.      * 初始化SMTP服务器信息
  48.      * @param array $opts SMTP服务器配置,格式如下:
  49.      * Array(
  50.      *     [smtp]     =>  'smtp服务器地址',
  51.      *     [port]     =>  '端口号码',   
  52.      *     [username] =>  '发送者smtp登陆账号',
  53.      *     [password] =>  '登陆密码',
  54.      *     [sender]   =>  '发送者邮箱',
  55.      *     [sendname] =>  '发送者名称'
  56.      * )
  57.      */
  58.     public function __construct($opts) {
  59.         if(!is_array($opts)){
  60.             trigger_error('Email:初始化参数只接受数组形式',E_USER_ERROR);
  61.         }
  62.         if(empty($opts['smtp'])){
  63.             trigger_error('Email:SMTP服务器地址不能为空',E_USER_ERROR);
  64.         }
  65.         $this->smtp = $opts['smtp'];
  66.         if($this->is_email($opts['sender']) !== true){
  67.             trigger_error('Email:发送者邮箱不合法',E_USER_ERROR);
  68.         }
  69.         $this->sender = $opts['sender'];
  70.         $this->port = empty($opts['port']) ? 25 : $opts['port'];
  71.         $this->username = empty($opts['username']) ? null : $opts['username'];
  72.         $this->password = empty($opts['password']) ? null : $opts['password'];
  73.         $this->sendname = empty($opts['sendname']) ? '' : $opts['sendname'];
  74.         $this->socket_timeout = empty($opts['socket_timeout']) ? 3 : $opts['socket_timeout'];
  75.         $this->charset = empty($opts['charset']) ? 'UTF-8' : $opts['charset'];
  76.         $this->rc = array();
  77.         $this->cc = array();
  78.         $this->bcc = array();
  79.         $this->append = array();
  80.     }
  81.     
  82.     
  83.     /**
  84.      * 创建一个邮件信封
  85.      * @param array $rc  收件人地址列表
  86.      * @param array $cc  抄送地址列表
  87.      * @param array $bcc 密送地址列表
  88.      * @return object
  89.      */
  90.     public function envelope($rc,$cc=array(),$bcc=array()){
  91.         if(!is_array($rc) || empty($rc)){
  92.             trigger_error('收信人地址需要一个必须的数组参数,至少有一个正确的收信地址',E_USER_WARNING);
  93.         }else{
  94.             foreach($rc as $row){
  95.                 if($this->is_email($row)===true){
  96.                     array_push($this->rc,$row);
  97.                 }
  98.             }
  99.             $this->rc = array_unique($this->rc);
  100.         }
  101.         
  102.         if(!is_array($cc)){
  103.             trigger_error('抄送地址需要一个数组参数',E_USER_WARNING);
  104.         }else{
  105.             foreach($cc as $row){
  106.                 if($this->is_email($row)===true){
  107.                     array_push($this->cc,$row);
  108.                 }
  109.             }
  110.             $this->cc = array_unique($this->cc);
  111.         }
  112.         
  113.         if(!is_array($bcc)){
  114.             trigger_error('密送地址需要一个数组参数',E_USER_WARNING);
  115.         }else{
  116.             foreach ($bcc as $row){
  117.                 if($this->is_email($row)===true){

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论