Exim: How to send email (with headers) from command line?

Exim expects to receive a full RFC 5322 / 2822 / 822 email message as input. So you need to give the headers in the form of... headers.

$ (echo "To: foo@bar";
   echo "Reply-To: [email protected]"
   echo "Subject: Hello!";
   echo "";
   echo "My Mail Content") | sendmail foo@bar
$ sendmail foo@bar <<'EOF'
From: "The Great Quux" <baz@quux>
To: "Fred Foobar" <foo@bar>
Reply-To: dev@null
Subject: Hello!
Content-Type: text/plain; charset=utf-8

My Mail Content
EOF

(Not sure if the names in From/To have to be quoted. RFC 5322 seems to say that they should)


exim -i -t <<< 'From: My Self <[email protected]>
To: Recip One <[email protected]>, Recip Two <[email protected]>
Subject: Test message

content line 1
content line 2'

This works for any sendmail-compatible MTA not just exim. If you have sendmail you could run same command but start with "sendmail -i -t...".

Of course you could load the input from a text file instead.

exim -i -t < file/path.txt

Quotes are not needed for label part of email addresses before angle backets.

-i switch prevents dot-line-termination. If you just run "exim -t" and paste or type in the content interactively then skip the -i switch and you can end with line containing just dot then a blank line (don't know why the extra blank line is needed--it didn't used to be).

-t derives recipients from the content you give rather than specifying as command-line parameter. Email agents expect To: field to be set and honor labels, so you should normally send it. If you do send a To: then use -t switch because specifying recipients on command line would be redundant and a source of possible conflict in that case.

Another tactic is to use s-nail's mailx program. With s-nail installed, just add "set mta=/usr/bin/exim" (or /usr/sbin/exim or wherever it resides) to /etc/mail.rc or your personal ~/.mailrc and use mailx like so:

mailx -s 'subject line' 'Recip One <[email protected]>' 'Recip Two <[email protected]>' <<< 'content line 1
content line 2'

or

mailx -s 'subject line' 'Recip One <[email protected]>' 'Recip Two <[email protected]>' < file/path.txt

Mailx has a nice -a switch for adding file attachments. If you're running a very old version of s-nail, there is no 'mta' setting and you need to replace /usr/sbin/sendmail or /usr/bin/sendmail (or similar) with a sym link to your exim binary.