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\" >
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.
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.
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
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.
you should use "append" mode redirection >>
instead of >
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
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