How can I send an HTML email using a shell script?
The tags include 'sendmail' so here's a solution using that:
(
echo "From: me@xyz.com "
echo "To: them@xyz.com "
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/alternative; "
echo ' boundary="some.unique.value.ABC123/server.xyz.com"'
echo "Subject: Test HTML e-mail."
echo ""
echo "This is a MIME-encapsulated message"
echo ""
echo "--some.unique.value.ABC123/server.xyz.com"
echo "Content-Type: text/html"
echo ""
echo "<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<a href='http://www.google.com'>Click Here</a>
</body>
</html>"
echo "------some.unique.value.ABC123/server.xyz.com--"
) | sendmail -t
A wrapper for sendmail can make this job easier, for example, mutt:
mutt -e 'set content_type="text/html"' me@mydomain.com -s "subject" < message.html
Another option is using msmtp.
What you need is to set up your .msmtprc with something like this (example is using gmail):
account default
host smtp.gmail.com
port 587
from example@gmail.com
tls on
tls_starttls on
tls_trust_file ~/.certs/equifax.pem
auth on
user example@gmail.com
password <password>
logfile ~/.msmtp.log
Then just call:
(echo "Subject: <subject>"; echo; echo "<message>") | msmtp <email@domain.tld>
in your script
Update: For HTML mail you have to put the headers as well, so you might want to make a file like this:
From: sender@domain.tld
To: email@domain.tld
Subject: Important message
Mime-Version: 1.0
Content-Type: text/html
<h1>Mail body will be here</h1>
The mail body <b>should</b> start after one blank line from the header.
And mail it like
cat email-template | msmtp email@domain.tld
The same can be done via command line as well, but it might be easier using a file.
You can use the option -o in sendEmail to send a html email.
-o message-content-type=html to specify the content type of the email.
-o message-file to add the html file to the email content.
I have tried this option in a shell scripts, and it works.
Here is the full command:
/usr/local/bin/sendEmail -f sender@test.com -t "reciever@test.com" -s \
smtp.test.com -u "Title" -xu sender@test.com -xp password \
-o message-charset=UTF-8 \
-o message-content-type=html \
-o message-file=test.html
Another option is the sendEmail script http://caspian.dotconf.net/menu/Software/SendEmail/, it also allows you to set the message type as html and include a file as the message body. See the link for details.
In addition to the correct answer by mdma, you can also use the mail command as follows:
mail person@email.com -s"Subject Here" -a"Content-Type: text/html; charset=\"us-ascii\""
you will get what you're looking for. Don't forget to put <HTML>
and </HTML>
in the email. Here's a quick script I use to email a daily report in HTML:
#!/bin/sh
(cat /path/to/tomorrow.txt mysql -h mysqlserver -u user -pPassword Database -H -e "select statement;" echo "</HTML>") | mail email@email.com -s"Tomorrow's orders as of now" -a"Content-Type: text/html; charset=\"us-ascii\""
Heres mine (given "mail" is configured correctly):
scanuser@owncloud:~$ vi sendMailAboutNewDocuments.sh
mail -s "You have new mail" -a "Content-type: text/html" -a "From: sender@xxx.com" $1 << EOF
<html>
<body>
Neues Dokument: $2<br>
<a href="https://xxx/index.php/apps/files/?dir=/Post">Hier anschauen</a>
</body>
</html>
EOF
to make executable:
chmod +x sendMailAboutNewDocuments.sh
then call:
./sendMailAboutNewDocuments.sh recipient@xxx.com test.doc