php实现邮件发送并带有附件


Posted in PHP onJanuary 24, 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); 
} 3water.com
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类型 
*/ 
?>

演示示例

<?php 
require_once('emailclass.php');
//发送邮件
//主? 
$subject = "test send email";
//收件人 
$sendto = 'abc@163.com';
//?件人 
$replyto = 'cdf@163.com';
//?热 3water.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.MVC的模板标签系统(一)
Sep 05 PHP
PHP下常用正则表达式整理
Oct 26 PHP
PHP简洁函数(PHP简单明了函数语法)
Jun 10 PHP
php正则表达式使用的详细介绍
Apr 27 PHP
php中url传递中文字符,特殊危险字符的解决方法
Aug 17 PHP
php实现处理输入转义字符的代码
Nov 08 PHP
配置eAccelerator和XCache扩展来加速PHP程序的执行
Dec 22 PHP
PHP简单数据库操作类实例【支持增删改查及链式操作】
Oct 10 PHP
实例分析基于PHP微信网页获取用户信息
Nov 24 PHP
PHP数组式访问接口ArrayAccess用法分析
Dec 28 PHP
PHP实现断点续传乱序合并文件的方法
Sep 06 PHP
PHP实现两种排课方式
Jun 26 PHP
php中sql注入漏洞示例 sql注入漏洞修复
Jan 24 #PHP
php 发送带附件邮件示例
Jan 23 #PHP
php 获取页面中指定内容的实现类
Jan 23 #PHP
php 根据url自动生成缩略图并处理高并发问题
Jan 23 #PHP
php 字符串压缩方法比较示例
Jan 23 #PHP
php 生成短网址原理及代码
Jan 23 #PHP
解决php接收shell返回的结果中文乱码问题
Jan 23 #PHP
You might like
php标签云的实现代码
2012/10/10 PHP
对PHP新手的一些建议(PHP学习经验总结)
2014/08/20 PHP
php使用ftp远程上传文件类(完美解决主从文件同步问题的方法)
2016/09/23 PHP
PHP脚本自动识别验证码查询汽车违章
2016/12/20 PHP
stripos函数知识点实例分享
2019/02/11 PHP
用JavaScript页面不刷新时全选择,全删除(GridView)
2009/04/14 Javascript
fireworks菜单生成器mm_menu.js在 IE 7.0 显示问题的解决方法
2009/10/20 Javascript
jquery attr 设定src中含有&amp;(宏)符号问题的解决方法
2011/07/26 Javascript
基于jquery的多功能软键盘插件
2012/07/25 Javascript
『jQuery』取指定url格式及分割函数应用
2013/04/22 Javascript
JavaScript学习笔记之基础语法
2015/01/22 Javascript
Javascript oop设计模式 面向对象编程简单实例介绍
2016/12/13 Javascript
Vue2 使用 Echarts 创建图表实例代码
2017/05/18 Javascript
学习JS中的DOM节点以及操作
2018/04/30 Javascript
垃圾回收器的相关知识点总结
2018/05/13 Javascript
微信小程序仿微信运动步数排行(交互)
2018/07/13 Javascript
vue-cli3 从搭建到优化的详细步骤
2019/01/20 Javascript
浅谈vue-router路由切换 组件重用挖下的坑
2019/11/01 Javascript
微信小程序基于movable-view实现滑动删除效果
2020/01/08 Javascript
JavaScript中reduce()的5个基本用法示例
2020/07/19 Javascript
[03:46]显微镜下的DOTA2第七期——满血与残血
2014/06/20 DOTA
[01:47]2018年度DOTA2最具人气解说-完美盛典
2018/12/16 DOTA
python中bisect模块用法实例
2014/09/25 Python
matplotlib绘制动画代码示例
2018/01/02 Python
Python使用Flask-SQLAlchemy连接数据库操作示例
2018/08/31 Python
Django使用中间件解决前后端同源策略问题
2019/09/02 Python
tensorflow 实现数据类型转换
2020/02/17 Python
Pycharm调试程序技巧小结
2020/08/08 Python
DIY手工制作经营店创业计划书
2014/02/01 职场文书
写好自荐信需做到的5要点
2014/03/07 职场文书
乒乓球兴趣小组活动总结
2014/07/08 职场文书
2015年春训学习心得体会范文
2015/03/09 职场文书
2015年司机工作总结
2015/04/23 职场文书
2016元旦主持人开场白
2015/12/03 职场文书
2016计算机专业毕业生自荐信
2016/01/28 职场文书
python函数指定默认值的实例讲解
2021/03/29 Python