Tuesday, August 20, 2019

UNIX - Send E-Mail with HTML Body and Multiple Attachments

Saw this question on several forums so thought to explain how to send e-mail from UNIX using sendmail with a HTML body and multiple attachments so below is the script for you to modify and use

font_text=`echo "<font face=\"arial\" size=3>"`
echo "To: me@domain.com" >> mime.txt
echo "From: someone@domain.com" >> mime.txt
echo "Subject: Your subject goes here" >> mime.txt
echo "MIME-Version: 1.0" >> mime.txt
echo "Content-Type: multipart/mixed; boundary=\"XXXXboundary text\"" >> mime.txt
echo "" >> mime.txt
echo "This is a multipart message in MIME format." >> mime.txt
echo "" >> mime.txt
echo "--XXXXboundary text" >> mime.txt
echo "Content-Type: text/html" >> mime.txt
echo "" >> mime.txt
echo "<html>" >> mime.txt
echo "<body>" >> mime.txt
echo "<p>" >> mime.txt
echo "<font face=\"calibri\" size=4>" >> mime.txt
echo "Dear User,<br><br>" >> mime.txt
echo "Put your HTML body over here" >> mime.txt
echo "</p>" >> mime.txt

#Below section you can remove if you do not have a table to send
echo "<table border=\"3\" cellpadding=\"10\">" >> mime.txt
echo "<tr bgcolor=\"cce6ff\">" >> mime.txt
echo "<td><b>${font_text}Column 1</b></td>" >> mime.txt
echo "<td><b>${font_text}Column 2</b></td>" >> mime.txt
echo "</tr>" >> mime.txt
echo "<tr>" >> mime.txt
echo "<td>${font_text}First column text</td>" >> mime.txt
echo "<td>${font_text}Second column text</td>" >> mime.txt
echo "</tr>" >> mime.txt
echo "</table>" >> mime.txt
#End of the table section

#Below is the signature or ending of the mail
echo "<p>" >> mime.txt
echo "<font face=\"calibri\" size=4>" >> mime.txt
echo "Thank You,<br>" >> mime.txt
echo "UNIX Box<br><br>" >> mime.txt
echo "NOTE: This is an automated e-mail from UNIX. Please do not respond to this message" >> mime.txt
echo "</p>" >> mime.txt

echo "</body>" >> mime.txt
echo "</html>" >> mime.txt

#Put first boundary here between HTML body and first attachment
echo "--XXXXboundary text" >> mime.txt
echo "Content-Type: application/zip;name=test.zip; Content-Transfer-Encoding: base64;Content-Disposition: attachment" >> mime.txt
echo "" >> mime.txt
uuencode test.zip test.zip  >> mime.txt
echo ""  >> mime.txt
#Put second boundary here between first attachment and second attachment
echo "--XXXXboundary text" >> mime.txt
echo "Content-Type: application/zip;name=test2.zip; Content-Transfer-Encoding: base64;Content-Disposition: attachment" >> mime.txt
echo "" >> mime.txt
uuencode test2.zip test2.zip >> mime.txt
echo "" >> mime.txt

#If you have more attachments add the same as above from the boundary till the uuencode

#Put final boundary
echo "--XXXXboundary text--" >> mime.txt

cat mime.txt | sendmail me@domain.com