Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sending HTML Newsletters in a Batch Using SQL Server


Sending HTML Newsletters in a Batch Using SQL Server

Author
Message
Satnam Singh
Satnam Singh
Old Hand
Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)

Group: General Forum Members
Points: 300 Visits: 1084
Comments posted to this topic are about the item Sending HTML Newsletters in a Batch Using SQL Server
wnasri
wnasri
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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
p.modi
p.modi
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 106
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.
jun.merencilla
jun.merencilla
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 100
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!
Satnam Singh
Satnam Singh
Old Hand
Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)

Group: General Forum Members
Points: 300 Visits: 1084
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' ;
chris.rogers00
chris.rogers00
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 187
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.
Rob Fisk
Rob Fisk
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 428
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.

jorge_gomes98
jorge_gomes98
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 86
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.
Satnam Singh
Satnam Singh
Old Hand
Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)

Group: General Forum Members
Points: 300 Visits: 1084
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.
Rob Fisk
Rob Fisk
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 428
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.

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search