2016-06-27 63 views
1

我正在尋找使用bashncat的解決方案。從this question on ServerFault開始,我已經重新編制了它,mostly this answer,非常基本;允許包含一個CC收件人,使用ncat而不是netcat,並將其清理乾淨以便使用(簡單的東西)。正如在這裏看到:使用ncat/netcat/telnet發送多個base64電子郵件附件

# Email Recipient 
TOEMAIL="$REPORTRCPT"; 
# Default Subject 
SUBJECT="Build report for $MACHINEFQDN - $DATE @ $DATETIME (PST)"; 
# Default Contents 
MSGBODY="See attachment. Includes all output as seen while running the script."; 
# Default Attachment 
ATTACHMENT="/tmp/$MACHINENAME-build-$DATE.html" 
ATTACHMENT2="/tmp/40_custom-$MACHINENAME-$DATE" 
# Default smtp server 
mailserver="111.111.111.112" 
mailserverPort="25" 

fappend() { 
    echo "$2">>$1; 
} 

domain=`grep search /etc/resolv.conf | awk '{print $2;}'` 
computer=`hostname` 
user=`whoami` 
FREMAIL="[email protected]$computer.$domain" 

MIMETYPE=`file --mime-type -b $ATTACHMENT` 
MIMETYPE2=`file --mime-type -b $ATTACHMENT2` 
TMP="/tmp/tmpmail_"`date +%N`; 
BOUNDARY=`date +%s|md5sum|awk '{print $1;}'` 
FILENAME=`basename $ATTACHMENT` 
FILENAME2=`basename $ATTACHMENT2` 

DATA=`cat $ATTACHMENT|base64` 
DATA2=`cat $ATTACHMENT2|base64` 

rm $TMP 2> /dev/null 

fappend $TMP "EHLO $computer.$domain" 
fappend $TMP "MAIL FROM:<$FREMAIL>" 
fappend $TMP "RCPT TO:<$TOEMAIL>" 
fappend $TMP "RCPT TO:<$REPORTRCPTCC>" 
fappend $TMP "DATA" 
fappend $TMP "From: $FREMAIL" 
fappend $TMP "To: $TOEMAIL" 
fappend $TMP "Cc: $REPORTRCPTCC" 
fappend $TMP "Reply-To: $FREMAIL" 
fappend $TMP "Subject: $SUBJECT" 
fappend $TMP "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"" 
fappend $TMP "" 
fappend $TMP "This is a MIME formatted message. If you see this text it means that your" 
fappend $TMP "email software does not support MIME formatted messages." 
fappend $TMP "" 
fappend $TMP "--$BOUNDARY" 
fappend $TMP "Content-Type: text/plain; charset=UTF-8; format=flowed" 
fappend $TMP "Content-Disposition: inline" 
fappend $TMP "" 
fappend $TMP "$MSGBODY" 
fappend $TMP "" 
fappend $TMP "" 
fappend $TMP "--$BOUNDARY" 
fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\"" 
fappend $TMP "Content-Transfer-Encoding: base64" 
fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";" 
fappend $TMP "" 
fappend $TMP "$DATA" 
fappend $TMP "" 
fappend $TMP "" 
fappend $TMP "--$BOUNDARY--" 
fappend $TMP "" 
fappend $TMP "." 
fappend $TMP "quit" 

ncat $mailserver $mailserverPort < $TMP >> $TMP 
rc="$?" 
if [ "$rc" -ne "0" ]; then 
    echo "Returncode: $rc" 
    echo "Please inspect $TMP" 
else 
    echosuccess "Email successfully sent, you should receive it!" 
    rm $TMP; 
fi 

但我一直在不斷地嘗試中欺騙它是多個base64附件。我已經嘗試了各種各樣的東西位置,BOUNDARY等等,添加了其他變量,如上所示。但是我空了。任何熟悉並能夠伸出援助之手的人?我希望這會像添加CC收件人一樣簡單直接。

回答

0

原則上,您的腳本適用於我(經過一些調整,如將ncat替換爲telnet)。你應該遵循它的建議和「請檢查$ TMP」。但是,您不應該使用< $TMP >> $TMP將診斷輸出附加到輸入;更好地使用單獨的日誌輸出文件。另外,方法

DATA=`cat $ATTACHMENT|base64` 

只有在編碼附件對於命令行不太長的情況下才起作用。你可以用

base64 $ATTACHMENT >>$TMP 

更換

fappend $TMP "$DATA" 

克服這個門檻。