There are many different versions of
mail around. When you go beyond
mail -s subject to1@address1 to2@address2 <body (for sending, that's all POSIX guarantees — and even
-s didn't exist in the old days), they tend to have different command line options. Adding an additional header isn't always easy.
mailx implementations, e.g. from
mailutils on Ubuntu or Debian's
bsd-mailx, it's easy, because there's an option for that.
mailx -a 'Content-Type: text/html' -s "Subject" to@address <test.html
With the Heirloom
mailx, there's no convenient way. One possibility to insert arbitrary headers is to set
editheaders=1 and use an external editor (which can be a script).
## Prepare a temporary script that will serve as an editor.
## This script will be passed to ed.
cat <<'EOF' >>"$temp_script"
## Call mailx, and tell it to invoke the editor script
EDITOR="ed -s $temp_script" heirloom-mailx -S editheaders=1 -s "Subject" to@address <<EOF
rm -f "$temp_script"
With a general POSIX
mailx, I don't know how to get at headers.
If you're going to use any
mailx, keep in mind that
- This isn't portable even within a given Linux distribution. For example, both Ubuntu and Debian have several alternatives for
- When composing a message,
mailx treats lines beginning with
~ as commands. If you pipe text into
mail, you need to arrange for this text not to contain lines beginning with
If you're going to install software anyway, you might as well install something more predictable than
mailx. For example, mutt.
cat <<'EOF' test.html | mutt -H -
Or you can invoke
sendmail directly. There are several versions of
sendmail out there, but they all support
sendmail -t to send a mail in the simplest fashion, reading the list of recipients from the mail. (I think they don't all support
Bcc:.) On most systems,
sendmail isn't in the usual
$PATH, it's in
cat <<'EOF' test.html | /usr/sbin/sendmail -t