7

I have a bash script that I use with nagios that sends notifications by email. The key part of it looks like this:

# $1 (FROM) | $2 (TO) | $3 (SUBJECT) | $4 (BODY)
/usr/bin/printf "%b" "$4" | /bin/mail -s "$3" "$2" -- -f $1

I know I could use the sendmail command directly (like this) but it would mean reworking a few scripts to take care of building the entire message including all headers.

Is there any way to specify additional SMTP headers using /bin/mail, in my own case I am trying to add the Importance: high header?

I'm running exim 4.63 as my MTA and CentOS 5.6 x64.

Kev
  • 1,429
  • 7
  • 21
  • 32

3 Answers3

10

There are many different versions of mail (see Mail vs. mail what is the difference and the Heirloom project's write-up on the different versions of mail).

If you want to keep your sanity, I recommend avoiding any tool whose name is too close to mail. Mutt is a lean text mode mail client which is often available, very flexible, and behaves the same everywhere.

mutt -H - "$2" <<EOF
From: $1
To: $2
Subject: $3
Importance: high

$4
EOF
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • What's does the middle standalone `-` do? – Kev Feb 02 '12 at 01:34
  • @Kev The option `-H` takes an argument, a file to read the mail contents (headers plus body) from. I pass the argument `-`, meaning to read standard input (and not a file called `-`; this is a very common convention in the unix world). – Gilles 'SO- stop being evil' Feb 02 '12 at 01:39
  • 1
    Thanks Gilles, works a treat. I never knew about the `-` convention before, will remember that in future. – Kev Feb 02 '12 at 01:45
1

Yes, use the -a flag, e.g. -a "Importance: high".

Lars Kotthoff
  • 1,204
  • 11
  • 11
  • -1 As per the linked server fault post, that doesn't work. – Kev Feb 01 '12 at 22:14
  • 1
    For Heirloom mailx 12.4 this option is `-a filename` (Attach the given file to the message). Not what I was looking for! – dsummersl Aug 28 '13 at 20:50
  • 1
    The -a option (for adding custom headers) is for bsd-mailx, not by heirloom-mailx. I do not see an equivalent option to add headers for heirloom-mailx. – DanB Aug 04 '14 at 11:19
0

No, there's no way. Header syntax is complicated and tools like mail were designed to limit user exposure to it. To use arbitrary headers you should submit the message using sendmail.

Kyle Jones
  • 14,845
  • 3
  • 40
  • 51
  • saying that there's no way to do something is almost always likely to be incorrect. That said, the mailx command based on Berkeley mail and w/compat w/the POSIX mailx cmd, allows "-S" to set a var on cmdline; where you can set a replacement sendmail, where you code have any language implementing a filter to add custom headers that would then submit the result to sendmail. Unix was designed to provide multiple ways to do things. It would be very unlikely that one of those ways wouldn't do what you want. – Astara Jun 02 '18 at 19:28