关于用php发送有附件的邮件,附件无法打开(急!!! 在线等)

cccca 2004-05-03 07:11:08
这段code是我参考网上的写的,关于php发送email的教学好像都差不多,类似的问题也看到人家问过,不过没有解答。 或者就是两大段class, 看的不是很明白。

最好是希望可以在这个code的基础上稍作改动。
ps: 我也参考过自己outlook收发有附件的email的code,好像在$header,$message上没有很大问题,所以我个人意见是不是$read出错? 不过这个几乎就是照网上抄的。

$attachment = "...path for....1.pdf";
$fp = fopen($attachment, "r");
$read = fread($fp, filesize($attachment));
$read = base64_encode($read);
$read = chunk_split($read);
//fclose($fp); 这句是否必要??


$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: XXX<XXX@XXX.XX>\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"4094d055be8ab\"";

$message = "--4094d055be8ab
Content-type: text/html;
Content-transfer-encoding: 8bit;
$message

--4094d055be8ab
Content-Type: application/pdf;
name=\"1.pdf\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=\"1.pdf\"

$read

--4094d055be8ab--";

收到的email有附件,大小也对,就是无法打开。
出错信息:could not open, because it is either not a supported file type or because the file has been corrupted (for example, it was sent as an email attachment and wasn't correctly decoded)

试过jpg的,也是无法打开。文件大小差不多。

请各位高手帮忙,先谢了。
...全文
293 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
unixdotnet 2004-06-17
  • 打赏
  • 举报
回复
不能依赖mail()
wen8u8 2004-06-17
  • 打赏
  • 举报
回复
学习中
hlddn 2004-05-14
  • 打赏
  • 举报
回复
下面是唠叨老大的一个发附件的脚本.

<?php
/*
$to 目标
$subject 主题
$message 正文
$from 发自
$content_type 类型
$attache 附件,文件名放在数组中
*/
$email = $_POST['email'];
$email = trim("$email");

$message = "hello";

function mail2( $to, $subject, $message, $from, $content_type, $attache="") {
if(!empty($from)) $head = "From: $from\n";
if(empty($content_type)) $content_type = "text/plain";

if(is_array($attache)) {
$boundary = "===" . md5(uniqid("")) . "===";
$head .= "Mime-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"";
$head .= "$boundary\"\n\nThis is a multi-part message in MIME format.\n\n";
$head .= "--$boundary\n";
$head .= "Content-Type: $content_type\n";
$head .= "\n$message\n\n";

while(list($key, $val) = each($attache)) {
$fd = fopen("$val", "r") or die("unable to open file $val");
$contents = chunk_split(base64_encode(fread($fd,filesize("$val"))));
fclose($fd);
$head .= "--$boundary\n";
$head .= "Content-Type: application/octet-stream; name=\"".basename($val);
$head .= "\"\nContent-Transfer-Encoding: BASE64\n";
$head .= "Content-Disposition: attachment; filename=\"".basename($val);
$head .= "\"\n\n" . $contents . "\n\n";
}
$head .= "--" . $boundary . "--\n\n";
}else{
if(!empty($content_type)) {
$head .= "Content-Type: $content_type\n";
$head .= "\n$message\n";
}
}
return mail($to,$subject,"",$head );
}



mail2("$email","",$message,"","",array("a.doc"));

?>
cccca 2004-05-03
  • 打赏
  • 举报
回复
谢谢,不过好像还是老问题。

hahawen帮偶改的是不是:
1。
boundary=\"4094d055be8ab\"\r\n\r\n";
加上\r\n\r\n以后无变化。
如果和上一句前换行的话,就无法正确显示html格式的邮件和pdf附件。全部都是code。
2。
另外$message前换了行,也没有帮助。
hahawen 2004-05-03
  • 打赏
  • 举报
回复
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: XXX<XXX@XXX.XX>\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"4094d055be8ab\"\r\n\r\n";

$message = "--4094d055be8ab
Content-type: text/html;
Content-transfer-encoding: 8bit;

$message

--4094d055be8ab
Content-Type: application/pdf;
name=\"1.pdf\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=\"1.pdf\"

$read

--4094d055be8ab--";
cccca 2004-05-03
  • 打赏
  • 举报
回复
我没有foxmail,我都是用ultraedit打开的。看不到\r\n,除了foxmail还有其他方法看到\r\n吗?
之前我也有考虑过这个问题,并在每行后面都加上\r\n,有时候反而让pdf不按照附件方式显示了。
请问是在每一行后面都加吗?还有分号是不是每行都要加?
hahawen 2004-05-03
  • 打赏
  • 举报
回复
邮件的格式不对,你用foxmail打开一个邮件,看看他的源码.

你的邮件的“\r\n”加的不全,head和body间的就不全。
cccca 2004-05-03
  • 打赏
  • 举报
回复
http://www.yesky.com/SoftChannel/72342371945349120/20020510/1610577.shtml
http://geo.jlu.edu.cn/date/mypost/show.php?id=32

是偶参考的教程,在网上其他地方找到的也是差不多的。
cccca 2004-05-03
  • 打赏
  • 举报
回复
其他就是email的内容而已。

$message定义的是一个html的邮件,我就是想再同时发送一个到两个pdf文件,文件的位置是在网上的,我是在$attachment里给出的,能够认到差不多的大小,应该是找到这个文件了。

再来就是发送,用的是 mail ($email, $subject, $message, $headers);


请各位帮忙了。
allkill 2004-05-03
  • 打赏
  • 举报
回复
把其他代码贴出来Look Look!

21,887

社区成员

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

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