发送邮件时附加文件,即find命令的结果集

发送邮件时附加文件,即find命令的结果集

通过使用find命令我得到了多个文件。现在我想将所有这些文件添加为邮件附件。如何将这些文件作为附件添加到单个邮件中?

我想在脚本中实现这个。我是否需要使用 for 循环并将文件存储在数组变量中?

例如:我通过以下方式获得了 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

相关内容