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.
On OS X (10.9.4),
cat
works, and is easier if your email is already in a file: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
):This script changes the values in the mail header as follows:
Content-Type:
totext/html; charset=utf-8
Content-Transfer-Encoding:
to8bit
(not sure if this is really needed).To send HTML email:
Make a file called tmp.html and put the following line in it:
Then paste all this into the commandline: (with the parenthesis and all).
The mail will be dispatched. And the message appeared as bold instead of with the
<b>
tags.Source:
How to send a html email with the bash command "sendmail"?
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
I found a really easy solution: add to the mail command the modifier -aContent-Type:text/html.
In your case would be:
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):
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.