歡迎您光臨本站 註冊首頁

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包的損壞?
如何解決這個問題呢?
大家幫幫忙,急!!!
《解決方案》

我沒用過 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


《解決方案》

請教點問題
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. 我認為用 application/octet-stream 沒有錯,因為不用去計較原來的文件格式,什麼文件都可以發,連 tar 這個程式都可以發
2. 何著為主我不知道,沒有注意過,不過這兩個東西在 OE 基本上都是相等的
3. RFC 2183 自修一下

[火星人 ] sendmail發附件時遇到問題,打不開發的tar包附件,用php寫的,附源碼已經有290次圍觀

http://coctec.com/docs/service/show-post-45663.html