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 相关文章推荐
用ADODB来让PHP操作ACCESS数据库的方法
Dec 31 PHP
PHP中数组合并的两种方法及区别介绍
Sep 14 PHP
PHP类与对象中的private访问控制的疑问
Nov 01 PHP
smarty缓存用法分析
Dec 16 PHP
PHP贪婪算法解决0-1背包问题实例分析
Mar 23 PHP
自写的利用PDO对mysql数据库增删改查操作类
Feb 19 PHP
thinkPHP3.2.2框架行为扩展及demo示例
Jun 19 PHP
PHP进阶学习之Geo的地图定位算法详解
Jun 19 PHP
解决php extension 加载顺序问题
Aug 16 PHP
Yii框架通过请求组件处理get,post请求的方法分析
Sep 03 PHP
php 中的信号处理操作实例详解
Mar 04 PHP
yii2.0框架多模型操作示例【添加/修改/删除】
Apr 13 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
Aster vs KG BO3 第二场2.18
2021/03/10 DOTA
关于JAVASCRIPT urldecode URL解码的问题
2012/01/08 Javascript
js调用AJAX时Get和post的乱码解决方法
2013/06/04 Javascript
获取select元素被选中的文本内容的js代码
2014/01/29 Javascript
js用闭包遍历树状数组的方法
2014/03/19 Javascript
JS简单实现动画弹出层效果
2015/05/05 Javascript
JS+CSS实现鼠标滑过时动态翻滚的导航条效果
2015/09/24 Javascript
基于JQuery的$.ajax方法进行异步请求导致页面闪烁的解决办法
2016/05/10 Javascript
javascript常用经典算法详解
2017/01/11 Javascript
JS中setTimeout和setInterval的最大延时值详解
2017/02/13 Javascript
vue分类筛选filter方法简单实例
2017/03/30 Javascript
浅谈Angular2 模块懒加载的方法
2017/10/04 Javascript
jQuery插件实现的日历功能示例【附源码下载】
2018/09/07 jQuery
从vue源码看props的用法
2019/01/09 Javascript
node命令行工具之实现项目工程自动初始化的标准流程
2019/08/12 Javascript
微信小程序后端(java)开发流程的详细步骤
2019/11/13 Javascript
jQuery+ajax实现文件上传功能
2020/12/22 jQuery
[06:20]2015国际邀请赛第三日top10
2015/08/08 DOTA
[04:00]黄浦江畔,再会英雄——完美世界DOTA2 TI9应援视频
2019/07/31 DOTA
python下MySQLdb用法实例分析
2015/06/08 Python
Python通过for循环理解迭代器和生成器实例详解
2019/02/16 Python
python开发游戏的前期准备
2019/05/05 Python
python同时遍历两个list用法说明
2020/05/02 Python
解决keras加入lambda层时shape的问题
2020/06/11 Python
HTML5 Canvas玩转酷炫大波浪进度图效果实例(附demo)
2016/12/14 HTML / CSS
欧迪办公美国官网:Office Depot
2016/08/22 全球购物
巴西葡萄酒销售网站:Wine.com.br
2017/11/07 全球购物
ReVive利维肤美国官网:RéVive Skincare
2018/04/18 全球购物
英国网上超市:Ocado
2020/03/05 全球购物
护士自我评价
2014/02/01 职场文书
临时工聘用合同协议书
2014/10/29 职场文书
羊脂球读书笔记
2015/06/30 职场文书
运动会闭幕式主持词
2015/07/01 职场文书
高中班主任心得体会
2016/01/07 职场文书
如何制定一份可行的计划!
2019/06/21 职场文书
Apache Pulsar集群搭建部署详细过程
2022/02/12 Servers