Chinaunix
标题:
sendmail发附件时遇到问题,打不开发的tar包附件,用php写的,附源码
[打印本页]
作者:
hjxisking
时间:
2006-09-26 14:27
标题:
sendmail发附件时遇到问题,打不开发的tar包附件,用php写的,附源码
///////////////////////////////////////////////////////////////
//文本内容
$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 .= "From
from\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包的损坏?
如何解决这个问题呢?
大家帮帮忙,急!!!
作者:
abel
时间:
2006-09-26 16:01
我沒用過 php 寫過,或許你的問題出現在 UTF8 也不一定
下面是我用 shell + perl 的方法供您參考看看
#!/bin/sh
# program Subject To Attachment
hostname=`hostname`
From=$LOGNAME@$hostname
echo $From
SUBJ=$1
TO=$2
FILE=$3
ts=`date +%s`
base64_data=`perl -e ' use MIME::Base64 qw(encode_base64); local($/) = undef; print encode_base64(<STDIN>);' < $FILE`
domain_part=`echo $TO | cut -f 2 -d'@'`
mxhost=`host -t mx $domain_part | cut -f 7 -d ' '`
[ -z "$mxhost" ] && host=$domain_part
[ -n "$mxhost" ] && host=$mxhost
echo "$SUBJ $TO $FILE $host"
echo $host
cat << EOF | nc $host 25
EHLO $hostname
$(sleep 1)MAIL FROM: <$From>
$(sleep 1)RCPT TO: <$TO>
$(sleep 1)DATA
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0590_01C5EEB7.B745FBD0"
Subject=$SUBJ
This is a multi-part message in MIME format.
------=_NextPart_000_0590_01C5EEB7.B745FBD0
Content-Type: text/plain;
charset="big5"
Content-Transfer-Encoding: 7bit
see the detail in attachment
------=_NextPart_000_0590_01C5EEB7.B745FBD0
Content-Type: application/octet-stream; name="$FILE"
name="$FILE"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$FILE"
$base64_data
------=_NextPart_000_0590_01C5EEB7.B745FBD0--
.
EOF
复制代码
作者:
hjxisking
时间:
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具体起到了什么作用?
作者:
abel
时间:
2006-09-26 16:20
1. 我認為用 application/octet-stream 沒有錯,因為不用去計較原來的文件格式,什麼文件都可以發,連 tar 這個程式都可以發
2. 何著為主我不知道,沒有注意過,不過這兩個東西在 OE 基本上都是相等的
3. RFC 2183 自修一下
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2