Hey everyone, it was quite a mystery for me since long time on how to send preformatted or html generated reports from a server and even though there are a lot of good people sharing knowledge on this part. Here I share a portion of code I developed (with their help) to use for this purpose.
Below is one of the shell script code you would need to use with "sendmail" utility to achieve sending mails in HTML format directly from any server to a list of recipients.
FROM="<user>@<yourdomain>"
TO="<username>@<yourdomain>.com, <username2>@<yourdomain>.com"
SUBJECT="An HTML Formatted Mail Body Test"
delimiter="`date +%Y%m%d%H%M%S`"
cat <<At | /usr/sbin/sendmail -t
From: ${FROM}
To: ${TO}
Subject: ${SUBJECT}
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="${delimiter}"
--${delimiter}
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<HTML>
`cat /tmp/your_html_file_or_message.html`
</HTML>
--${delimiter}--
At
You could echo a variable, write html code by hand or can expand an html file content like I did in email body between HTML tags.
Feel free to leave a comment if you would like to understand explanation behind any part of above code.
Even though you could use mailx to attach html file using -a switch. Having a report in mail body saves time of opening an attachment.
Cheers!
Anurag
Below is one of the shell script code you would need to use with "sendmail" utility to achieve sending mails in HTML format directly from any server to a list of recipients.
FROM="<user>@<yourdomain>"
TO="<username>@<yourdomain>.com, <username2>@<yourdomain>.com"
SUBJECT="An HTML Formatted Mail Body Test"
delimiter="`date +%Y%m%d%H%M%S`"
cat <<At | /usr/sbin/sendmail -t
From: ${FROM}
To: ${TO}
Subject: ${SUBJECT}
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="${delimiter}"
--${delimiter}
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<HTML>
`cat /tmp/your_html_file_or_message.html`
</HTML>
--${delimiter}--
At
You could echo a variable, write html code by hand or can expand an html file content like I did in email body between HTML tags.
Feel free to leave a comment if you would like to understand explanation behind any part of above code.
Even though you could use mailx to attach html file using -a switch. Having a report in mail body saves time of opening an attachment.
Cheers!
Anurag