sending mail from command line: Null message body

0

I'm somehow unable to get my head around this. I am sending mails from commandline with help of a little script. But the line

echo 'LOREM IPSUM' | mail -s 'SUBJECT' -a 'From:TEST' < /root/recipients.txt

somehow seems to be wrong. The mail gets sent but without any text in it and I get the error

mail: Null message body; hope that's ok

What am I doing wrong here?

farosch

Posted 2017-01-13T10:38:56.337

Reputation: 355

Answers

1

You're trying to redirect mail's stdin from two sources at once:

  1. First you have echo | mail, which binds echo's stdout to mail's stdin (via pipe), replacing the default stdin (terminal);
  2. Then you have mail < recipients.txt, which binds the file to mail's stdin, replacing any previous redirections.

The important bit being, it does not combine both inputs. If you need to do that, use some combination of cat and command grouping:

  • (cat recipients.txt; echo 'Hello world') | mail -s ...

  • bash-only: cat recipients.txt <(echo 'Hello world') | mail -s ...

Although in this case it might be better to pass the recipient list as command-line arguments instead of input:

  • echo 'Hello world' | mail -s ... $(cat recipients.txt)

user1686

Posted 2017-01-13T10:38:56.337

Reputation: 283 655