Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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
share|improve this answer
    
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 Feb 2 '12 at 1:39
    
Thanks Gilles, works a treat. I never knew about the - convention before, will remember that in future. –  Kev Feb 2 '12 at 1:45
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
-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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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