PHP发送"附件"邮件的方法?

axiebin 2010-11-07 10:00:31
网上扒了一个加附件发送邮件的方法代码?反复修改,总是发送失败,贴上来请大家研究研究??

<html>
<body>
<form method="post" name="sndml" action="2.php" ENCTYPE="multipart/form-data">
<table>
<tr ><td>邮件标题:</td>
<td><input type="text" name="subject" ></td>
</tr>
<tr ><td>发送者:</td>
<td><input type="text" name="from" ></td>
</tr>
<tr ><td>接受者:</td>
<td><input type="text" name="to" ></td>
</tr>
<tr ><td>下载提示:</td>
<td><input type="text" name="text" ></td>
</tr>
<tr ><td>源数据文件:</td>
<td><input type="file" name="upload_file" size="40"></td>
</tr>
<tr><td> </td>
<td><input type="submit" value="确定">
</td>
</tr>
</table>
</form>
</body>
</html>


<?php
//文本内容
$text = $_POST['text'];
//标题
$subject = $_POST['subject'];
//发送者
$from = $_POST['from'];
//接受者
$to = $_POST['to'];

//附件
$file = $_FILES['upload_file']['tmp_name'];
// 定义分界线
$boundary = uniqid( "");
$headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";
$headers .= "From:$from\r\n";
//确定上传文件的MIME类型
if($_FILES['upload_file']['type'])
$mimeType = $_FILES['upload_file']['type'];
else
$mimeType ="application/unknown";
//文件名
$fileName = $_FILES['upload_file']['name'];

// 打开文件
$fp = fopen($file, "r");
// 把整个文件读入一个变量
$read = fread($fp, filesize($file));
//我们用base64方法把它编码
$read = base64_encode($read);
//把这个长字符串切成由每行76个字符组成的小块
$read = chunk_split($read);
//现在我们可以建立邮件的主体
$body = "--$boundary
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit
$text
--$boundary
Content-type: $mimeType; name=$fileName
Content-disposition: attachment; filename=$fileName
Content-transfer-encoding: base64
$read
--$boundary--";
//发送邮件
if(mail($to, $subject,$body,$headers))
print "OK! the mail $from --- $to has been send<br>";
else
print "fail to send mail <br>";
?>
...全文
94 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LuciferStar 2010-11-10
  • 打赏
  • 举报
回复
是什么系统?linux下是否安装了sendmail?
windows下IIS里是否安装了SMTP?PHP.INI里是否设置了相关数据?
如果你mail之后直接得到发送失败,就是设置问题了。
LuciferStar 2010-11-08
  • 打赏
  • 举报
回复
$pdfname="test.pdf";
$email="test@test.com";
$text = "您好,附件中是您需要的PDF文件。请点击下载。<br><a href=http://www.singlestudio.net.cn>www.singlestudio.net.cn</a>"; //文本内容

$text = base64_encode($text); //用base64方法把它编码
$text = chunk_split($text); //把这个长字符串切成由每行76个字符组成的小块

$subject = $pdfname; //标题
$from = "admin@singlestudio.net.cn"; //发送者
$to = $email; //接受者

//附件
// 定义分界线
$boundary = "NextPart_".uniqid("");
$boundary2 = "NextPart_".uniqid("");
$headers = "To: $to\r\n";
$headers .= "From: $from\r\n";
$headers .="Mime-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;
boundary=\"----=_$boundary\"\r\n";

$read=file_get_contents($pdfname);

$read = base64_encode($read); //用base64方法把它编码
$read = chunk_split($read); //把这个长字符串切成由每行76个字符组成的小块

//现在我们可以建立邮件的主体
$body = "This is a multi-part message in MIME format.

------=_$boundary
Content-Type: multipart/alternative;
boundary=\"----=_$boundary2\";

------=_$boundary2
Content-Type: text/html;
charset=\"gbk\"
Content-Transfer-Encoding: base64

$text

------=_$boundary2--

------=_$boundary
Content-Type: application/octet-stream;
charset=\"GBK\";
name=\"$pdfname\"
Content-Disposition: attachment; filename=\"$pdfname\"
Content-Transfer-Encoding: base64

$read

-------=_$boundary--";

if(mail($to, $subject,$body,$headers))
echo "您需要的PDF文件(".$pdfname.")已经发往您的邮箱:".$to."。<br>请查收。";
else
echo "抱歉,发送失败了。<br>";
LuciferStar 2010-11-08
  • 打赏
  • 举报
回复
你的boundary都一样,所以失败了。
body一个boundary,附件一个boundary,要区别开。
axiebin 2010-11-08
  • 打赏
  • 举报
回复
回复2楼:我照着您的方式写了一遍,还是发送不出去
HTML 还是和上面的一样,但是还是发送失败?,麻烦了.....

<?php
$pdfname = $_POST['upload_file'];
$email= $_POST['to'];
$text = $_POST['text']; //文本内容

$text = base64_encode($text); //用base64方法把它编码
$text = chunk_split($text); //把这个长字符串切成由每行76个字符组成的小块

$subject = $_POST['subject']; //标题
$from = $_POST['from']; //发送者
$to = $email; //接受者

//附件 // 定义分界线
$boundary = "NextPart_".uniqid("");
$boundary2 = "NextPart_".uniqid("");
$headers = "To: $to\r\n";
$headers .= "From: $from\r\n";
$headers .="Mime-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;
boundary=\"----=_$boundary\"\r\n";

$read=file_get_contents($pdfname);

$read = base64_encode($read); //用base64方法把它编码
$read = chunk_split($read); //把这个长字符串切成由每行76个字符组成的小块 //现在我们可以建立邮件的主体
$body = "This is a multi-part message in MIME format.
------=_$boundary
Content-Type: multipart/alternative;
boundary=\"----=_$boundary2\";


------=_$boundary2
Content-Type: text/html;
charset=\"gbk\"
Content-Transfer-Encoding: base64


$text


------=_$boundary2--


------=_$boundary
Content-Type: application/octet-stream;
charset=\"GBK\";
name=\"$pdfname\"
Content-Disposition: attachment; filename=\"$pdfname\"
Content-Transfer-Encoding: base64

$read

-------=_$boundary--";

if(mail($to,$subject,$body,$headers))
echo "您需要的PDF文件(".$pdfname.")已经发往您的邮箱:".$to."。<br>请查收。";
else
echo "抱歉,发送失败了。<br>";
?>

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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