php 发送带附件邮件示例


Posted in PHP onJanuary 23, 2014

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 相关文章推荐
在JavaScript中调用php程序
Mar 09 PHP
Fatal error: Call to undefined function curl_init()解决方法
Apr 09 PHP
PHP5.3的垃圾回收机制(动态存储分配方案)深入理解
Dec 10 PHP
基于php伪静态的实现详细介绍
Apr 28 PHP
CI(CodeIgniter)框架中的增删改查操作
Jun 10 PHP
thinkphp中session和cookie无效的解决方法
Dec 19 PHP
PHP四舍五入、取整、round函数使用示例
Feb 06 PHP
33道php常见面试题及答案
Jul 06 PHP
Thinkphp3.2.3分页使用实例解析
Jul 28 PHP
PHP实现登陆表单提交CSRF及验证码
Jan 24 PHP
PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别分析
Jul 03 PHP
WordPress免插件实现面包屑导航的示例代码
Aug 20 PHP
php 获取页面中指定内容的实现类
Jan 23 #PHP
php 根据url自动生成缩略图并处理高并发问题
Jan 23 #PHP
php 字符串压缩方法比较示例
Jan 23 #PHP
php 生成短网址原理及代码
Jan 23 #PHP
解决php接收shell返回的结果中文乱码问题
Jan 23 #PHP
php弹出对话框实现重定向代码
Jan 23 #PHP
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
Jan 22 #PHP
You might like
PHP实现对文本数据库的常用操作方法实例演示
2014/07/04 PHP
PHP连接access数据库
2015/03/27 PHP
在Mac上编译安装PHP7的开发环境
2015/07/28 PHP
php通过执行CutyCapt命令实现网页截图的方法
2016/09/30 PHP
简易的投票系统以及js刷票思路和方法
2015/04/07 Javascript
JavaScript 七大技巧(二)
2015/12/13 Javascript
对Angular.js Controller如何进行单元测试
2016/10/25 Javascript
vue组件实现弹出框点击显示隐藏效果
2020/10/26 Javascript
layer弹出层父子页面事件相互调用方法
2018/08/17 Javascript
ES6数组与对象的解构赋值详解
2019/06/14 Javascript
微信小程序嵌入腾讯视频源过程详解
2019/08/08 Javascript
手把手教你实现 Promise的使用方法
2020/09/02 Javascript
Vue中父子组件的值传递与方法传递
2020/09/28 Javascript
详解vue-cli项目在IE浏览器打开报错解决方法
2020/12/10 Vue.js
[50:54]完美世界DOTA2联赛 GXR vs IO 第三场 11.07
2020/11/10 DOTA
Python查找相似单词的方法
2015/03/05 Python
Python编程实现控制cmd命令行显示颜色的方法示例
2017/08/14 Python
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
2018/02/03 Python
基于DataFrame筛选数据与loc的用法详解
2018/05/18 Python
python实现祝福弹窗效果
2019/04/07 Python
eclipse创建python项目步骤详解
2019/05/10 Python
python批量读取文件名并写入txt文件中
2020/09/05 Python
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
2020/12/11 Python
通过canvas转换颜色为RGBA格式及性能问题的解决
2019/11/22 HTML / CSS
打造经典复古风格的品牌:Alice + Olivia(爱丽丝+奥利维亚)
2016/09/07 全球购物
优衣库美国官网:UNIQLO美国
2018/04/14 全球购物
PHP如何设置和取得Cookie值
2015/06/30 面试题
节约用水倡议书
2014/04/16 职场文书
医药公司采购员岗位职责
2014/09/12 职场文书
再婚婚前财产协议书范本
2014/10/19 职场文书
贷款承诺书
2015/01/20 职场文书
区域经理岗位职责
2015/02/02 职场文书
教师个人发展总结
2015/02/11 职场文书
2019年教师节祝福语精选,给老师送上真诚的祝福
2019/09/09 职场文书
详解使用 CSS prefers-* 规范提升网站的可访问性与健壮性
2021/05/25 HTML / CSS
tomcat默认最大连接数及相关调整方法
2022/05/06 Servers