免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2081 | 回复: 3

[Mail] sendmail发附件时遇到问题,打不开发的tar包附件,用php写的,附源码 [复制链接]

论坛徽章:
0
发表于 2006-09-26 14:27 |显示全部楼层
///////////////////////////////////////////////////////////////
//文本内容
$text = "\n系统日志报告\n";
//标题
$subject = "=?UTF-8?B?".base64_encode("系统日报"."?=";
//发送者
$from = "nobody@streamfount";
//接受者
$to = "hjxisking@gmail.com";
//附件
$day_name = date("Ymd".".tar";
$file = "/tmp/".$day_name;
// 定义分界线
$boundary = uniqid( "";
$headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";
$headers .= "Fromfrom\r\n";
//确定上传文件的MIME类型
//if($_FILES['upload_file']['type'])
$mimeType = "application/x-tar";//$_FILES['upload_file']['type'];
//else
//$mimeType ="application/unknown";
//文件名
$fileName = "=?UTF-8?B?".base64_encode("日报"."?=";

// 打开文件
$fp = fopen($file, "rb";
// 把整个文件读入一个变量
$read = fread($fp, filesize($file));
//我们用base64方法把它编码
$read = base64_encode($read);
//把这个长字符串切成由每行76个字符组成的小块
$read = chunk_split($read);
//现在我们可以建立邮件的主体
$body = "--$boundary
Content-type: text/plain; charset=utf-8;
Content-transfer-encoding: 8bit
$text
--$boundary
Content-type: $mimeType; name=$day_name;
Content-disposition: attachment; filename=$day_name
Content-transfer-encoding: base64
$read
--$boundary--";
//echo $body;
//发送邮件
//mb_language(’Neutral’);
//mb_internal_encoding(”UTF-8″);
//mb_http_input(”UTF-8″);
//mb_http_output(”UTF-8″);
if(mail($to, $subject,$body,$headers))
print "OK! the mail $from --- $to has been send<br>";
else
print "fail to send mail <br>";

利用sendmail默认配置,能够顺利发信,附件为一个tar包,收到后,打开tar包出错,winrar说tar包损坏
如果我发的是一个文本文件,很奇怪,发现文本文件最头上会少掉1到2行字,我猜会不会是这个原因引起了tar包的损坏?
如何解决这个问题呢?
大家帮帮忙,急!!!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2006-09-26 16:01 |显示全部楼层
我沒用過 php 寫過,或許你的問題出現在 UTF8 也不一定
下面是我用 shell + perl 的方法供您參考看看

  1. #!/bin/sh

  2. # program Subject To Attachment
  3. hostname=`hostname`
  4. From=$LOGNAME@$hostname
  5. echo $From
  6. SUBJ=$1
  7. TO=$2
  8. FILE=$3
  9. ts=`date +%s`
  10. base64_data=`perl -e ' use MIME::Base64 qw(encode_base64); local($/) = undef; print encode_base64(<STDIN>);' < $FILE`

  11. domain_part=`echo $TO | cut -f 2 -d'@'`
  12. mxhost=`host -t mx $domain_part | cut -f 7 -d ' '`

  13. [ -z "$mxhost" ] && host=$domain_part
  14. [ -n "$mxhost" ] && host=$mxhost

  15. echo "$SUBJ $TO $FILE $host"

  16. echo $host
  17. cat << EOF | nc $host 25
  18. EHLO $hostname
  19. $(sleep 1)MAIL FROM: <$From>
  20. $(sleep 1)RCPT TO: <$TO>
  21. $(sleep 1)DATA
  22. MIME-Version: 1.0
  23. Content-Type: multipart/mixed;
  24.         boundary="----=_NextPart_000_0590_01C5EEB7.B745FBD0"
  25. Subject=$SUBJ

  26. This is a multi-part message in MIME format.

  27. ------=_NextPart_000_0590_01C5EEB7.B745FBD0
  28. Content-Type: text/plain;
  29.         charset="big5"
  30. Content-Transfer-Encoding: 7bit

  31. see the detail in attachment
  32. ------=_NextPart_000_0590_01C5EEB7.B745FBD0
  33. Content-Type: application/octet-stream; name="$FILE"
  34.         name="$FILE"
  35. Content-Transfer-Encoding: base64
  36. Content-Disposition: attachment;
  37.         filename="$FILE"

  38. $base64_data

  39. ------=_NextPart_000_0590_01C5EEB7.B745FBD0--
  40. .
  41. EOF


复制代码

论坛徽章:
0
发表于 2006-09-26 16:12 |显示全部楼层
请教点问题
1。application/octet-stream和application/x-tar使用上有什么区别?我发的是个tar包,不是用application/x-tar?
2。打开邮件看到的附件名字是由Content-Type: application/octet-stream; name="$FILE"定的还是由Content-Disposition: attachment;filename="$FILE"确定的?
3。Content-Disposition具体起到了什么作用?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2006-09-26 16:20 |显示全部楼层
1. 我認為用 application/octet-stream 沒有錯,因為不用去計較原來的文件格式,什麼文件都可以發,連 tar 這個程式都可以發
2. 何著為主我不知道,沒有注意過,不過這兩個東西在 OE 基本上都是相等的
3. RFC 2183 自修一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP