Greetings again mates,
The following stored proc performs several different tasks at once.
At the top of the SP, the SELECT statement queries two tables (Employees and AuthorizedUsers) to pull names and email addresses of employees based on some WHERE predicate.
This works great.
The second part stores the data pulled from the tables above into another table called Notifications table.
This works great as well.
The third part of the task the SP does is to retrieve records from Notifications table where the value of a flag called IsSent = No.
Finally, the data pulled from Notifications table is sent to the email addresses also pulled from the Notifications tables and after the emails are sent, then IsSent is updated to Yes.
So far, almost everything works except that the emails are not being sent using sp_send_dbmail.
I am having difficulty figuring out the source of the problem.
Any assistance is greatly appreciated.
CREATE PROCEDURE [dbo].[GetRegistrationInfo]
AS
BEGIN
DECLARE Register_Cursor CURSOR FOR
SELECT Name, COALESCE(NULLIF(PersonalEmail,''),WorkEmail) AS EmailAddress
FROM AuthorizedUsers au
WHERE NOT EXISTS (
SELECT *
FROM Employees e
WHERE e.email IN (au.work_email, au.home_email)
ORDER BY Name ASC
OPEN Register_Cursor
DECLARE @fullname NVARCHAR(100)
DECLARE @email NVARCHAR(MAX)
-- Get the current MAX ID
DECLARE @mailID as INT
-- Start reading each record from the cursor.
FETCH Register_Cursor INTO @fullname, @email
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO [Notifications] (mailContent, FullName, email_Addr, sender, IsSent)
VALUES ('This is a computer generated email message.
Please DO NOT use the REPLY button above to respond to this email.
Dear '+@FullName+':
Thanks for registering for the Training!
Below are details of your registration information:
Your UserName is: '+@email+'.
Your Password is: '12334r'.
Once you have retrieved your login information, please click the link below to get back to Training login screen and begin to begin to enjoy the benefits of membership.
http://servername/training/
Regards,
The Registrations & Elections Office.', @FullName, @email, 'NoReply@serverdomain', 'No')
FETCH Register_Cursor INTO @FullName, @email
END
CLOSE Register_Cursor
DEALLOCATE Register_Cursor
END
BEGIN
DECLARE MAIL_CURSOR CURSOR FOR
SELECT mailid, sender, mailcontent
FROM [Notifications]
WHERE IsSent = 'No'
DECLARE @mail1 INT
DECLARE @sender NVARCHAR(100)
DECLARE @content1 NVARCHAR(4000)
OPEN MAIL_CURSOR
FETCH MAIL_CURSOR INTO @mail1, @sender, @content1
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @email = @email + ';' + Email
FROM [Notifications]
WHERE IsSent = 'No'
-- exec sp_send_cdontsmail @mail1, null,null,@content1,null
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Elections_Office',
@recipients = @email,
@subject = 'Your Account Details',
@body = @content1;
-- Update the record in Notifications table where IsSent = 'No'.
UPDATE [Notifications]
SET IsSent = 'Yes'
WHERE IsSent = 'No' AND mailid = @mail1
FETCH MAIL_CURSOR INTO @mail1, @sender, @content1
END
CLOSE MAIL_CURSOR
DEALLOCATE MAIL_CURSOR
END
I believe my issue lies in this snip:
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @email = @email + ';' + Email
FROM [Notifications]
WHERE IsSent = 'No'
-- exec sp_send_cdontsmail @mail1, null,null,@content1,null
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Elections_Office',
@recipients = @email,
@subject = 'Your Account Details',
Thanks in advance for your assistance