文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>发送html、附件、文本文件、html图片的类(2)

发送html、附件、文本文件、html图片的类(2)

时间:2007-02-17  来源:PHP爱好者

*******************************************************************************/

function formatAttachmentHeader($inFileLocation){

$outAttachmentHeader = "";

//--get content type based on file extension

$contentType = $this->getContentType($inFileLocation);

//--if content type is TEXT the standard 7bit encoding

if(ereg( "text",$contentType)){

//--format header

$outAttachmentHeader .= "Content-Type: ".$contentType. ";n";

$outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";

$outAttachmentHeader .= "Content-Transfer-Encoding: 7bitn";

$outAttachmentHeader .= "Content-Disposition: attachment;n"; //--other: inline

$outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";

$textFile = fopen($inFileLocation, "r");

//--loop through file, line by line

while(!feof($textFile)){

//--get 1000 chars or (line break internal to fgets)

$outAttachmentHeader .= fgets($textFile,1000);

}

//--close file

fclose($textFile);

$outAttachmentHeader .= "n";

}

//--NON-TEXT use 64-bit encoding

else{

//--format header

$outAttachmentHeader .= "Content-Type: ".$contentType. ";n";

$outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";

$outAttachmentHeader .= "Content-Transfer-Encoding: base64n";

$outAttachmentHeader .= "Content-Disposition: attachment;n"; //--other: inline

$outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";

//--call uuencode - output is returned to the return array

exec( "uuencode -m $inFileLocation nothing_out",$returnArray);

//--add each line returned

for ($i = 1; $i<(count($returnArray)); $i++){

$outAttachmentHeader .= $returnArray[$i]. "n";

}

}

return $outAttachmentHeader;

}

/*******************************************************************************

Function: send()

Description: sends the email

Arguments: none

Returns: true if sent

*******************************************************************************/

function send(){

//--set mail header to blank

$mailHeader = "";

//--add CC

if($this->mailCC != "") $mailHeader .= "CC: ".$this->mailCC. "n";

//--add BCC

if($this->mailBCC != "") $mailHeader .= "BCC: ".$this->mailBCC. "n";

//--add From

if($this->mailFrom != "") $mailHeader .= "FROM: ".$this->mailFrom. "n";

//---------------------------MESSAGE TYPE-------------------------------

//--TEXT ONLY

if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){

return mail($this->mailTo,$this->mailSubject,$this->mailText,$mailHeader);

}

//--HTML AND TEXT

elseif($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments == ""){

//--get random boundary for content types

$bodyBoundary = $this->getRandomBoundary();

//--format headers

$textHeader = $this->formatTextHeader();

$htmlHeader = $this->formatHTMLHeader();

//--set MIME-Version

$mailHeader .= "MIME-Version: 1.0n";

//--set up main content header with boundary

$mailHeader .= "Content-Type: multipart/alternative;n";

$mailHeader .= ' boundary="'.$bodyBoundary. '"';

$mailHeader .= "nnn";

//--add body and boundaries

$mailHeader .= "--".$bodyBoundary. "n";

$mailHeader .= $textHeader;

$mailHeader .= "--".$bodyBoundary. "n";

//--add html and ending boundary

$mailHeader .= $htmlHeader;

if($this->mailImg!="")

{

//--add html include images

$ImgArray = explode( ",",$this->mailImg);

//--loop through each Img

for($i=0;$i<count($ImgArray);$i++){

//--Img separator

$mailHeader .= "n--".$bodyBoundary. "n";

//--get images info

$mailHeader .= $this->formatImgHeader($ImgArray[$i]);

}

}

$mailHeader .= "n--".$bodyBoundary. "--";

//--send message

return mail($this->mailTo,$this->mailSubject, "",$mailHeader);

}

//--HTML AND TEXT AND ATTACHMENTS

elseif($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments != ""){

//--get random boundary for attachments

$attachmentBoundary = $this->getRandomBoundary();

//--set main header for all parts and boundary

$mailHeader .= "Content-Type: multipart/mixed;n";

$mailHeader .= ' boundary="'.$attachmentBoundary. '"'. "nn";

$mailHeader .= "This is a multi-part message in MIME format.n";

$mailHeader .= "--".$attachmentBoundary. "n";

//--TEXT AND HTML--

//--get random boundary for content types

$bodyBoundary = $this->getRandomBoundary(1);

//--format headers

$textHeader = $this->formatTextHeader();

$htmlHeader = $this->formatHTMLHeader();

//--set MIME-Version

$mailHeader .= "MIME-Version: 1.0n";

//--set up main content header with boundary

$mailHeader .= "Content-Type: multipart/alternative;n";

$mailHeader .= ' boundary="'.$bodyBoundary. '"';

$mailHeader .= "nnn";

//--add body and boundaries

$mailHeader .= "--".$bodyBoundary. "n";

$mailHeader .= $textHeader;

$mailHeader .= "--".$bodyBoundary. "n";

//--add html and ending boundary

$mailHeader .= $htmlHeader;

if($this->mailImg!="")

{

//--add html include images

$ImgArray = explode( ",",$this->mailImg);

//--loop through each Img

for($i=0;$i<count($ImgArray);$i++){

//--Img separator

$mailHeader .= "n--".$bodyBoundary. "n";

//--get images info

$mailHeader .= $this->formatImgHeader($ImgArray[$i]);

}

}

$mailHeader .= "n--".$bodyBoundary. "--";

//--send message

//--END TEXT AND HTML

//--get array of attachment filenames

$attachmentArray = explode( ",",$this->mailAttachments);

//--loop through each attachment

for($i=0;$i<count($attachmentArray);$i++){

//--attachment separator

$mailHeader .= "n--".$attachmentBoundary. "n";

//--get attachment info

$mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);

}

$mailHeader .= "--".$attachmentBoundary. "--";

return mail($this->mailTo,$this->mailSubject, "",$mailHeader);

}elseif($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments != "")

{

//--get random boundary for attachments

$attachmentBoundary = $this->getRandomBoundary();

//--set main header for all parts and boundary

$mailHeader .= "Content-Type: multipart/mixed;n";

$mailHeader .= ' boundary="'.$attachmentBoundary. '"'. "nn";

$mailHeader .= "This is a multi-part message in MIME format.n";

$mailHeader .= "--".$attachmentBoundary. "n";

//--TEXT AND HTML--

//--get random boundary for content types

$bodyBoundary = $this->getRandomBoundary(1);

//--format headers

$textHeader = $this->formatTextHeader();

//--$htmlHeader = $this->formatHTMLHeader();

//--set MIME-Version

$mailHeader .= "MIME-Version: 1.0n";

//--set up main content header with boundary

$mailHeader .= "Content-Type: multipart/alternative;n";

$mailHeader .= ' boundary="'.$bodyBoundary. '"';

$mailHeader .= "nnn";

//--add body and boundaries

$mailHeader .= "--".$bodyBoundary. "n";

$mailHeader .= $textHeader;

//--$mailHeader .= "--".$bodyBoundary. "n";

//--add html and ending boundary

//--$mailHeader .= $htmlHeader;

$mailHeader .= "n--".$bodyBoundary. "--";

//--send message

//--END TEXT AND HTML

//--get array of attachment filenames

$attachmentArray = explode( ",",$this->mailAttachments);

//--loop through each attachment

for($i=0;$i<count($attachmentArray);$i++){

//--attachment separator

$mailHeader .= "n--".$attachmentBoundary. "n";

//--get attachment info

$mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);

}

$mailHeader .= "--".$attachmentBoundary. "--";

return mail($this->mailTo,$this->mailSubject, "",$mailHeader);

}

return false;

}

}

function message($mess)

{

print "<table border=0 cellpadding=20 cellspacing=5 width=90%><tr><td

align=center>MESSAGE</td></tr><tr><td align=center class=1050>$mess</td></tr>";

print "<tr><td align=center><a href="javascript:window.history.go(-1);" target=_self

style="color=blue">返回上一页</a></td></tr></table></body></html>";

exit;

}

?>

测试程序email.php

<?

if($paction=="send")

{

include("./emailclass.php");

if($emailto==""||$emailfrom==""||$title==""||$content=="")

{

message("EMAILL地址有错或内容/主题没填写!");

}else

{

$content=stripSlashes($content);

$mail= new Email();

if(!($mail->setTo($emailto)))

{

message("收件人EMAIL地址有错!");

exit;

}

if($emailto2!="")

{

if(!($mail->setCC($emailto2)))

{

message("邮件抄送EMAIL地址有错!");

exit;

}

}

if($emailto3!="")

{

if(!($mail->setBCC($emailto3)))

{

message("邮件转发EMAIL地址有错!");

exit;

}

}

if(!($mail->setFrom($emailfrom)))

{

message("您的回复EMAIL地址有错!");

exit;

}

if(!($mail->setSubject($title)))

{

message("您没有填写主题!");

exit;

}

if($html==0)

{

if(!($mail->setText($content)))

{

message("您没有书写邮件内容!");

exit;

}

}else

{

$mail->setText("以下的内容包含HTML代码");

if(!($mail->setHTML($content)))

{

message("您没有书写邮件内容!");

exit;

}

}

if($file!="none" && $file!=""&& $file_size>0)

{

exec("cp $file ./temp/$file_name");

$mail->setAttachments("./temp/$file_name");

# $mail->setAttachments("$file");

}

$tatus=$mail->send();

if($tatus)

{

print "<div align=center>您的信息已经成功发送!</div>";

}else

{

message("对不起您的信息没能发送成功!请再试一次。如有问题请与<a

href=mailto:[email protected]>William</a>联系!

或访问我的主页寻求帮助。<a href=http://www.chinaebu.com target=_blank>www.chinaebu.com</a>

<a href=http://www.chinesebbc.com target=_blank>www.chinesebbc.com</a></div>");

exit;

}

exec("rm ./temp/$file_name");

}

}

?>

<html>

<body>

<table width="80%" border="0" cellspacing="1" cellpadding="2" align=center bgcolor="#FFFFff">

<form name=go ENCTYPE="multipart/form-data" action="<?echo $PHP_SELF?>" target=_self

method=post>

<tr bgcolor="#006699">

<td align=center colspan=2><font color=#ffffff><b>发送电子邮件</b></font></td>

</tr>

<tr bgcolor="#3388bb">

<td nowrap><font color="#FFFFFF">对方电子邮件:</font></td>

<td nowrap><input type=hidden name=paction value=send><input type=text name=emailto

size=30></td></tr>

<tr bgcolor="#3388bb">

<td><font color="#FFFFFF">您的电子邮件:</font></td>

<td> <font color="#FFFFFF">

<input type=text name=emailfrom maxlength=50 size=50 value="">

</font></td>

</tr>

<tr bgcolor="#3388bb">

<td><font color="#FFFFFF">邮 件 抄 送:</font></td>

<td> <font color="#FFFFFF">

<input type=text name=emailto2 size=50>

</font></td>

</tr>

<tr bgcolor="#3388bb">

<td><font color="#FFFFFF">邮 件 转 发:</font></td>

<td> <font color="#FFFFFF">

<input type=text name=emailto3 size=50>

</font></td>

</tr>

<tr bgcolor="#3388bb">

<td><font color="#FFFFFF">邮 件 主 题 :</font></td>

<td> <font color="#FFFFFF">

<input type=text name=title maxlength=50 size=50>

</font></td>

</tr>

<tr bgcolor="#3388bb">

<td><font color="#FFFFFF">选 择 附 件 :</font></td>

<td> <font color="#FFFFFF">

<input type=hidden name="MAX_FILE_SIZE" value="40000">

<input type="file" name="file">

</font></td>

</tr>

<tr bgcolor="#3388bb">

<td><font color="#FFFFFF">邮 件 模 式 :</font></td>

<td> <font color="#FFFFFF">

<select name=html>

<option value=1>包含HTML

<option selected value=0>不包含HTML

</select>

</font></td>

</tr>

<tr bgcolor="#3388bb">

<td><font color="#FFFFFF">邮 件 内 容 :</font></td>

<td> <font color="#FFFFFF">

<textarea name="content" cols="80" rows="15"></textarea>

</font></td>

</tr>

<tr bgcolor="#3388bb">

<td colspan=2 align=center> <font color="#FFFFFF">

<input type=submit name=go value=发送>

<input type=reset name=reset value=重填>

</font></td>

</tr>

</form>

</table>

</body></html>
php爱好者站 http://www.phpfans.net 网页特效|网页模板
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载