71

I need to send email with html format. I have only linux command line and command "mail".

Currently have used:

echo "To: address@example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv

echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv

mail -s "Built notification" address@example.com < /var/www/report.csv

But in my mail-agent i get only plain/text.

alt text

codeforester
  • 39,467
  • 16
  • 112
  • 140
Oleh Herych
  • 897
  • 1
  • 9
  • 16
  • 6
    Readers of answers to this question beware: there are several different programs called `mail`, for example `heirloom-mailx` and `bsd-mailx` on Debian jessie. If a `mail` command from an answer here doesn't work for you, you're probably using the wrong `mail`. Refer to your distribution's package manager to install the correct package, and use the specific name of that binary (e.g. `bsd-mailx` on Debian) to resolve that issue. More details on this here: http://heirloom.sourceforge.net/mailx_history.html – Martin von Wittich Oct 18 '17 at 07:52

11 Answers11

65

This worked for me:

echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" foo@example.com
Dude
  • 683
  • 5
  • 3
47

My version of mail does not have --append and it too smart for the echo -e \n-trick (it simply replaces \n with space). It does, however, have -a:

mail -a "Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.html
Ole Tange
  • 31,768
  • 5
  • 86
  • 104
  • Works on ubuntu 14.04.1 LTS, mail set to bsd-mailx – kidmose Feb 10 '15 at 13:06
  • I use this way `echo \`curl -L www.google.es\` | mail -a "Content-type: text/html" -s "website content" email@email.com` instead of `<`. For my convenience, i create a /usr/local/bin script to bypass the most of the command. I just use `wmail url email` – m3nda Jun 12 '15 at 03:30
  • I tried it but SpamAssassin gave me lower score than I got without the -a "Cont...", maybe beacuse I didn't have text as html – Valter Ekholm Aug 11 '19 at 08:26
  • 5
    I get no such file or directory for content type with mail and mailx – Saikiran Apr 10 '20 at 12:01
  • 3
    I also get "Content-type: text/html: No such file or directory". Are you sure the "-a" is right ? – daveg Jan 29 '21 at 01:37
  • does not work on OSX – coterobarros May 08 '22 at 15:14
35

Send email using command Line

This answer is over 11 years old, these days I use python's import ezgmail for a 4 line plug, auth and play solution

Create a file named tmp.html with the following contents:

<b>my bold message</b>

Next, paste the following into the command line (parentheses and all):

(
  echo To: youremail@blah.com
  echo From: el@defiant.com
  echo "Content-Type: text/html; "
  echo Subject: a logfile
  echo
  cat tmp.html
) | sendmail -t

The mail will be dispatched including a bold message due to the <b> element.

Shell Script

As a script, save the following as email.sh:

ARG_EMAIL_TO="recipient@domain.com"
ARG_EMAIL_FROM="Your Name <you@host.com>"
ARG_EMAIL_SUBJECT="Subject Line"

(
  echo "To: ${ARG_EMAIL_TO}"
  echo "From: ${ARG_EMAIL_FROM}"
  echo "Subject: ${ARG_EMAIL_SUBJECT}"
  echo "Mime-Version: 1.0"
  echo "Content-Type: text/html; charset='utf-8'"
  echo
  cat contents.html
) | sendmail -t

Create a file named contents.html in the same directory as the email.sh script that resembles:

<html><head><title>Subject Line</title></head>
<body>
  <p style='color:red'>HTML Content</p>
</body>
</html>

Run email.sh. When the email arrives, the HTML Content text will appear red.

Related

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • `echo | cat msg-headers - msg-body | sendmail -t` (with `msg-headers` and `msg-body` as text files containing your headers and body, respectively) – palswim Oct 09 '18 at 17:26
  • This worked for me but how can we add an attachment when we are using sendmail – DileepGogula Dec 19 '18 at 11:21
  • 1
    @DileepGogula See http://www.zedwood.com/article/bash-send-mail-with-an-attachment (linked from the "Related" part above in two clicks) – koppor Jul 26 '20 at 21:20
10

On OS X (10.9.4), cat works, and is easier if your email is already in a file:

cat email_template.html  | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl@marx.com
jamesnotjim
  • 575
  • 7
  • 17
6

The problem is that when redirecting a file into 'mail' like that, it's used for the message body only. Any headers you embed in the file will go into the body instead.

Try:

mail --append="Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.csv

--append lets you add arbitrary headers to the mail, which is where you should specify the content-type and content-disposition. There's no need to embed the To and Subject headers in your file, or specify them with --append, since you're implicitly setting them on the command line already (-s is the subject, and address@example.com automatically becomes the To).

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 3
    don't have that option on my mail command mail: invalid option -- a Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ... [-- sendmail-options ...] mail [-iInNv] -f [name] mail [-iInNv] [-u user] – Tom Jan 20 '12 at 02:45
  • @nylund: `mail (GNU Mailutils 2.2)`. this is off ubuntu 12.04, but will be in older versions as well, given this answer's almost 3 years old now. – Marc B Jan 09 '13 at 17:43
3

With heirloom-mailx you can change sendmail program to your hook script, replace headers there and then use sendmail.

The script I use (~/bin/sendmail-hook):

#!/bin/bash

sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $@

This script changes the values in the mail header as follows:

  • Content-Type: to text/html; charset=utf-8
  • Content-Transfer-Encoding: to 8bit (not sure if this is really needed).

To send HTML email:

mail -Ssendmail='~/bin/sendmail-hook' \
    -s "Built notification" address@example.com < /var/www/report.csv
loentar
  • 5,111
  • 1
  • 24
  • 25
  • For some reason I can't get this to work in a function, but you can indeed use this as a separate executable file. This worked when other suggestions on this page did not. – bgStack15 Jul 14 '15 at 17:18
  • You can't get this work as a function because mail's process spawned don't have access to functions in your bash script – loentar Jul 14 '15 at 18:01
  • [Can't figure out how to take this to a chat or PM.] Could I source my mail command? `. mail -Ssendmail='~/bin/sendmail-hook' -s "Subject" address@example.com < inputfile` – bgStack15 Jul 14 '15 at 18:35
  • 1
    You cannot source it, because it is binary. You should create own bash script with those commands and (if you need that) source it. – loentar Jul 14 '15 at 21:06
3

I found a really easy solution: add to the mail command the modifier -aContent-Type:text/html.

In your case would be:

mail -aContent-Type:text/html -s "Built notification" address@example.com < /var/www/report.csv
Muc
  • 1,464
  • 2
  • 13
  • 31
  • 2
    But I get no such file or directory when am using this – Saikiran Apr 10 '20 at 12:06
  • @eightStacker: is there a file called report.csv in your /var/www directory? If not you should replace that with the file you want to send. Also address@example.com is an example, you have to replace that with the actual destinatary of your message – Muc Dec 23 '20 at 14:30
  • @Zafar same thing I told eightStacker :) – Muc Dec 23 '20 at 14:30
  • `-a` option is for "attachment"... I'm working with version "12.5 7/5/10" on a **CentOS** 7.4 server; in other comments they talk about `--append`, and this is not even an option ... why so many _(apparently)_ **mail** implementations? – ecedenyo Dec 20 '21 at 22:02
  • See also https://stackoverflow.com/a/48588035/874188 which discusses the many `mail` implementations and how to know which one you have. – tripleee Apr 02 '23 at 17:23
2

I was struggling with similar problem (with mail) in one of my git's post_receive hooks and finally I found out, that sendmail actually works better for that kind of things, especially if you know a bit of how e-mails are constructed (and it seems like you know). I know this answer comes very late, but maybe it will be of some use to others too. I made use of heredoc operator and use of the feature, that it expands variables, so it can also run inlined scripts. Just check this out (bash script):

#!/bin/bash
recipients=(
    'john@example.com'
    'marry@not-so-an.example.com'
#   'naah@not.this.one'
);
sender='highly-automated-reporter@example.com';
subject='Oh, who really cares, seriously...';
sendmail -t <<-MAIL
    From: ${sender}
    `for r in "${recipients[@]}"; do echo "To: ${r}"; done;`
    Subject: ${subject}
    Content-Type: text/html; charset=UTF-8

    <html><head><meta charset="UTF-8"/></head>
    <body><p>Ladies and gents, here comes the report!</p>
    <pre>`mysql -u ***** -p***** -H -e "SELECT * FROM users LIMIT 20"`</pre>
    </body></html>
MAIL

Note of backticks in the MAIL part to generate some output and remember, that <<- operator strips only tabs (not spaces) from the beginning of lines, so in that case copy-paste will not work (you need to replace indentation with proper tabs). Or use << operator and make no indentation at all. Hope this will help someone. Of course you can use backticks outside o MAIL part and save the output into some variable, that you can later use in the MAIL part — matter of taste and readability. And I know, #!/bin/bash does not always work on every system.

Cromax
  • 1,822
  • 1
  • 23
  • 35
2

Very old question, however it ranked high when I googled a question about this.

Find the answer here:

Sending HTML mail using a shell script

Community
  • 1
  • 1
Magge
  • 268
  • 2
  • 7
1

you should use "append" mode redirection >> instead of >

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

Try with :

echo "To: address@example.com" > /var/www/report.csv
echo "Subject: Subject" >> /var/www/report.csv
echo "MIME-Version: 1.0" >> /var/www/report.csv
echo "Content-Type: text/html; charset=\"us-ascii\"" >> /var/www/report.csv
echo "Content-Disposition: inline" >> /var/www/report.csv

echo "<html>" >> /var/www/report.csv
mysql -u ***** -p***** -H -e "select * from users LIMIT 20" dev >> /var/www/report.csv
echo "</html>" >> /var/www/report.csv

mail -s "Built notification" address@example.com < /var/www/report.csv
Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70
  • This does not work. $ lsb_release -a Distributor ID: Ubuntu Description: Ubuntu 10.04.4 LTS Release: 10.04 Codename: lucid – lrkwz Mar 09 '12 at 17:15
  • 1
    @Simone, this is not working perfectly because of order of **MIME-Version** and **Content-Tye**. You need to put this information prior to **Subject** to work for most of the unix/linux flavor. – Alpesh Gediya May 24 '13 at 13:34