1

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" -- myself@gmail.com

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?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136

1 Answers1

1

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[@]}" -- myself@gmail.com < 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[@]}" -- myself@gmail.com < 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[@]}" -- myself@gmail.com < mail.txt
chepner
  • 497,756
  • 71
  • 530
  • 681
  • How do I put ls ../*.log inside attachments? – KcFnMi Nov 18 '15 at 19:04
  • `attachments+=( ../*.log )` – chepner Nov 18 '15 at 19:07
  • Does not work @chepner. Only one file is appended and the rest of files some way goes as destination of the e-mail. – KcFnMi Nov 18 '15 at 19:27
  • I'm seeing conflicting man pages about whether `-a` can take multiple arguments or not. Try `attachments+=(-a "$attachment")`, and remove the `-a` from the mutt command line. (http://www.mutt.org/doc/man_page.html says one file only, so that's probably definitive.) – chepner Nov 18 '15 at 19:31
  • This way it correctly send to the destination (files are not being appended as destination). But there is only one file appended. I'm doing this: attachment+=(../*.log) attachments+=(-a "$attachment"), is it correct? – KcFnMi Nov 18 '15 at 19:49