I am attempting to send out 72 separate emails using the following code;
However, while the temp table returns 72 records, I only get one email. I am sure it is just the placement of my statements or the ommission of an 'END', but I can't figure it out. Could someone offer some advice or just a pair of fresh eyes?
Thanks in advance,
Donald
Code:
DECLARE @cardname varchar(100),
@mailrecipients varchar(100),
@numatcard varchar(100),
@trackno varchar(50),
@docnum as varchar(50),
@DocDate as varchar(50),
@newBody nvarchar(max),
@subject1 varchar(100)
SELECT T1.E_mail,T0.CardName,T0.NumAtCard,ISNULL(T0.TrackNo,'1ZOOPS') AS TrackNo,T0.DocDate,T0.DocNum, 0 as processed
INTO #maillist
FROM OINV T0 INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
WHERE T0.SlpCode = 3 AND T0.NumAtCard is not null AND T0.Series = 37 AND T0.DocDate > getdate()-2
WHILE (select count(*) from #maillist where processed = 0) > 0
BEGIN
SELECT TOP 1 @cardname = isnull(CardName,''),
@numatcard = isnull(NumAtCard,''),
@trackno = isnull(TrackNo,''),
@mailrecipients = isnull(E_mail,''),
@docnum = DocNum,
@docdate = substring(CAST(DocDate as NVARCHAR(11)),1,11)
FROM #maillist
WHERE processed = 0
SET @newBody = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Your return has been processed</title>
<style type="text/css">
<!--
.callout {
border-width:0;vertical-align:baseline;background-color:transparent;display:inline;float:left;position:relative;width:800px;margin-top:0;margin-bottom:0;margin-right:auto;margin-left:0px;text-align:center;padding-top:15px;padding-bottom:0;padding-right:0;padding-left:0;background-image:url([URL unfurl="true"]http://www.goorin.com/sites/default/files/templates/orderproc/promo_lines.gif);background-repeat:no-repeat;height:35px;font-family:Georgia,Trebuchet[/URL] MS,Times New Roman;font-size:100%;font-weight:bold;color:#682609; text-transform: capitalize;
}
.action {
margin-right:0; margin-left:0; padding-top:0; padding-bottom:0; padding-right:0; padding-left:0; border-width:0; font-size:100%; vertical-align:baseline; background-color:transparent; line-height:1.5em; letter-spacing:1.1px;" ><span style="margin-top:0;margin-bottom:0;margin-right:0;margin-left:0;padding-top:0;padding-bottom:0;padding-right:0;padding-left:0;border-width:0;vertical-align:baseline;background-color:transparent;font-size:20px;color:#682609;
}
p {
font-family:Georgia, "Times New Roman", Times, serif;
}
.invoicedetails {
font-family:Georgia, "Times New Roman", Times, serif;
font-size: 14px;
}
-->
</style>
</head>
<body>
<table background="[URL unfurl="true"]http://www.goorin.com/sites/default/files/templates/orderproc/bg_repeat_42.jpg">[/URL]
<tr>
<td align="center">
<table width="800">
<tr>
<td><a href="[URL unfurl="true"]http://www.goorin.com"><img[/URL] src="[URL unfurl="true"]http://www.goorin.com/sites/default/files/templates/orderproc/bg_header.gif"[/URL] border="0"/></a></td>
</tr>
<tr>
<td><table style="width:800px;border-width:0;padding-top:0;padding-bottom:0;padding-right:0;padding-left:0;border-width:0;position:relative;float:left;background-image:url([URL unfurl="true"]http://www.goorin.com/sites/default/files/templates/orderproc/bg_nav.jpg);background-repeat:no-repeat;background-position:center;background-attachment:scroll;height:30px;"><tr><td>[/URL]
<div style="font-family:Georgia,Trebuchet MS,Times New Roman;font-size:16px;color:#000;margin-top:5px;padding:0px; height:33px; vertical-align:middle;"> <span style="margin-left:20px; margin-right:20px;"><a style="color:black;text-decoration:none;" href="[URL unfurl="true"]http://www.goorin.com/hats">Shop</a></span>[/URL] <span style="margin-right:20px;"><a style="color:black;text-decoration:none;" href="[URL unfurl="true"]http://www.goorin.com/blog">Blog</a></span>[/URL] <span style="margin-right:20px;"><a style="color:black;text-decoration:none;" href="[URL unfurl="true"]http://www.goorin.com/about-goorin">The[/URL] Goorin Story</a></span> <span style="margin-right:20px;"><a style="color:black;text-decoration:none;" href="[URL unfurl="true"]http://www.goorin.com/hat-shops">Our[/URL] Hat Shops</a></span> <span style="margin-right:20px;"><a style="color:black;text-decoration:none;" href="[URL unfurl="true"]http://www.goorin.com/faq">Customer[/URL] Service</a></span> </div>
</td></tr></table></td>
</tr>
<tr>
<td class="callout">SHIPPING CONFIRMATION</td>
</tr>
<tr>
<td><table width="800">
<tr>
<td width="530" align="left" valign="top"><p class="action">
Your order has shipped. </p>
<p style="font-family:Georgia,Trebuchet MS,Times New Roman;">You''re just a few days away from looking (even more) stylish in your new Goorin hat.<br /><br />
Now that you''ve ordered with us, keep up to date on the latest Goorin happenings and deals via our <a href="http:/[URL unfurl="true"]www.goorin.com/blog">blog</a>,[/URL] our <a href="[URL unfurl="true"]http://www.facebook.com/goorinbrothers">Facebook[/URL] Page</a>, and our <a href="[URL unfurl="true"]http://www.twitter.com/goorinbros">Twitter[/URL] stream</a>.<br /><br />
When you receive your order, take a photo of yourself in your new hat and publish it to our <a href="[URL unfurl="true"]http://www.facebook.com/goorinbrothers">Facebook[/URL] wall</a> to receive 15% off your next purchase.</p></td>
<td> </td>
<td><p><img src="[URL unfurl="true"]http://www.goorin.com/sites/default/files/templates/orderproc/400x400.jpg"[/URL] alt="" width="400" height="300" /></p>
<p><em>Mr. Fitzall''s on his way with your new Goorin hat!</em></p></td>
</tr>
</table></td>
</tr>
<tr>
<td class="callout">INVOICE DETAILS
</td>
</tr>
<tr>
<td align="center">
<table border="0" cellpadding="2" cellspacing="0" width="400">
<tr>
<td valign="top">
<table border="0" cellpadding="2" cellspacing="1" width="100%" style="border-collapse: collapse; border-color:#d3d3d3;">
<tr>
<td bgcolor="#D8D8D8" valign="top" class="invoicedetails">Order #
</td>
<td bgcolor="#FFFFFF" valign="top" class="invoicedetails">' + @numatcard + '</td>
</tr>
<tr>
<td bgcolor="#D8D8D8" valign="top" class="invoicedetails">Reference #
</td>
<td bgcolor="#FFFFFF" valign="top" class="invoicedetails">' + @docnum + '</td>
</tr>
<tr>
<td bgcolor="#D8D8D8" valign="top" class="invoicedetails">Order shipped
</td>
<td bgcolor="#FFFFFF" valign="top" class="invoicedetails">' + @docdate + '</td>
</tr>
<tr>
<td bgcolor="#D8D8D8" valign="top" class="invoicedetails">Ship Method</td>
<td bgcolor="#FFFFFF" valign="top" class="invoicedetails">UPS Ground</td>
</tr>
<tr>
<td bgcolor="#D8D8D8" valign="top" class="invoicedetails">Tracking number</td>
<td bgcolor="#FFFFFF" valign="top" class="invoicedetails">' + @trackno + '</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="callout">
QUESTIONS FEEDBACK</td>
</tr>
<tr>
<td>
<p>Don''t like your hat? Is it the wrong fit? </p>
</td>
</tr>
</table>
</td>
</table>'
SET @subject1 = 'Your Goorin Brothers Order: ' + @numatcard + ' is on its way!'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MyMailProfile',
@recipients = @mailrecipients,
@subject = @subject1,
@body = @newBody,
@body_format = 'HTML';
UPDATE #maillist set processed = 1 where CardName = @cardname
END;
SELECT * FROM #maillist;
DROP TABLE #maillist;
However, while the temp table returns 72 records, I only get one email. I am sure it is just the placement of my statements or the ommission of an 'END', but I can't figure it out. Could someone offer some advice or just a pair of fresh eyes?
Thanks in advance,
Donald