您的位置:论坛首页 >> 软件论坛论坛 

一个可以发送附件及HTML格式邮件的PHP类

楼主: 2008-5-24 12:56:52
游客
 编辑  删除  回复  收藏  留言  好友   置顶   取消精华

原文:一个可以发送附件及HTML格式邮件的PHP类


一个用Php Class写的发信程序
这个类里显示了发送MIME的方法,也就是HTML格式信件和附件发送的问题。

/*
Mail: An object to encapsulate the sending of email. Allows user-specified
From: addresses, handles the encoding of attachments; conforms more or less to
MIME standards.. Uses sendmail to send the mail, mimencode to do the MIME
encoding, and zip to automatically zip attachments.

Contacting the author(s):
Brought to you by the team at Sequoia Softworks, http://www.sequoiasoft.com
Feel free to contact tony@sequoiasoft.com and tell us how you like it!
(Or to complain bitterly, report bugs, or suggest new features.)

Known shortcomings/bugs:
o guessMIMEType()only knows about a few MIME types. You can expand this as
you need.
o $mime_boundary in the Send() method should be randomly generated, but it
isn't likely to ever hurt anything in its current form

Example:
require("Mail.phtml");
$mymessage = new Mail();
$mymessage->from = "php3.script@www.somedomain.net";
$mymessage->to = "luckyuser@destination.org";
$mymessage->subject = "This is your lucky day";
$mymessage->headers["Reply-To"] = "webmaster@www.somedomain.net";
$mymessage->headers["X-Extra-Header"] = "Pointless Header v3.0";
$mymessage->body = "Doesn't it feel good to get mail?nEspecially with files
attached!n";
$mymessage->attachments[0] = "tarball.tar.gz";
$mymessage->attachments[1] = "images/smiling_countenance.gif";
$mymessage->attachments[2] = "/usr/share/reference/jargondict.html";
$mymessage->attachments[3] = "./core";
$mymessage->attachments[4] = "/etc/passwd"; //naughty naughty!!
$mymessage->ZipAttachments("your_files.zip"); //uncomment this to zip all
//the attachments into one big
//attachment.
$mymessage->Send();
*/
class Mail {
var $from; //The sender
var $to; //The recipient
var $subject; //The subject line
var $headers; //A hash of additional headers (headername => headervalue)
var $zipname; //The name of the file the attachments are zipped into
//($zipname == false if attachments are to be sent
//individually)
var $attachments; //An array of files to attach
var $body; //The body text of the message

//Mail constructor: initializes vars to default values. 'Nuff said.
function Mail() {
$this->from = "";
$this->to = "";
$this->subject = "";
$this->headers = array();
$this->zipname = false;
$this->attachments = array();
$this->body = "";
}

//Auxiliary method, used to guess a file's MIME type
//based on its extension. Doesn't know about too many
//extensions right now
function guessMIMEType($filename) {
//GUESS MIME TYPE
$filename = basename($filename);
if(strrchr($filename,".") == false) {
return("application/octet-stream");
}

$ext = strrchr($filename,".");
switch($ext) {
case ".gif":
return "image/gif";
break;
case ".gz":
return "application/x-gzip";
case ".htm":
case ".html":
return "text/html";
break;
case ".jpg":
return "image/jpeg";
break;
case ".tar":
return "application/x-tar";
break;
case ".txt":
return "text/plain";
break;
case ".zip":
return "application/zip";
break;
default:
return "application/octet-stream";
break;
}
}

//Cute little convenience method. Supply it with a filename to
//zip attachments to, or supply it with false if attachments are
//sent individually
function ZipAttachments($name) {
$this->zipname = $name;
}

//The workhorse method, does the actually sending of the mail.
//Doesn't check for errors so be careful!
function Send($sendmail = "sendmail") {
if($this->from == "")
$fp = popen($sendmail . " -i " . $this->to, "w");
else
$fp = popen($sendmail . " -i -f"" . $this->from . ""
" . $this->to, "w");

$mime_boundary = "-1747901728-1448367683-913849620=:4553";

if($fp == false)
return false;

//Write subject header
fwrite($fp,"Subject: " . $this->subject . "n");

//Write user-defined headers
reset($this->headers);
while(list($hdrname,$hdrval) = each($this->headers)) {
fwrite($fp,$hdrname . ": " . $hdrval . "n");
}

//If there are attachments, this needs to be a MIME message
if(count($this->attachments) > 0) {
//Write MIME headers
fwrite($fp,"MIME-Version: 1.0n");
fwrite($fp,"Content-Type: multipart/mixed; BOUNDARY=""
. $mime_boundary . ""n");
fwrite($fp,"n");
//Write dummy message body
fwrite($fp," This message is in MIME format. The
first part should be readable text,n");
fwrite($fp," while the remaining parts are likely
unreadable without MIME-aware tools.n");
fwrite($fp," Send mail to
mime@docserver.cac.washington.edu for more info.n");
fwrite($fp,"n");

//Write message text
fwrite($fp,"--" . "$mime_boundary" . "n");
fwrite($fp,"Content-Type: text/plain; charset=US-
ASCIIn");
fwrite($fp,"n");
fwrite($fp,$this->body);
fwrite($fp,"n");

//Handle attachments
if($this->zipname != false) { //IF we've been told to
//zip the attachments
fwrite($fp,"--" . $mime_boundary . "n");
fwrite($fp,"Content-Type: application/zip; name=
"". $this->zipname . ""n");
fwrite($fp,"Content-Transfer-Encoding: base64
n");
//fwrite($fp,"Content-ID: " . $content_ID . "n");
fwrite($fp,"Content-Description:n");
fwrite($fp,"n");
$cmdline = "zip - ";
while(list($key, $attachment_name) = each($this->attachments))
$cmdline .= "$attachment_name ";
$cmdline .= "| mimencode -b";
$pp = popen($cmdline,"r");
while(!feof($pp)) {
$data = fread($pp,4096);
fwrite($fp,$data);
}
pclose($pp);
}
else { //no need to zip the attachments, attach them
//separately
while(list($key, $attachment_name) = each($this->attachments)) {
fwrite($fp,"--" . $mime_boundary . "n");
fwrite($fp,"Content-Type: " . $this->guessMIMEType($attachment_name) . ";
name="". basename($attachment_name) . ""n");
fwrite($fp,"Content-Transfer-Encoding: base64n");
//fwrite($fp,"Content-ID: " .
//$content_ID . "n");
fwrite($fp,"Content-Description:n");
fwrite($fp,"n");
$pp = popen("mimencode -b $attachment_name","r");
while(!feof($pp)) {
$data = fread($pp,4096);
fwrite($fp,$data);
}
pclose($pp);
}
}

fwrite($fp,"--" . $mime_boundary . "--n");
}
//No need for a MIME message, so it's an RFC822 message
else {
fwrite($fp,"n");
fwrite($fp,$this->body);
}


pclose($fp);
}
}
?>
 

 1楼: 2008-5-29 6:04:30
游客
 删除  留言  好友

故障类别:
黑屏;花屏;发暗;图象模糊;显示抖动;色偏;触摸不准;触摸失效;屏裂;黑带;报警;程序问题等等
维修实力:
速度快,成功率高,价格公道
联系人:唐工 北京京电测维科技有限公司 13161609988 FX:010-52022328
维修对象:
西门子OP17、27、37,TP170A、170B,TP270、TP178,MP270、MP370、TD200、400C,三菱F920GOT、F930GOT、F940GOT、F940WGOT、A970GOT、A985GOT,欧姆龙NS10-TV00B-V2、NT631、MPT002,富士UG430、530系列、DBH45、55-4A,海泰克PWS1760-CTN、PWS520S-LED、PWS3260-DTN、PWS3760-TFT,基恩士VT2-7SR、VT2-5MW、VT2-V7、VT2-10FB,研华TPC-60S、TPC-1260系列、TPC-1560系列,研扬AMB655、AOP-8070、EMB-820T 、OPD-217,台安TP02系列、OPS6、16、36L,威纶MT506L 、MT506M 、MT8121等等;
欢迎来电咨询。
 

 2楼: 2008-7-17 5:16:30
游客
 删除  留言  好友

厦门小虫社区正式开通运营,欢迎厦门广大网虫们踊跃注册!
口号:打造一个和谐温馨的社区
网址:http://bbs.xcshxx.cn
 

 3楼: 2008-7-20 14:57:33
游客
 删除  留言  好友

厦门小虫社区正式开通运营,欢迎厦门广大网虫们踊跃注册!
口号:打造一个和谐温馨的社区
网址:http://bbs.xcshxx.cn
 

 4楼: 2008-7-30 12:04:55
游客
 删除  留言  好友

上海明基 笔记本风扇维修服务中心 

客户服务热线电话 021-64865667 54246961

维修点网址:http://www.pc1998.cn
维修部地址:上海市徐汇区南丹东路228弄1号1003室
地点:漕溪北路路口金轩酒店后面第一栋楼10楼,地铁一号线徐家汇站二号出口对面
联系人:肖亮

浦东新区维修点地址:福山路309号竹园商务楼2101室(地铁2、4、6号世纪大道站6号出口对面,大阿福酒店旁)
浦东新区维修部电话:021-50583010 58309870


全新 原装BENQ 明基 笔记本风扇,适用于JOYBOOK A33 ,A33E,带散热片,绝对全新,没上过机,工厂全新包装,价格全国最低价,请放心购买.

另外还有大量明基其它机种的CPU风扇销售:JOYBOOK DH2100 7000 R53 R22E S52 S53 S53E A33 A32 A33E等.

IBM X60 X60S风扇

上海金汤笔记本维修配件中心

http://www.pc1998.cn
 

 5楼: 2008-8-1 3:26:11
游客
 删除  留言  好友

厦门小虫社区正式开通运营,欢迎厦门广大网虫们踊跃注册!
口号:打造一个和谐温馨的社区
网址:http://bbs.xcshxx.cn
招聘管理团队和版主
 

 6楼: 2008-8-14 10:31:29
游客
 删除  留言  好友

山东省畜牧局大型畜牧良种原种场是由省、市畜牧部门审批,在工商管理部门注册,成立于1986年,多年从事畜牧良种的繁育和推广改良工作,经验丰富、实力雄厚,本场现有高级畜牧师6名,中级畜牧师8名,助理畜牧师10名,有6处养殖场及10余个养殖基地和推广中心是集科研、育种、推广为一体的综合性基地,是鲁西南规模较大的种畜繁育场体系,几年来向全国各地调拨纯种畜牧良种200万余只,并成功的在各地繁衍生息,为畜牧业的健康发展,畜牧品种的改良,农牧民朋友的脱贫致富做出了一定的贡献。先后被省畜牧协会、市、县畜牧局评为:“一级繁育基地”“经济支柱企业” “重合同守信用单位” “十佳养殖企业” “养殖示范单位”等称号。
我场常年外调:黑白花奶牛、红白花奶牛、荷斯坦奶牛、西门塔尔牛、鲁西黄牛、利木赞牛、夏洛莱牛、海福特牛、小尾寒羊、波尔山羊等优质畜牧良种。为确保客户的利益,我们严把质量关、价格关、防疫关、出境手、运输关,做到售前、售中、售后全方位跟踪服务。
经  理:  董运河 
手  机:  013954710488
联系电话:  0537-6784598
传  真:  0537-6785186
客服QQ:  79003040 
网  址:  www.yfdxyzc.com 
Email:  chinaniu@126.com
 


回复帖子 RE: 一个可以发送附件及HTML格式邮件的PHP类

 1、第1个回帖者得3分,第2个回帖者得2分,其余得1个积分!

 2、如是求助帖,前10位有效回帖用户每人再各加10分!

 3、如果乱回求助帖,让原帖作者很不满意,他有权收回积分,系统也同时对您惩罚,扣除10个积分

帐号:
用户名 密码 (不填为游客)  注册新用户
内容:


撤消 恢复 粗体 斜体 下划线 左对齐 居中 右对齐 超级链接 取消超级链接 插入网上图片,支持格式为:gif、jpg、png、bmp 使用帮助 上传图片
表情符号:


附加码:
(附加码: 255 )