KcFnMi
KcFnMi

Reputation: 6171

Tell Mutt to attach files (listed in file)

I'm doing my first steps with Mutt (with msmtp in Slackware 13.1).

I'm already able to send mail with attachments like so:

cat mail.txt | mutt -a "/date.log" -a "/report.sh" -s "subject of message" -- [email protected]

I would like to define the files to be attached in another file and then tell Mutt to read it, something like this:

mutt -? listoffilestoattach.txt

Is it possible? Or there are similar approaches?

Upvotes: 1

Views: 1736

Answers (1)

chepner
chepner

Reputation: 531808

You can populate an array with the list of file names fairly easily, then use that as the argument to -a.

while IFS= read -r attachment; do
    attachments+=( "$attachment" )
done < listoffilestoattach.txt
for f in ../*.log; do
    attachments+=("$f")
done

mutt -s "subject of message" -a "${attachments[@]}" -- [email protected] < mail.txt

If you are using bash 4 or later, you can replace the while loop with the (slightly) more interactive-friendly readarray command:

readarray -t attachments < listoffilestoattach.txt

If, as appears to be the case, mutt requires a single file per -a option, then you'll need something slightly different:

while IFS= read -r attachment; do
    attachments+=( -a "$attachment" )
done < listoffilestoattach.txt
for f in ../*.log; do
    attachments+=( -a "$f")
done

mutt -s "subject of message" "${attachments[@]}" -- [email protected] < mail.txt

Using readarray, try

readarray -t attachments < listoffilestoattach.txt
attachments+=( ../*.log )
for a in "${attachements[@]}"; do
    attach_args+=(-a "$a")
done
mutt -s "subject of message" "${attach_args[@]}" -- [email protected] < mail.txt

Upvotes: 1

Related Questions