Tell me more ×
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.

mail -s "subject" [email protected] <test.html works, but only for plain text email.

What is the correct way to send HTML email using the Linux command mail?

share|improve this question

migrated from stackoverflow.com Jun 22 '11 at 6:21

4 Answers

cat htmlfile.html | mail -s "subject" [email protected]
share|improve this answer
3  
Nope. This will send a text email with HTML in the body. – Noufal Ibrahim Jun 22 '11 at 6:35

You will have to add Content-Type header to your email to make this happen.

echo "<html><b>Hello</b></html>" | mail -a "Content-type: text/html;" -s "Testing" [email protected]

will work

share|improve this answer
mail: illegal option -- a – pyth0ner Jun 22 '11 at 6:56
Do you have mailx? That might have the option. If that doesn't work. If that doesn't work, you can consider using mutt although I don't know off hand what the command line switches to do it are. – Noufal Ibrahim Jun 22 '11 at 7:04
1  
if mail isn't cutting the mustard, use python ... docs.python.org/library/email-examples.html examples is the 3rd one or #6 which suits your requirement. – sdolgy Jun 22 '11 at 8:40
Python will require you to write (and maintain) a script. A command line one liner has different advantages. – Noufal Ibrahim Jun 22 '11 at 11:04
I have mailx,but "option -a" don't work,still show:mail: illegal option -- a – pyth0ner Jun 27 '11 at 1:17

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.

  • With some 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.
    temp_script=$(mktemp)
    cat <<'EOF' >>"$temp_script"
    1a
    Content-Type: text/html
    .
    $r test.html
    w
    q
    EOF
    ## 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
    ~e
    .
    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 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.

cat <<'EOF' test.html | mutt -H -
To: to@address
Subject: hello
Content-Type: text/html

EOF

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 -t
To: to@address
Subject: hello
Content-Type: text/html

EOF
share|improve this answer
+1. Wonderfully detailed. – Noufal Ibrahim Jun 23 '11 at 15:20
#!/bin/sh

(
echo "To: [email protected]"
echo "Subject: hello"
echo "Content-Type: text/html"
echo
echo "<html><b><font size='7'>H</font>ello</b></html>"
echo
) | /usr/sbin/sendmail -t
share|improve this answer
3  
That doesn't acually use mail. – user16144 Oct 17 '12 at 19:09

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.