Mailx send html message [duplicate] Mailx send html message [duplicate] linux linux

Mailx send html message [duplicate]


It's easy, if your mailx command supports the -a (append header) option:

$ mailx -a 'Content-Type: text/html' -s "my subject" user@gmail.com < email.html

If it doesn't, try using sendmail:

# create a header file$ cat mailheaderTo: user@gmail.comSubject: my subjectContent-Type: text/html# send$ cat mailheader email.html | sendmail -t


There are many different versions of mail around. When you go beyond mail -s subject to1@address1 to2@address2

  • With some mailx implementations, e.g. from mailutils on Ubuntu orDebian'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 possibilityto insert arbitrary headers is to set editheaders=1 and use anexternal editor (which can be a script).

    ## Prepare a temporary script that will serve as an editor.## This script will be passed to ed.temp_script=$(mktemp)cat <<'EOF' >>"$temp_script"1aContent-Type: text/html.$r test.htmlwqEOF## Call mailx, and tell it to invoke the editor scriptEDITOR="ed -s $temp_script" heirloom-mailx -S editheaders=1 -s "Subject" to@address <<EOF~e.EOFrm -f "$temp_script"
  • With a general POSIX mailx, I don't know how to get at headers.

If you're going to use any mail or 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 mail and mailx.

  • When composing a message, mail and 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 mail/Mail/mailx. For example, mutt. With Mutt, you can supply most headers in the input with the -H option, but not Content-Type, which needs to be set via a mutt option.

mutt -e 'set content_type=text/html' -s 'hello' 'to@address' <test.html

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 /usr/sbin or /usr/lib.

cat <<'EOF' - test.html | /usr/sbin/sendmail -tTo: to@addressSubject: helloContent-Type: text/htmlEOF


I had successfully used the following on Arch Linux (where the -a flag is used for attachments) for several years:

mailx -s "The Subject $(echo -e \\\nContent-Type: text/html)" user@gmail.com < email.html

This appended the Content-Type header to the subject header, which worked great until a recent update. Now the new line is filtered out of the -s subject. Presumably, this was done to improve security.

Instead of relying on hacking the subject line, I now use a bash subshell:

(    echo -e "Content-Type: text/html\n"    cat mail.html ) | mail -s "The Subject" -t user@gmail.com

And since we are really only using mailx's subject flag, it seems there is no reason not to switch to sendmail as suggested by @dogbane:

(    echo "To: user@gmail.com"    echo "Subject: The Subject"    echo "Content-Type: text/html"    echo    cat mail.html) | sendmail -t

The use of bash subshells avoids having to create a temporary file.