来自文件的多个附件

时间:2014-08-15 02:46:04

标签: bash mutt

早上,我想通过mutt发送电子邮件,附件列表来自文本文件。

这是我的代码:

#!/bin/bash
subj=$(cat /home/lazuardi/00000000042/subject.txt)
attc=$(find /home/lazuardi/00000000042 -name "*.*" | grep -v body.txt | grep -v email.txt | grep -v subject.txt | grep -v body.html > attachment.txt)
ls=$(for x in $attc; do read; done)
while read recp; do
    while read ls; do
        mutt -e "set content_type=text/html" $recp -s "$subj" -- $ls < /home/lazuardi/00000000042
    done < /home/lazuardi/attachment.txt
done < /home/lazuardi/00000000042/email.txt 

我仍然无法在attachment.txt中附加文件 我尝试使用FOR LOOP,它有相同的结果。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

您应该将变量放在引号周围以防止分词。它会导致单个参数变为两个或更多:

mutt -e "set content_type=text/html" "$recp" -s "$subj" -- "$ls" < /home/lazuardi/00000000042

我不确定从目录中读取输入?

/home/lazuardi/00000000042

此处的作业也没有意义:

attc=$(find /home/lazuardi/00000000042 -name "*.*" | grep -v body.txt | grep -v email.txt | grep -v subject.txt | grep -v body.html > attachment.txt)
ls=$(for x in $attc; do read; done)

试试这个:

#!/bin/bash

subj=$(</home/lazuardi/00000000042/subject.txt)

attachments=()
while IFS= read -r file; do
    attachments+=("$file")
done < <(exec find /home/lazuardi/00000000042 -name "*.*" | grep -v -e body.txt -e email.txt -e subject.txt -e body.html)

echo '---- Attachments ----'
printf '%s\n' "${attachments[@]}"
echo

recipients=()
while read recp; do
    recipients+=("$recp")
done < /home/lazuardi/00000000042/email.txt

echo '---- Recipients ----'
printf '%s\n' "${recipients[@]}"
echo

for recp in "${recipients[@]}"; do
    for attachment in "${attachments[@]}"; do
        echo "Sending content to $recp with subject $subj and attachment $attachment."
        mutt -e "set content_type=text/html" "$recp" -s "$subj" -- "$attachment" < /home/lazuardi/00000000042/body.txt
    done
done

如果Bash版本为4.0+,可以简化为:

#!/bin/bash

subj=$(</home/lazuardi/00000000042/subject.txt)

readarray -t attachments \
    < <(exec find /home/lazuardi/00000000042 -name "*.*" | grep -v -e body.txt -e email.txt -e subject.txt -e body.html)

echo '---- Attachments ----'
printf '%s\n' "${attachments[@]}"
echo

readarray -t recipients < /home/lazuardi/00000000042/email.txt

echo '---- Recipients ----'
printf '%s\n' "${recipients[@]}"
echo

for recp in "${recipients[@]}"; do
    for attachment in "${attachments[@]}"; do
        echo "Sending content to $recp with subject $subj and attachment $attachment."
        mutt -e "set content_type=text/html" "$recp" -s "$subj" -- "$attachment" < /home/lazuardi/00000000042/body.txt
    done
done