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.


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

  • What's does the middle standalone - do? – Kev Feb 2 '12 at 1: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 2 '12 at 1:39
  • 1
    Thanks Gilles, works a treat. I never knew about the - convention before, will remember that in future. – Kev Feb 2 '12 at 1:45

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

  • -1 As per the linked server fault post, that doesn't work. – Kev Feb 1 '12 at 22:14
  • 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 4 '14 at 11:19

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.

  • 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 2 '18 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.