Setting headers using the /bin/mail command

27,180

Solution 1

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

Solution 2

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

Share:
27,180

Related videos on Youtube

user134706
Author by

user134706

###Actively looking for freelance work ###About Me: I'm a professional software developer and have spent my time building provisioning and web based self-service systems for IIS, Apache and Citrix XenServer, amongst other things. My Curriculum Vitae can be viewed on Stack Overflow Careers (might be a bit out of date). Stuff I like to listen to at last.fm You can get in touch here: kevin.e.kenny #@# gmail.com (you know what to do with the # and spaces). No Survey Emails Please. Also not ashamed to admit I like trains, mostly diesels, late Era 8 (BR Sectorisation) and Era 9 onwards :) I'm also interested in signalling if anyone from Network Rail is looking this far down ;)

Updated on September 18, 2022

Comments

  • user134706
    user134706 almost 2 years

    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.

  • user134706
    user134706 over 12 years
    -1 As per the linked server fault post, that doesn't work.
  • user134706
    user134706 over 12 years
    What's does the middle standalone - do?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    @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).
  • user134706
    user134706 over 12 years
    Thanks Gilles, works a treat. I never knew about the - convention before, will remember that in future.
  • DanB
    DanB almost 10 years
    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.
  • Astara
    Astara about 6 years
    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.