Skip to content

Instantly share code, notes, and snippets.

@ronascentes
Last active July 5, 2018 17:27
Show Gist options
  • Save ronascentes/d9b1545965b432572bd343823b5be52a to your computer and use it in GitHub Desktop.
Save ronascentes/d9b1545965b432572bd343823b5be52a to your computer and use it in GitHub Desktop.
Send a formatted HTML email from SQL Server
SET NOCOUNT ON;
DECLARE @body nvarchar(max)
SET @body = N'
<style type=''text/css''>
TABLE{border-width: 1px;border-style: solid;background-color: #E8E8E8; border-color: black;border-collapse: collapse;}
TH{color:black; border-width: 1px;font-size: 11px; padding: 3px;border-style: solid;border-color: black;}
TD{color:black; border-width: 1px;font-size: 11px; padding: 3px;border-style: solid;border-color: black;}
</style>' +
N'<h5> *** This is an automatic email, please do not reply *** </h5>' +
N'<table>' +
N'<th> name </th> <th> is_disabled </th>' +
CAST((select column_name AS 'td','' , column_name AS 'td' FROM table_name
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX)) +
N'</table>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'dbmail',
@recipients = '[email protected]; [email protected]',
@copy_recipients = '[email protected]; [email protected]',
@subject = 'This is an automatic email',
@body = @body,
@body_format ='HTML',
@importance = 'High'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment