HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

SQL Server Email Pastes Literal HTML

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
emailsqlliteralpastesserverhtml

Problem

I am attempting to use HTML in my sql server send mail, but my email just shows what I type instead of formatting properly. Can someone show me what needs to be altered in order for this to appear as HTML

declare @body1 varchar(4000)
set @body1 = '
        
        
        
        
        
        
        
        
        
        
        
        '

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Mod',
    @from_address = 'modis@modisglobal.com',
    @recipients= 'rsmith@gmail.com',
    @subject= 'Test Email', 
    @body = @body1

Solution

From the documentation, you can see there's a @body_format option.


[ @body_format= ] 'body_format' Is the format of the message body. The
parameter is of type varchar(20), with a default of NULL. When
specified, the headers of the outgoing message are set to indicate
that the message body has the specified format. The parameter may
contain one of the following values: TEXT HTML Defaults to TEXT.

Set it to HTML.

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Mod',
    @from_address = 'modis@modisglobal.com',
    @recipients= 'rsmith@gmail.com',
    @subject= 'Test Email', 
    @body = @body1,
    @body_format = 'HTML'

Code Snippets

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Mod',
    @from_address = 'modis@modisglobal.com',
    @recipients= 'rsmith@gmail.com',
    @subject= 'Test Email', 
    @body = @body1,
    @body_format = 'HTML'

Context

StackExchange Database Administrators Q#152125, answer score: 4

Revisions (0)

No revisions yet.