Last updated on October 4th, 2022 at 12:59 pm
It is a good idea to use sendmail command to send an html email from a linux or unix server. By using this command the email content will be displayed in HTML format (not just HTML code). In this tutorial I will be covering two options to send HTML email using shell script,
Using SendMail
If you don’t have sendmail installed please install it first. I personally suggest to install sendmail to do this trick.
Save all your HTML content in to a file with some name. For example I am giving the file name as email.html
Add the below content
<h1>This is a test email<h2><b>Mistonline.in one of the largest website tutorial provider</b>
Save this file to some path like /home/admin
Now create a shell script named test_email.sh in path /home/admin and add the below content, Make sure execute permission for test_email.sh file is set accordingly.
Note: Change the To: [email protected] to email address of your choice
#!/bin/sh
host=`hostname`
(echo To: [email protected]
echo From: server@$host
echo "Content-Type: text/html; "
echo Subject: Some Subject
echo
cat email.html ) | sendmail -t
If you do not want a file to be created but just want some html content to be send through email then use the below commands
#!/bin/sh
host=`hostname`
(echo To: [email protected]
echo From: server@$host
echo "Content-Type: text/html; "
echo Subject: Some Subject
echo
echo "<h1>This is a test email<h2><b>Mistonline.in one of the largest website tutorial provider</b>" ) | sendmail -t
Sample Output

You might also be interested in
How to send email with attachment using PHP
Using mail
If you don’t have sendmail installed then try using the mail command.
Send HTML email in a single mail command
# mail -a "Content-type: text/html" -s "Testing HTML Email" [email protected] << EOF
<html><h2>This is a test email</h2><a href="https://webtutorials.dev">Click Here</a></html>
EOF
Here,
-a, –append=HEADER: VALUE append given header to the message being sent
-s, is the subject of the email
Send HTML email by appending HTML file in the mail command
mail -a "Content-type: text/html" -s "Testing HTML Email" [email protected] < email.html
In the above command email.html contains the HTML format we are intending to send as an email.
Make sure to replace [email protected] to the email address of your choice.
Sample output
