动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 用EMAIL附件传送一个文件(用mail)

作者来源: 
阅读 数 272 人次 , 2006-3-29 4:10:00 


<?

/*
* class mime_mail
* original implementation by sascha schumann < sascha@schumann.cx >
* modified by tobias ratschiller < tobias@dnet.it >:
* - general code clean-up
* - separate body- and from-property
* - killed some mostly un-necessary stuff
*/

class mime_mail
{
var $parts;
var $to;
var $from;
var $headers;
var $subject;
var $body;

/*
* void mime_mail()
* class constructor
*/
function mime_mail()
{
$this->parts = array();
$this->to = "";
$this->from = "";
$this->subject = "";
$this->body = "";
$this->headers = "";
}

/*
* void add_attachment(string message, [string name], [string ctype])
* add an attachment to the mail object
*/
function add_attachment($message, $name = "", $ctype = "application/octet-stream")
{
$this->parts[] = array (
"ctype" => $ctype,
"message" => $message,
"encode" => $encode,
"name" => $name
);
}

/*
* void build_message(array part=
* build message parts of an multipart mail
*/
function build_message($part)
{
$message = $part[ "message"];
$message = chunk_split(base64_encode($message));
$encoding = "base64";
return "content-type: ".$part[ "ctype"].
($part[ "name"]? "; name = \"".$part[ "name"]. "\"" : "").
"\ncontent-transfer-encoding: $encoding\n\n$message\n";
}

/*
* void build_multipart()
* build a multipart mail
*/
function build_multipart()
{
$boundary = "b".md5(uniqid(time()));
$multipart = "content-type: multipart/mixed; boundary = $boundary\n\nthis is a mime
encoded message.\n\n--$boundary";

for($i = sizeof($this->parts)-1; $i >= 0; $i--)
{
$multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary";
}
return $multipart.= "--\n";
}

/*
* void send()
* send the mail (last class-function to be called)
*/
function send()
{
$mime = "";
if (!empty($this->from))
$mime .= "from: ".$this->from. "\n";
if (!empty($this->headers))
$mime .= $this->headers. "\n";

if (!empty($this->body))
$this->add_attachment($this->body, "", "text/plain");
$mime .= "mime-version: 1.0\n".$this->build_multipart();
mail($this->to, $this->subject, "", $mime);
}
}; // end of class

/*
* example usage
*

$attachment = fread(fopen("test.jpg", "r"), filesize("test.jpg"));

$mail = new mime_mail();
$mail->from = " foo@bar.com ";
$mail->headers = "errors-to: foo@bar.com ";
$mail->to = " bar@foo.com ";
$mail->subject = "testing...";
$mail->body = "this is just a test.";
$mail->add_attachment("$attachment", "test.jpg", "image/jpeg");
$mail->send();

*/
?>

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:PHP/MySQL 购物车
· 下一篇:使用PHPLIB进行Session的管理和认证
· 介绍Zend Encode的安装方法
· PHP 编码规范(5)
· 使用PHP制作新闻系统的思路
· PHP如何读取COOKIES
· ftp类(example.php)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077