How to send HTML email using linux command line

I need to send an html email. I have only the linux command and the mail command.

Currently 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 only get text / text.

alt text

+55
html linux email send
Apr 07 '10 at 10:53 on
source share
12 answers

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 
+49
Apr 28 '11 at 16:51
source share

My version of mail does not have --append and is too smart for echo -e \n -trick (it just replaces \ n with a space). However, it has -a :

 mail -a "Content-type: text/html" -s "Built notification" address@example.com < /var/www/report.html 
+39
Sep 04 '12 at 11:18
source share

Create a tmp.html file and put the following line in it:

 <b>my bold message</b> 

Then paste all of this into the command line: (with bracket 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 

Mail will be sent. And the message appeared as bold, and not with <b> tags.

Source:
How to send html email using bash sendmail "?

+26
Dec 13
source share

The problem is that when redirecting a file to "mail" it is used only for the message body. All the headers that you insert into the file will be sent to the body.

Try:

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

- append allows you to add arbitrary headers to the mail, where you must specify the type of content and content. There is no need to embed the To and Subject headers in your file or specify them using --append, since you implicitly install them on the command line (-s is the theme and the @. Com example automatically becomes To ).

+7
Apr 07 2018-10-22T00:
source share

On OS X (10.9.4), cat works, and it's easier if your email is already in the file:

 cat email_template.html | mail -s "$(echo -e "Test\nContent-Type: text/html")" karl@marx.com 
+6
Aug 19 '14 at 19:21
source share

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

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 text/html; charset=utf-8
  • Content-Transfer-Encoding: up to 8bit (not sure if this is really necessary).

To send an HTML email address:

 mail -Ssendmail='~/bin/sendmail-hook' \ -s "Built notification" address@example.com < /var/www/report.csv 
+3
Sep 17 '14 at 7:01
source share

you should use add mode redirection instead of >

+2
Apr 07 '10 at
source share

A very old question, however, it occupies a high place when I asked a question about it.

Find the answer here:

Sending HTML using shell script

+2
Mar 27 '12 at 7:57
source share

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

In your case, it will be:

 mail -aContent-Type:text/html -s "Built notification" address@example.com < /var/www/report.csv 
+2
Nov 17 '17 at 13:11
source share

Try:

 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 
+1
Apr 07 '10 at
source share

I struggled with a similar problem (with mail) in one of my git post_receive hooks, and finally I found out that sendmail really works better for this kind of thing, especially if you know a little bit about how emails are (and it seems you know). I know that this answer comes very late, but perhaps it will be useful to others. I used the heredoc operator and used a function to expand the variables, so it can also run inline scripts. Just check this (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 the backlinks in the MAIL part to generate some output and remember that the <<- operator breaks only tabs (not spaces) from the beginning of lines, so copying will not work in this case (you need to replace the indents with the corresponding tabs). Or use the << operator and do not indent. Hope this helps someone. Of course, you can use backticks outside the o MAIL part and save the output in some kind of variable that you can later use in the MAIL part to taste and readability. And I know #!/bin/bash does not always work on every system.

+1
Feb 16 '17 at 5:51 on
source share

Use the CLI for mass mailing , an efficient and powerful tool for sending dynamic emails to the mailing list with one simple command: bulkmail mail !

Do quick, mini, hassle-free email marketing with this small but Powerful tool πŸ’Œ

0
Apr 02 '19 at 15:46
source share



All Articles