关于用php发送有附件的邮件,附件无法打开(急!!! 在线等)
这段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的,也是无法打开。文件大小差不多。
请各位高手帮忙,先谢了。
问题点数:0、回复次数:10Top
1 楼allkill(傲气游)回复于 2004-05-03 19:59:03 得分 0
把其他代码贴出来Look Look!Top
2 楼cccca()回复于 2004-05-03 21:09:43 得分 0
其他就是email的内容而已。
$message定义的是一个html的邮件,我就是想再同时发送一个到两个pdf文件,文件的位置是在网上的,我是在$attachment里给出的,能够认到差不多的大小,应该是找到这个文件了。
再来就是发送,用的是 mail ($email, $subject, $message, $headers);
请各位帮忙了。Top
3 楼cccca()回复于 2004-05-03 21:12:10 得分 0
http://www.yesky.com/SoftChannel/72342371945349120/20020510/1610577.shtml
http://geo.jlu.edu.cn/date/mypost/show.php?id=32
是偶参考的教程,在网上其他地方找到的也是差不多的。Top
4 楼hahawen(变态的大龄青年)回复于 2004-05-03 23:00:30 得分 0
邮件的格式不对,你用foxmail打开一个邮件,看看他的源码.
你的邮件的“\r\n”加的不全,head和body间的就不全。Top
5 楼cccca()回复于 2004-05-03 23:11:42 得分 0
我没有foxmail,我都是用ultraedit打开的。看不到\r\n,除了foxmail还有其他方法看到\r\n吗?
之前我也有考虑过这个问题,并在每行后面都加上\r\n,有时候反而让pdf不按照附件方式显示了。
请问是在每一行后面都加吗?还有分号是不是每行都要加?Top
6 楼hahawen(变态的大龄青年)回复于 2004-05-03 23:18:37 得分 0
$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--";
Top
7 楼cccca()回复于 2004-05-03 23:33:11 得分 0
谢谢,不过好像还是老问题。
hahawen帮偶改的是不是:
1。
boundary=\"4094d055be8ab\"\r\n\r\n";
加上\r\n\r\n以后无变化。
如果和上一句前换行的话,就无法正确显示html格式的邮件和pdf附件。全部都是code。
2。
另外$message前换了行,也没有帮助。Top
8 楼hlddn(西藏的那片天)回复于 2004-05-14 17:56:22 得分 0
下面是唠叨老大的一个发附件的脚本.
<?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"));
?>
Top
9 楼wen8u8((L,说你最想说的一句话))回复于 2004-06-17 14:57:34 得分 0
学习中Top
10 楼unixdotnet(byopen)回复于 2004-06-17 16:55:47 得分 0
不能依赖mail()Top




