透過使用find
命令我得到了多個文件。現在我想將所有這些文件新增為郵件附件。如何將這些文件作為附件新增至單一郵件?
我想在腳本中實現這個。我是否需要使用 for 循環並將文件儲存在數組變數中?
EX:我透過以下方式得到了 3 個檔案結果
find . -type f -name "sum*"
結果:
sum123.pdf
sum234.pdf
sum453.pdf
答案1
mutt
你可以這樣做:
mutt -a $(find . -type f -name "sum*")
如果你想以非互動方式進行,請嘗試
mutt -s "Subject" -a $(find . -type f -name "sum*") -- [email protected] < /dev/null
如果mutt
沒有安裝,這裡mail
是一個包含更多工具的範例(例如mpack
)!
所以它應該是這樣的
#!/bin/bash
# This needs heirloom-mailx
from="[email protected]"
to="[email protected]"
subject="Some fancy title"
body="This is the body of our email"
declare -a attargs
for att in $(find . -type f -name "sum*"); do
attargs+=( "-a" "$att" )
done
mail -s "$subject" -r "$from" "${attargs[@]}" "$to" <<< "$body"
對於沒有聲明的 sh 環境:
#!/bin/sh
# This needs heirloom-mailx
from="[email protected]"
to="[email protected]"
subject="Some fancy title"
body="This is the body of our email"
attargs=""
for att in $(find . -type f -name "sum*"); do
attargs="${attargs}-a $att "
done
attargs=${attargs::-1}
mail -s "$subject" -r "$from" ${attargs[@]} "$to" <<< "$body"
答案2
ATTACH_FILE=`ls $HOME/data/*log.txt`
rmdat $HOME/file.dat
rmdat $HOME/sendemail.dat
ATTACH_FILE="$(echo $ATTACH_FILE | sed 's/ /\\n/g')"
export FILE=$HOME/file.dat
export FILE1=$HOME/sendemail.dat
echo $ATTACH_FILE >> $FILE
ATT_FILES=""
while read BP_fl
do
ATT_FILES=$ATT_FILES" uuencode $BP_fl $(basename $BP_fl) ;"
done < $HOM/file.dat
echo '( echo "Hi" ; ' >> $FILE1
echo $ATT_FILES >> $FILE1
echo ') | mailx -m -s " automation test email" [email protected] ' >> $FILE1
chmod 777 $FILE1
. $FILE1