php实现的发送带附件邮件类实例


Posted in PHP onSeptember 22, 2014

本文实例讲述了php实现的发送带附件邮件类的方法,是一个非常实用的功能。分享给大家供大家参考。具体方法如下:

emailclass.php类文件如下:

<? 
class CMailFile {  
 
  var $subject;  
  var $addr_to;  
  var $text_body;  
  var $text_encoded;  
  var $mime_headers;  
  var $mime_boundary = "--==================_846811060==_";  
  var $smtp_headers;  
   
  function CMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype = "application/octet-stream",$mime_filename = false) {  
    $this->subject = $subject;     
    $this->addr_to = $to;     
    $this->smtp_headers = $this->write_smtpheaders($from); 
    $this->text_body = $this->write_body($msg); 
    $this->text_encoded = $this->attach_file($filename,$downfilename,$mimetype,$mime_filename); 
    $this->mime_headers = $this->write_mimeheaders($filename, $mime_filename); 
  }  
 
  function attach_file($filename,$downfilename,$mimetype,$mime_filename) { 
    $encoded = $this->encode_file($filename); 
    if ($mime_filename) $filename = $mime_filename; 
    $out = "--" . $this->mime_boundary . "\n"; 
    $out = $out . "Content-type: " . $mimetype . "; name=\"$filename\";\n"; 
    $out = $out . "Content-Transfer-Encoding: base64\n"; 
    $out = $out . "Content-disposition: attachment; filename=\"$downfilename\"\n\n"; 
    $out = $out . $encoded . "\n"; 
    $out = $out . "--" . $this->mime_boundary . "--" . "\n"; 
    return $out; 
  }  
 
  function encode_file($sourcefile) {  
    if (is_readable($sourcefile)) {  
      $fd = fopen($sourcefile, "r");  
      $contents = fread($fd, filesize($sourcefile));  
      $encoded = chunk_split(base64_encode($contents));  
      fclose($fd);  
    }  
    return $encoded;  
  }  
 
  function sendfile() {   
    $headers = $this->smtp_headers . $this->mime_headers;  
    $message = $this->text_body . $this->text_encoded;  
    mail($this->addr_to,$this->subject,$message,$headers);  
  }  
 
  function write_body($msgtext) {  
    $out = "--" . $this->mime_boundary . "\n";  
    $out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n";  
    $out = $out . $msgtext . "\n";  
    return $out;  
  }  
 
  function write_mimeheaders($filename, $mime_filename) {  
    if ($mime_filename) $filename = $mime_filename;  
    $out = "MIME-version: 1.0\n";  
    $out = $out . "Content-type: multipart/mixed; ";  
    $out = $out . "boundary=\"$this->mime_boundary\"\n";  
    $out = $out . "Content-transfer-encoding: 7BIT\n";  
    $out = $out . "X-attachments: $filename;\n\n";  
    return $out;  
  }  
 
  function write_smtpheaders($addr_from) {  
    $out = "From: $addr_from\n";  
    $out = $out . "Reply-To: $addr_from\n";  
    $out = $out . "X-Mailer: PHP3\n";  
    $out = $out . "X-Sender: $addr_from\n";  
    return $out;  
  }  
}  
 
/*用法 - 例如:mimetype 为 "image/gif" 
  $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype); 
  $mailfile->sendfile(); 
 
  $subject -- 主题 
  $sendto -- 收信人地址 
  $replyto -- 回复地址 
  $message -- 信件内容 
  $filename -- 附件文件名 
  $downfilename -- 下?的文件名 
  $mimetype -- mime类型 
*/ 
?>

Demo示例文件如下:

<?php 
  require_once('emailclass.php'); 
 
  //发送邮件 
   
  //主? 
  $subject = "test send email"; 
 
  //收件人 
  $sendto = 'abc@163.com'; 
   
  //?件人 
  $replyto = 'cdf@163.com'; 
   
  //?热 
  $message = "test send email content"; 
   
  //附件 
  $filename = 'test.jpg'; 
   
  //附件??e 
  $mimetype = "image/jpeg"; 
 
  $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype);  
  $mailfile->sendfile(); 
?>

相信本文所述对大家php程序设计的学习有一定的借鉴价值。

PHP 相关文章推荐
批量修改RAR文件注释的php代码
Nov 20 PHP
PHP中date()日期函数有关参数整理
Jul 19 PHP
用PHP即时捕捉PHP中的错误并发送email通知的实现代码
Jan 19 PHP
PHP批量采集下载美女图片的实现代码
Jun 03 PHP
php多维数组去掉重复值示例分享
Mar 02 PHP
php实现高效获取图片尺寸的方法
Dec 12 PHP
php通过记录IP来防止表单重复提交方法分析
Dec 16 PHP
php数据访问之增删改查操作
May 09 PHP
功能强大的PHP POST提交数据类
Jul 15 PHP
PHP房贷计算器实例代码,等额本息,等额本金
Apr 01 PHP
Yii2框架类自动加载机制实例分析
May 02 PHP
thinkphp5框架前后端分离项目实现分页功能的方法分析
Oct 08 PHP
PHP实现AES256加密算法实例
Sep 22 #PHP
php生成QRcode实例
Sep 22 #PHP
php实现的Captcha验证码类实例
Sep 22 #PHP
php中unserialize返回false的解决方法
Sep 22 #PHP
php实现根据字符串生成对应数组的方法
Sep 22 #PHP
PHP中auto_prepend_file与auto_append_file用法实例分析
Sep 22 #PHP
php中Y2K38的漏洞解决方法实例分析
Sep 22 #PHP
You might like
PHP中array_merge和array相加的区别分析
2013/06/17 PHP
PHP实现针对日期,月数,天数,周数,小时,分,秒等的加减运算示例【基于strtotime】
2017/04/19 PHP
PHP实现链式操作的三种方法详解
2017/11/16 PHP
jQuery实现原理的模拟代码 -6 代码下载
2010/08/16 Javascript
JavaScript高级程序设计 DOM学习笔记
2011/09/10 Javascript
jQuery制作简洁的图片轮播效果
2015/04/03 Javascript
js实现新年倒计时效果
2015/12/10 Javascript
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法
2017/06/17 Javascript
详解vue添加删除元素的方法
2018/06/30 Javascript
js实现的格式化数字和金额功能简单示例
2019/07/30 Javascript
Vue toFixed保留两位小数的3种方式
2020/10/23 Javascript
[24:42]VP vs TNC Supermajor小组赛B组 BO3 第三场 6.2
2018/06/03 DOTA
Python中AND、OR的一个使用小技巧
2015/02/18 Python
Python 3 实现定义跨模块的全局变量和使用教程
2019/07/07 Python
关于pytorch多GPU训练实例与性能对比分析
2019/08/19 Python
详解从Django Allauth中进行登录改造小结
2019/12/18 Python
Python 如何反方向迭代一个序列
2020/07/28 Python
pymysql模块使用简介与示例
2020/11/17 Python
Python使用tkinter制作在线翻译软件
2021/02/22 Python
python 对xml解析的示例
2021/02/27 Python
CSS3属性使网站设计增强同时不消弱可用性
2009/08/29 HTML / CSS
HTML5 video 视频标签使用介绍
2014/02/03 HTML / CSS
猫途鹰英国网站:TripAdvisor英国(旅游社区和旅游评论)
2016/08/30 全球购物
美国网上眼镜商城:Zenni Optical
2016/11/20 全球购物
英国天然宝石首饰购买网站:Gemondo Jewellery
2018/10/23 全球购物
屈臣氏乌克兰:Watsons UA
2019/10/29 全球购物
如何开启linux的ssh服务
2015/02/14 面试题
财务会计毕业生自荐信
2013/11/02 职场文书
大学老师推荐信
2014/02/25 职场文书
酒店行政人事部经理职务说明书
2014/02/26 职场文书
环保公益广告语
2014/03/13 职场文书
设计师求职信模板
2014/05/06 职场文书
员工给公司的建议书
2019/06/24 职场文书
2019年关于小学生课外阅读情况的分析报告
2019/12/02 职场文书
Python中Numpy和Matplotlib的基本使用指南
2021/11/02 Python
React四级菜单的实现
2022/04/08 Javascript