1

I am using Ubuntu 18.04 LTS, GNU Mailutils 3.4 and MSMTP 1.6.6 to send an e-mail, containing an attachment, from a Bash script (and/or testing from the command line). I was using BSD-Mailx when the server was running 16.04, but upgrading to 18.04 caused Mailx to not be able to send attachments.

I have tried multiple formats of the mail command in order to pass text to the body of the e-mail, yet they all seem to fail. Some examples:

echo "This is the body of the e-mail" | mail address@example.com -s "This is the subject" -A /file/path/file.txt

All I get is the attached file with an empty e-mail.

mail address@example.com -s "This is the subject" -A /file/path/file.txt <<< echo "This is the body of the e-mail"

Again, empty e-mail with the attachment.

I have also tried it with the e-mail address at the end of the command, which still just gives an empty e-mail with the attachment.

I have tried several other iterations of the above, such as a single < redirect, | the text at the end of the command, which of course fail, but just trying to guess at the correct format.

Does anyone else have this figured this out?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
boards188
  • 104
  • 1
  • 8
  • If you run `mail` without redirection, you may see that it prompts for `Cc:`. Perhaps your message body has ended up in that header. – jhnc Mar 16 '19 at 17:50
  • I have looked through the headers and don't see any of the text that was provided for the message body. I would think that I would receive an "undeliverable" response if my text was inserted in the CC: line since the text isn't a valid e-mail addres. – boards188 Mar 16 '19 at 18:03
  • 1
    Maybe a duplicate of https://stackoverflow.com/q/17359/7552 -- full disclosure, I have an answer there I'm proud of. – glenn jackman Mar 16 '19 at 22:03

2 Answers2

1

using mailutils

I think the problem is that if you specify -A, stdin is ignored: https://savannah.gnu.org/bugs/?54992

You can include the body text as an additional attachment:

echo "This is the body of the e-mail" |\
mail address@example.com \
    -s "This is the subject" \
    --skip-empty-attachments \
    --content-type text/plain -A - \
    -A /file/path/file.txt

using mutt

Although I don't think mutt is really intended for scripting, it looks like this should work:

echo "this is the body" |\
mutt \
  -s "this is the subject" \
  -a /file/path/file.txt -- \
  address@example.com
jhnc
  • 11,310
  • 1
  • 9
  • 26
  • Yes, you are correct; I didn't think to test without the attachment. Plus, sending the text of the e-mail as an attachment works...but isn't very useful. It is strange to me that this worked before with BSD-Mailx, but apparently an "upgrade" of that application changed the -a/-A function from attaching files to adding headers(?). I was hoping that a switch to Mailutils would do what I needed, but apparently not. And I have tried Mutt, but not as simple as I was hoping. – boards188 Mar 18 '19 at 14:01
  • If you have a multipart mime message, I believe every body part is an attachment. I've updated the answer to avoid the empty first attachment that was being added. – jhnc Mar 18 '19 at 14:27
  • `mime-construct` appears to exist in ports – jhnc Mar 18 '19 at 14:37
0

Thanks to @jhnc I for pointing me to https://savannah.gnu.org/bugs/?54992. I posted my issue there and received a response that this was a bug which has now been fixed in Mailutils 3.5-3 according to this discussion https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=918806#22.

There is a workaround, in the meantime, by adding the --mime attribute, like so:

echo "body text" | /usr/bin/mail --mime -s "some subject" -A "somefile.csv" my@email.com

Apparently, I need some work on my 'Google foo' and Stackoverflow participation. And I hope this is the "right" way to respond to my original question.

boards188
  • 104
  • 1
  • 8