Solved

sp_send_dbmail format

Posted on 2011-02-16
6
1,117 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 500 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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