Reputation: 428
Morning, I want to send email via mutt with attachment list from text file.
Here is my code :
#!/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
I still can't attach file inside attachment.txt I've try with FOR LOOP, it has same result. How should I do?
Upvotes: 1
Views: 246
Reputation: 75548
You should place your variables around quotes to prevent word splitting. It causes a single argument to become two or more:
mutt -e "set content_type=text/html" "$recp" -s "$subj" -- "$ls" < /home/lazuardi/00000000042
And I'm not sure about reading input from a directory?
/home/lazuardi/00000000042
Assignments here don't have meaning as well:
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)
Try this one:
#!/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
Which if Bash version is 4.0+ can be simplified to:
#!/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
Upvotes: 1