?
Solved

sp_send_dbmail format

Posted on 2011-02-16
6
Medium Priority
?
1,136 Views
Last Modified: 2012-06-21
when sending an email through the above proc in SQL Server to outlook with HTML format, it is jumbled and not proper. what does outlook need for it to receive the format in nice looking way?

thanks
0
Comment
Question by:anushahanna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 34911376
You need to format the document like in the example below:

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

SET @title = 'Summary Report - Passive and Active List'
SET @tableHTML =
    N'<H1>' + @title + '</H1>' +
    N'<table border="1">' +
    N'<tr>' +
      N'<th>gway</th>' +
      N'<th>valid_count</th>' +
      N'<th>active</th>' +
      N'<th>excluded</th>' +
   CAST(
            (SELECT
                  td = gway, '',
                  td = flag, '',
                  td = case when active = 1 then 'YES' else 'NO' END ,'',
                  td = case when gel = 1 then 'YES' else 'NO' END ,''
            FROM  servername.dbo.tablename
            FOR XML PATH('tr'), TYPE)
      AS NVARCHAR(MAX) ) + N'</table>' ;

IF @tableHTML is not null
BEGIN
    EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'your_profile',
            @recipients='anushahanna@mail.com',
            @subject = @title,
            @body = @tableHTML ,
            @body_format = 'HTML' ;
END
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34935836
thanks lcohan- does this make sure any recipient mails, will receive it ok, in proper format (as long the recipient mail system supports HTML)?
0
 
LVL 40

Accepted Solution

by:
lcohan earned 2000 total points
ID: 34954808
That is correct including even blackberry users on both 2005 and 2008 so far.
Please test is and adjust it to your table structure - other nice thing is that if table is empty like you have nothing to send that particular time they won't get an email with nothing attached.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 6

Author Comment

by:anushahanna
ID: 34979810
lcohan- thanks.

can you please add some dummy values to the below code, just to prove the point of how pretty this can be made. can we add 2 rows of data for the 4 columns and put some colors?
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>'  + N'</table>' ;

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

Open in new window

0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 2000 total points
ID: 34981164
I'm pretty sure ther must be a way to color it and you should be able to find that in SQL BOL however to get the example you wanted just replace your mail profile and actual email in query below:

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

Open in new window

0
 
LVL 6

Author Comment

by:anushahanna
ID: 34982861
very nice, lcohan - thanks much.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Read this checklist to learn more about the 15 things you should never include in an email signature.
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
Suggested Courses

800 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