Send HTML Mail the Easy Way

In the name of all that is sweet and holy, stop sending yourself ugly badly-formatted tabular data in text mode. Instead, give this procedure a try and make your Database Mail messages beautiful.

SQLTableToHTMLTable will generate the HTML for you without a ton of effort so that a table looks like a table in your mail messages. A developer on my team wrote it over 10 years ago and I’ve been using it since (thanks, Aaron H).

The procedure assumes you have a database called ‘DBAdmin’.

SQLTableToHTMLTable converts the contents of any reasonably sized table to HTML and stores it in an output variable you can pass in to sp_send_dbmail. So if you have metrics or some other data you deposited into a table, you can send the contents of it in a clean HTML formatted email. Yes, it also works with temp tables. And the table can be in any database on the SQL instance.

Obviously you wouldn’t want to send an email with 10k lines of tabular data. This is for monitoring or sending a few hundred rows at the most.

So give it a try if. All the necessary scripts are contained here.

In the spirit of of the first line of this post, I present you Mr. B and his trumpet rendition of the Supremes’ Stop! In The Name of Love.

By Dan

Hi, my name is Dan Shargel. I've been a DBA for probably too long. I'm writing these posts with either new MSSQL DBA's in mind or system administrators with a lot on their plate who got handed the keys without much prior SQL experience. Most of my experience has been with MSSQL, MySQL, Scotch, Bourbon and White Russians.