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 相关文章推荐
PHP常用代码大全(新手入门必备)
Jun 29 PHP
采用PHP函数memory_get_usage获取PHP内存清耗量的方法
Dec 06 PHP
php遍历所有文件及文件夹的方法深入解析
Jun 08 PHP
ThinkPHP CURD方法之field方法详解
Jun 18 PHP
php创建多级目录的方法
Mar 24 PHP
thinkPHP线上自动加载异常与修复方法实例分析
Dec 01 PHP
PHP函数rtrim()使用中的怪异现象分析
Feb 24 PHP
Thinkphp结合ajaxFileUpload实现异步图片传输示例
Mar 13 PHP
form自动提交实例讲解
Jul 10 PHP
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
Jul 22 PHP
thinkphp5修改view到根目录实例方法
Jul 02 PHP
PHP实现chrome表单请求数据转换为接口使用的json数据
Mar 04 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 MsSql server时遇到的中文编码问题
2009/06/11 PHP
php feof用来识别文件末尾字符的方法
2010/08/01 PHP
php中3种方法删除字符串中间的空格
2014/03/10 PHP
PHP连接操作access数据库实例
2015/03/30 PHP
PHP合并数组函数array_merge用法分析
2017/02/17 PHP
php插件Xajax使用方法详解
2017/08/31 PHP
PHP实现打包下载文件的方法示例
2017/10/07 PHP
Javascript 面向对象之重载
2010/05/04 Javascript
jQuery .attr()和.removeAttr()方法操作元素属性示例
2013/07/16 Javascript
Clipboard.js 无需Flash的JavaScript复制粘贴库
2015/10/02 Javascript
JavaScript高级教程5.6之基本包装类型(详细)
2015/11/23 Javascript
vue.js的提示组件
2017/03/02 Javascript
详解vue-cli + webpack 多页面实例应用
2017/04/25 Javascript
bootstrap modal+gridview实现弹出框效果
2017/08/15 Javascript
taro小程序添加骨架屏的实现代码
2019/11/15 Javascript
分析在Python中何种情况下需要使用断言
2015/04/01 Python
Python 爬虫之超链接 url中含有中文出错及解决办法
2017/08/03 Python
python 设置文件编码格式的实现方法
2017/12/21 Python
VScode编写第一个Python程序HelloWorld步骤
2018/04/06 Python
Python中利用xpath解析HTML的方法
2018/05/14 Python
python绘制中国大陆人口热力图
2018/11/07 Python
python简单贪吃蛇开发
2019/01/28 Python
对python numpy.array插入一行或一列的方法详解
2019/01/29 Python
wxpython多线程防假死与线程间传递消息实例详解
2019/12/13 Python
pymysql模块的操作实例
2019/12/17 Python
websocket+sockjs+stompjs详解及实例代码
2018/11/30 HTML / CSS
HTML5和以前HTML4的区别整理
2013/10/20 HTML / CSS
html5+css如何实现中间大两头小的轮播效果
2018/12/06 HTML / CSS
工商管理实习生自我鉴定范文
2013/12/18 职场文书
我的求职择业计划书
2014/04/04 职场文书
2015年感恩母亲节活动方案
2015/05/04 职场文书
天鹅湖观后感
2015/06/09 职场文书
焦裕禄纪念馆观后感
2015/06/09 职场文书
辞职信怎么写?你都知道吗?
2019/06/24 职场文书
springBoot基于webSocket实现扫码登录
2021/06/22 Java/Android
python manim实现排序算法动画示例
2022/08/14 Python