Solved

Sql Server 2008 Database Mail amends html tags

Posted on 2011-03-04
9
723 Views
Last Modified: 2012-06-21
When I send a
<br />

Open in new window

tag or indeed any self closing tag the source of the message gets altered by SQL Server Mail and the final / is removed from the tag i.e
<br />

Open in new window

becomes
<br>

Open in new window

.

I'm trying to send valid XHTML but of course
<br>

Open in new window

is not a valid for this DTD.

I've tried escaping the / and using the CHAR function both of which make no difference.

I assume it's go something to do with the way Database Mail has been set-up but I can't find anything related to this sort of thing.

Please help
0
Comment
Question by:rs256
  • 5
  • 2
  • 2
9 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 35039850
0
 

Author Comment

by:rs256
ID: 35042971
I've tried that here is a code snippet

		SET @BodyHTML =
		  N'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html>
		 		<head>
					<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		 			<title> + @MessageSubject + </title>
					<style type="text/css">
						body
						{ 
							font-family: Verdana, Arial, Helvetica, sans-serif;
							font-size: 12px;
							color: #333;
						}
					</style>
				</head>
				<body><br/>

Open in new window


The meta tag and the br tag both have their self closing / removed.

Any other ideas please.
0
 

Author Comment

by:rs256
ID: 35045179
Just one other thing to add lcohan, your example doesn't have any self closing tags.

It is specifically these that I'm having a problem with

Many thanks

rs256

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35047132
Have you considered sending the email as text instead of HTML?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:rs256
ID: 35054910
Yes, however we are sending email with a table in it with 20-30 records in it and it works a lot better in HTML.

I understand all real coders only send text messages with links in but this is a customer facing system and HTML is pretty much expected !

Thanks for the thought though.

0
 
LVL 39

Expert Comment

by:lcohan
ID: 35058496
We are at least 2 individuals in this comunity that tested and agreed the code below works fine for sending HTML - maybe you missed something with copy paste:

create table #test (column1 int, column2 text, column3 text, column4 text)
insert into #test SELECT 1 ,'text2','text3','text4'
insert into #test SELECT 2 ,'code2','code3','code4'


DECLARE @title nvarchar(200)
DECLARE @tableHTML nvarchar(MAX)

SET @title = 'Summary Report - FunctionCode'
SET @tableHTML =
    N'<H1>' + @title + '</H1>' +
    N'<table border="1">' +
    N'<tr>' +
      N'<th>Column1</th>' +
      N'<th>Column2</th>' +
      N'<th>Column3</th>' +
      N'<th>Column4</th>' +
     CAST(
            (SELECT
                  td = Column1 ,'',
                  td = Column2,'',
                  td = Column3,'',
                  td = Column4,''
            FROM  #test WHERE column1 in (1,2)
            FOR XML PATH('tr'), TYPE)
       AS NVARCHAR(MAX) )
       + N'</table>' ;

IF @tableHTML is not null
BEGIN
    EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'mail_profile',
            @recipients='user@mail.com',
            @subject = @title,
            @body = @tableHTML ,
            @body_format = 'HTML' ;
END

drop table #test
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 35060054
Something else is going on here.  I just executed the code below and verified the html sent (using sysmail_sentitems) and received matched what was originally entered.
DECLARE @rc int,
	@Message varchar(1000)

SET @Message = '<html>
			<body bgColor=#ffffff>
				Test Email. <br />
			</body>
		</html>'
EXEC @rc = msdb.dbo.sp_send_dbmail 
	    @recipients = 'my email address goes here', 
	    @subject = 'Test Email',
	    @body_format = 'HTML', 
	    @body = @Message
	
SELECT  @rc

Open in new window

0
 

Author Comment

by:rs256
ID: 35068016
Many thanks for your efforts lcohan and acperkins.  lcohan's example still doesn't have any self closing tags but acperkins got to the bottom of it.

It's not database mail that is causing the problem it's Exchange 2010, although Gmail seems to do the same thing we did find some mail mails servers that left the tags alone.

This takes the issue out of SQL database mail territory so I'll close it and accept acperkins's answer.

Thanks again for the help.

rs256
0
 

Author Closing Comment

by:rs256
ID: 35068019
Really appreciate the help from both all parties.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now