|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, April 05, 2013 3:42 AM
Points: 291,
Visits: 1,064
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 27, 2010 6:49 AM
Points: 1,
Visits: 4
|
|
that's pretty beautiful, however I am not SQL Server Expert
if any one tried it, would you please provide something such as a template that non-expert users can use?
all help is appreciated
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 11:21 PM
Points: 12,
Visits: 101
|
|
The article is designed in beautiful manner...
The script are written in simple manner, so it helped me to understand the whole concept.
Thanks, Pankaj.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Sunday, March 10, 2013 8:04 PM
Points: 26,
Visits: 99
|
|
Nice article. Some questions though. If I want to include Cc and Bcc fields in the emails, how do I do it? How about the email Priority (Low, Normal, High Importance)
Personally, I find it easier to create a simple C# application. I would create an HTML template and shoot the desired info from the database to template so that it looks like a nice HTML email. Using dynamic sql to build the HTML can be challenging for more complex emails (with formatting).
Sample: CustomerList.htm:
<tr> <td style="width:50px; text-align:center; color:Blue"> [SNo] </td> <td style="width:150px; text-align:left; color:Blue"> [CustomerCode] </td> <td style="width:300px; text-align:left; color:Blue"> [CustomerName] </td> <td style="width:50px; text-align:center; color:Blue"> [CustomerType] </td> <td style="width:50px; text-align:center; color:Blue;"> [CustomerRank] </td> </tr>
MainEmail.htm (headers, embedded styles and other formatting are removed)
<tr> <td> <table> <tr class="DataGridHeaderGray"> <td style="width:50px; text-align:center"> S. No. </td> <td style="width:150px; text-align:center"> Customer Code </td> <td style="width:400px; text-align:center"> Customer Name </td> <td style="width:50px; text-align:center"> Type </td> <td style="width:50px; text-align:center"> Rank </td> </tr> [CUSTOMERLIST] </table> </td> </tr>
Then create a StreamReader that will read the contents of the template. Loop through your list of, replace the tags in square brackets (SNo, CustomerCode, etc). Set the recipients (To, Cc and Bcc). Create a MailMessage object and send the email.
Of course, there are other different ways. Cheers!
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, April 05, 2013 3:42 AM
Points: 291,
Visits: 1,064
|
|
Many Thanks to you. In order to achieve the results, use the below T-SQL query: EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Profile Name', @recipients=@cs_application_email, @copy_recipients ='EMail addresses to be included in cc', @blind_copy_recipients='EMail addresses to be included in bcc', @sensitivity ='High/Normal/Low' @subject ='Subject Of The EMail', @body = @query, @body_format = 'HTML' ;
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Yesterday @ 7:45 AM
Points: 54,
Visits: 118
|
|
I've been using the send mail functionality for a while now and have found so many uses for it, it's really cool. I send out morning reports using an HTML template or sending out pretty emails to customers, like this...
The weekly report style
DECLARE @TableHTML VARCHAR(MAX)
SET @tableHTML = N'<H1>Weekly Report For Week Beginning '+CONVERT(VARCHAR(20),dbo.ConvertTo5amDate(DATEADD(wk, DATEDIFF(wk,0,@Start), 0)))+'</H1>' + N'<style type="text/css">' + N'table {' + N'font-family: Arial, Helvetica, sans-serif;' + N'font-size: small;' + N'color: #111111;' + N'background-color: #EEEEEE;' + N'}' + N'</style>' + N'<table border="1">' + N'<tr><th>Col 1</th>' + N'<th>Col 2</th>' + N'<th>Col 3</th>' + N'<th>Col 4</th></tr>' + CAST ( ( SELECT td = [Period] ,'' ,td = [Total] ,'' ,td = [Processed] ,'' ,td = [NotProcessed] ,'' FROM #Table FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) + N'</table>' ;
IF @@ERROR<>0 BEGIN exec msdb.dbo.sp_send_dbmail @recipients = 'errors@here.co.uk' ,@body = 'An error occurred in the execution of [WeeklyReport]' ,@subject = 'ERROR mail from the database' ,@reply_to = 'me@here.co.uk' END ELSE BEGIN exec msdb.dbo.sp_send_dbmail @recipients = 'thisdistributionlist@here.co.uk' ,@body_format = 'HTML' ,@body = @TableHTML ,@subject = @SubjectLine ,@reply_to = 'me@here.co.uk' END
And the pretty email style
DECLARE @TableHTML VARCHAR(MAX)
SET @UserEmail = LTRIM(rtrim(@UserEmail))
SET @TableHTML ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>My document title</title> </head> <body> HI ' + @UserName + ' thanks for whatever you have done on our website, this is a generic confirmation message. Check out these boobies while you wait for something else to happen <img src="http://www.destructoid.com/elephant//ul/1929-468x-boobies.jpg"> </body> </html>' IF @@ERROR<>0 BEGIN exec msdb.dbo.sp_send_dbmail @recipients = 'errors@here.co.uk;' ,@body = 'An error occurred in the execution of the SendSexualEmail' ,@subject = 'ERROR mail from the database' ,@reply_to = 'me@here.co.uk' END ELSE BEGIN exec msdb.dbo.sp_send_dbmail @recipients = @UserEmail ,@from_address = 'noreply@here.co.uk' ,@body_format = 'HTML' ,@body = @TableHTML ,@subject = 'Your Sexual Email' ,@reply_to = 'me@here.co.uk'
END
There's tonnes more stuff you can do and obviously I don't spend all my day sending people images of boobies.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, June 29, 2012 4:56 AM
Points: 163,
Visits: 427
|
|
Nice article and it made me look back at some stuff I had done a while back and re-jig it to fit in with this.
I took a slightly different take on generating the mail batches. Also I was not sure why you needed the @query in there twice rather than generating beforehand.
My other different approach is to curse through each batch and semi-colon separate all the addresses into one addresses line:
--declare the tables for use DECLARE @tempEmails TABLE(emailID INT IDENTITY(1,1), emailAddress NVARCHAR(100)) DECLARE @emailBatches TABLE(batchID INT IDENTITY(1,1), startID INT, endID INT) DECLARE @batchSize INT DECLARE @totalMails INT DECLARE @startID INT DECLARE @endID INT DECLARE @emailAddresses NVARCHAR(4000) DECLARE @firstName NVARCHAR(100) DECLARE @mainBody NVARCHAR(MAX)
SELECT @mainBody = '<p>Welcome to the lovely batch mail we have here</p> <p>I think you will like it very much</p> <p>Bye then</p> <div id="Mysignature">'
SELECT @batchSize = 8
--test Data INSERT @tempEmails(emailAddress) SELECT 'fred@fred.com' UNION SELECT 'bill@bill.com' UNION SELECT 'jane@jane.com' UNION SELECT 'norman@norman.com' UNION SELECT 'weeble@weeble.com'
/* You might of course do INSERT @tempEmails(emailAddress) SELECT DISTINCT emailAddress FROM myTable WHERE emailAddress IS NOT NULL */
--see how many mails there are SELECT @totalMails = MAX(emailID) FROM @tempEmails
/** * compile the list of batches. * OK so the endID may go over the MaxID * but this with the below cursor this will not matter in the least **/ INSERT @emailBatches(startID, endID) SELECT @batchSize*(t1.emailID-1)+1, @batchSize*(t1.emailID-1)+@batchSize FROM @tempEmails t1 WHERE @batchSize*(t1.emailID-1)+1 <= @totalMails
DECLARE mailBatchCursor CURSOR FOR SELECT startID, endID FROM @emailBatches ORDER BY batchID OPEN mailBatchCursor FETCH NEXT FROM mailBatchCursor INTO @startID, @endID WHILE (@@fetch_status = 0) BEGIN --semi-colon separate the email addresses in the batch SELECT @emailAddresses = STUFF((SELECT ';' + emailAddress FROM @tempEmails WHERE emailID BETWEEN @startID AND @endID FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1,1,'') EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Profile Name', @recipients=@cs_application_email, @blind_copy_recipients =@emailAddresses, @subject ='Subject Of The EMail', @body = @query, @body_format = @mainBody ; WAITFOR delay '00:10:00' FETCH NEXT FROM mailBatchCursor INTO @startID, @endID END CLOSE mailBatchCursor DEALLOCATE mailBatchCursor Using FOR XML again you can easily make a table out of any query to add:
SELECT @table1 = '<h1>Look at the results below</h1><table><tr><th>col1</th><th>col2</th><th>col3</th></tr>' + (SELECT '<tr>' + '<td>' + col1 + '</td><td>' + col2 + '</td><td>' + col3 + '</td></tr>' FROM mytable WHERE myid = 5 FOR XML PATH(''),TYPE).value('.','VARCHAR(4000)') + '</table>'
_______________________________________________________ Change is inevitable... Except from a vending machine.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, December 05, 2011 4:38 AM
Points: 66,
Visits: 72
|
|
hello, the whole process of email is via email from SQL - if I get it! And if the email is external to SQL, for example Lotus Notes?
Thank you.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, April 05, 2013 3:42 AM
Points: 291,
Visits: 1,064
|
|
| You can send email from SQL to Outlook, Lotus Notes etc.I have implemented it for Microsoft Outlook.We need to know the relay server IP address while creating the Database Mail Configuration.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, June 29, 2012 4:56 AM
Points: 163,
Visits: 427
|
|
Outlook and Notes are clients (not sure if Notes server is called that). Mine is set to send via MS Exchange though any email server software such as sendmail would do. You could probably create a profile that sent via your gmail or hotmail accounts if you really wanted to.
In the bad old days pre 2005 you needed client software of Outlook 2000 installed to send mails
_______________________________________________________ Change is inevitable... Except from a vending machine.
|
|
|
|