如何发送带附件的邮件?

billgate2000 2003-02-19 12:15:55
小弟最近碰到个难题,php的mail函数能发送邮件,但好像不能带附件,请问有什么好方法带附件一起发送
...全文
135 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nhconch 2003-02-19
  • 打赏
  • 举报
回复
这是提交页面的form部分:
<form name="form1" method="post" action="sendmail.php" enctype="multipart/form-data">
<tr bgcolor="#FDCAF5">
<td align=right height=32 width="19%" class="unnamed1" bgcolor="#66CCFF"><font color="#D73383">*姓名:</font></td>
<td height=32 width="81%" class="unnamed1" bgcolor="#66CCFF">
<input type=text name=name>
</td>
</tr>
<tr>
<td align=right bgcolor=#f5f5f5 height=20 width="19%" class="unnamed1"><font color="#D73383">性别:</font></td>
<td bgcolor=#f5f5f5 height=20 width="81%" class="unnamed1">
<input CHECKED name=sex type=radio value=男>
男 
<input name=sex type=radio value=女>
女</td>
</tr>
<tr bgcolor="#66CCFF">
<td align=right height=25 width="19%" class="unnamed1"><font color="#D73383">年龄:</font></td>
<td height=25 width="81%" class="unnamed1" bgcolor="#66CCFF">
<input type=text name=age size=7>
</td>
</tr>
<tr>
<td align=right bgcolor=#f5f5f5 height=25 width="19%" class="unnamed1"><font color="#D73383">籍贯:</font></td>
<td bgcolor=#f5f5f5 height=25 width="81%" class="unnamed1">
<input type=text name=native size="15">
</td>
</tr>
<tr>
<td align=right bgcolor=#f5f5f5 height=25 width="19%" class="unnamed1"><font color="#D73383">政治面貌:</font></td>
<td bgcolor=#f5f5f5 height=25 width="81%" class="unnamed1">
<input type=text name=polity>
</td>
</tr>
<tr bgcolor="#66CCFF">
<td align=right height=25 width="19%" class="unnamed1"><font color="#D73383">所在单位:</font></td>
<td height=25 width="81%" class="unnamed1">
<input type=text name=company size=40>
</td>
</tr>
<tr>
<td align=right bgcolor=#f5f5f5 height=25 width="19%" class="unnamed1"><font color="#D73383">从事职务:</font></td>
<td bgcolor=#f5f5f5 height=25 width="81%" class="unnamed1">
<input type=text name=job>
</td>
</tr>
<tr>
<td align=right bgcolor=#f5f5f5 height=25 width="19%" class="unnamed1"><font color="#D73383">*电子邮件:</font></td>
<td bgcolor=#f5f5f5 height=25 width="81%" class="unnamed1">
<input type=text name=email>
</td>
</tr>
<tr bgcolor="#66CCFF">
<td align=right height=25 width="19%" class="unnamed1"><font color="#D73383">*联系地址:</font></td>
<td height=25 width="81%" class="unnamed1">
<input type=text name=address size=48>
</td>
</tr>
<tr>
<td align=right bgcolor=#f5f5f5 height=25 width="19%" class="unnamed1"><font color="#D73383">邮政编码:</font></td>
<td bgcolor=#f5f5f5 height=25 width="81%" class="unnamed1">
<input type=text name=postcode>
</td>
</tr>
<tr bgcolor="#66CCFF">
<td align=right height=98 valign=center
width="19%" class="unnamed1"><font color="#D73383">*发表文章:</font></td>
<td height=98 valign=top width="81%" class="unnamed1" bgcolor="#66CCFF">
<textarea cols=60 name=content rows=9></textarea>
</td>
</tr>
<tr>
<td align=right bgcolor=#f5f5f5 height=25 valign=center
width="19%" class="unnamed1"><font color="#D73383">相关图片:</font></td>
<td bgcolor=#f5f5f5 height=25 valign=middle width="81%" class="unnamed1">
<input type="file" name="ImageFile" size="50" maxlength="255">
</td>
</tr>
<tr bgcolor="#66CCFF" valign="middle">
<td align=right height=25
width="19%"><font color="#D73383">另附文件:</font></td>
<td height=25 width="81%">
<input type="file" name="DocumentFile" size="50" maxlength="255">
<tr bgcolor="#f5f5f5" valign="middle">
<td align=right height=44
width="19%">  </td>
<td height=44 width="81%">
<table width="40%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="unnamed1">
<div align="center">
<input type="submit" name="Submit" value="提交">
</div>
</td>
<td class="unnamed1">
<div align="center">
<input type="reset" name="Submit2" value="重填">
</div>
</td>
</tr>
</table>
</form>
nhconch 2003-02-19
  • 打赏
  • 举报
回复
适当处理一下MIME邮件头,用mail函数就可以了,建议你搜索一下“MIME”,能找到答案。

给你一个现用的例子:
//sendmail.php
<html>

<head>
<TITLE>--邮件发送</TITLE>
</head>

<BODY leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" text="#666600" link="#009900" alink="#00FF00" vlink="#006600">
<center>
<?php

function getImageInfo($imgPath)
{
$img_info = getimagesize($imgPath);
switch ($img_info[2])
{
case 1:
$imgtype = "gif";
break;
case 2:
$imgtype = "jpeg";
break;
case 3:
$imgtype = "png";
break;
default:
$imgtype = "";
break;
}
$new_img_info
=
array
(
"width" => $img_info[0],
"height"=> $img_info[1],
"type" => $imgtype,
"size" => filesize($imgPath)
);
return $new_img_info;
}

function checkImg($imgPath)
{
if(!file_exists($imgPath))
{
echo "您选择的图像文件不存在<br>";
return 0;
}
else
{
$imgInfo = getImageInfo($imgPath);
if ($imgInfo[type] == "")
{
echo "请选择有效的图像文件<br>";
return 0;
}
}
return $imgInfo;
}

function readFileData($fileName, $fielSize)
{
if ($fp = fopen($fileName,"r"))
{
$buf = fread($fp, $fielSize)
fclose($fp);
return $buf;
}
else
{
echo "文件 $fileName_name 不能读取!<br>";
return 0;
}
}

if ($name=="" || $email=="" || $address=="" || $content=="")
{
?>
<p> </p>
<p>请填写好必需信息后再提交</p>
<p><a href="#" onclick="parent.history.back();return false;">返回</a></p>
<br>
<?php
}
else
{
$headers = "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"---------=aSAWXmGXWuWzjjdTUbgVHfjMdHiVknOQNEnEW\";\r\n";
$headers .= " charset=\"gb2312\"\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "From: ".$email."\r\n";
//$headers .= "Cc: \r\n";

$bodytxt = "-----------=aSAWXmGXWuWzjjdTUbgVHfjMdHiVknOQNEnEW\r\n";
//$bodytxt .= "Content-Type: text/plain\r\n";
$bodytxt .= "Content-Type: text/html\r\n";
$bodytxt .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$bodytxt .= $body."\r\n\r\n<html><head>";
$bodytxt .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\"></head>\r\n";
$bodytxt .= "<body><table border=0 width=410 height=275 bordercolorlight=#0080FF cellspacing=1 cellpadding=0";
$bodytxt .= "bordercolordark=#0080FF bordercolor=#0080FF><tr><td width=410 height=35 ";
$bodytxt .= "colspan=2 bgcolor=#0080FF><p align=center><font color=#FFFFFF>外来工专栏</font>";
$bodytxt .= "</td></tr><tr><td align=center>姓名:</td><td>".$name."</td></tr><tr>";
$bodytxt .= "<td align=center>性别:</td><td>".$sex."</td></tr><tr><td align=center>";
$bodytxt .= "年龄:</td><td>".$age."</td></tr><tr><td align=center>籍贯:</td><td>".$native;
$bodytxt .= "</td></tr><tr><td align=center>政治面貌:</td><td>".$polity."</td></tr><tr>";
$bodytxt .= "<td align=center>所在单位:</td><td>".$company."</td></tr><tr><td align=center>";
$bodytxt .= "从事职务:</td><td>".$job."</td></tr><tr><td align=center>email:</td><td>".$email;
$bodytxt .= "</td></tr><tr><td align=center>联系地址:</td><td>".$address."</td></tr><tr>";
$bodytxt .= "<td align=center>邮政编码:</td><td>".$postcode."</td></tr><tr><td align=center>";
$bodytxt .= " 文章内容:</td><td>".htmlspecialchars($content)."</td></tr><tr><td align=center>相关图片:</td><td>";
$bodytxt .= $ImageFile_name."</td></tr></table></body></html>\r\n\r\n";

//相关图片
if ($ImageFile!="" && $ImageFile!="none" && ($img_info = checkImg($ImageFile)) && ($imgBuffer = readFileData($ImageFile, $ImageFile_size)))
{
$bodytxt .= "-----------=aSAWXmGXWuWzjjdTUbgVHfjMdHiVknOQNEnEW\r\n";
$bodytxt .= "Content-Type: image/".$img_info[type].";\r\n";
$bodytxt .= " name=\"".$ImageFile_name."\"\r\n";
$bodytxt .= "Content-Transfer-Encoding: base64\r\n";
$bodytxt .= "Content-Disposition: attachment;\r\n";
$bodytxt .= " filename=\"".$ImageFile_name."\"\r\n\r\n";
$bodytxt .= base64_encode($imgBuffer)."\r\n";
}

//另附文本文件
if ($DocumentFile!="" && $DocumentFile!="none" && ($docBuffer = readFileData($DocumentFile, $DocumentFile_size)))
{
$bodytxt .= "-----------=aSAWXmGXWuWzjjdTUbgVHfjMdHiVknOQNEnEW\r\n";
$bodytxt .= "Content-Type: application/octet-stream;\r\n";
$bodytxt .= " name=\"".$DocumentFile_name."\"\r\n";
$bodytxt .= "Content-Transfer-Encoding: base64\r\n";
$bodytxt .= "Content-Disposition: attachment;\r\n";
$bodytxt .= " filename=\"".$DocumentFile_name."\"\r\n\r\n";
$bodytxt .= base64_encode($docBuffer)."\r\n";
}
//*/
$bodytxt .= "-----------=aSAWXmGXWuWzjjdTUbgVHfjMdHiVknOQNEnEW--\r\n\r\n";
if (mail("your@host.net", $name." 投稿", str_replace('"' '"' $bodytxt), $headers)) //请修改成你自己的邮箱
{
?>
<p> </p>
<p>邮件已送出.请 <a href="index.htm">返回首页<a></p>
<br>
<?php
}
else
{
?>
<p> </p>
<p>邮件发送失败</p>
<p><a href="#" onclick="parent.history.back();return false;">回上一步</a></p>
<br>
<?php } ?>
</center>
</body>

</html>
<?php } ?>
liugmwx 2003-02-19
  • 打赏
  • 举报
回复
liugmwx007@sina.com
谢谢
coffee_cn 2003-02-19
  • 打赏
  • 举报
回复
发送附件一般先要把附件上传到服务器,然后发送邮件!
bombshell 2003-02-19
  • 打赏
  • 举报
回复
有个类很好用,它可以实现发送附件。如果需要留下你的eamil。
billgate2000 2003-02-19
  • 打赏
  • 举报
回复
解决了,谢谢!

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧