我们编写了一个存储过程,它使用 SQL Server 的 send_dbmail 功能向潜在用户发送电子邮件,其中包含他们的注册详细信息。
这个存储过程很好用。
我们目前唯一的问题是用户还想索取一份他们忘记的密码的副本。
我可以写另一个存储过程来处理这个吗?但是,有没有办法修改下面的存储过程,使其识别请求的模式?
例如,当前用户创建了一个帐户,该帐户的详细信息存储在tblLogin
表中。
此存储过程获取帐户信息,将其存储在 Notifications 表中,最后将该帐户的详细信息通过电子邮件发送给刚刚注册的用户。
Forgotten Password
同样,当用户使用该功能请求密码时,我们希望处理相同的存储过程。
如何根据请求修改存储过程发送电子邮件?
换句话说,如果存储在tblLogin
表中的数据用于new account
创建,则存储过程应该获取它并向用户发送一封包含详细信息的电子邮件。
如果数据用于forgotten password
,则存储过程也应该只通过电子邮件发送该信息。
这可以通过下面的存储过程实现吗?
ALTER PROCEDURE [dbo].[GetRegistrationInfo]
AS
BEGIN
DECLARE Register_Cursor CURSOR FOR
SELECT
LoginId, FullName, email, Password
FROM
[tblLogin]
WHERE
ProcessedFlag = 'No'
ORDER BY
LoginId DESC
OPEN Register_Cursor
DECLARE @LoginId INT
DECLARE @fullname NVARCHAR(100)
DECLARE @email NVARCHAR(MAX)
DECLARE @password NVARCHAR(20)
-- Get the current MAX ID
DECLARE @mailID as INT
-- Start reading each record from the cursor.
FETCH Register_Cursor INTO @LoginId, @fullname, @email, @password
WHILE @@FETCH_STATUS = 0
BEGIN
--set @mailID = (SELECT max(mailID) from Notifications) Not needed; let's auto-genereate the id
INSERT INTO [Notifications] (mailContent, LoginId, FullName, email, Password, sender, Sent)
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 to take the Training!
Below are details of your registration information:
Your UserName is: '+@email+'.
Your Password is: '+@password+'.
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.', @LoginId, @FullName, @email, @Password, 'NoReply@serverdomain', 'No')
FETCH Register_Cursor INTO @LoginId, @FullName, @email, @password
END
CLOSE Register_Cursor
DEALLOCATE Register_Cursor
END
BEGIN
DECLARE MAIL_CURSOR CURSOR FOR
SELECT mailid, sender, mailcontent
FROM [Notifications]
WHERE Sent = '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 sent = 'No'
-- exec sp_send_cdontsmail @mail1, null,null,@content1,null
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'The Office',
@recipients = @email, -- your email
--@blind_copy_recipients = @email,
@subject = 'Your Account Details',
@body = @content1;
-- Update the record in Notifications table where Sent = 'No'.
UPDATE [Notifications]
SET Sent = 'Yes'
WHERE Sent = 'No' AND mailid = @mail1
UPDATE [tblLogin]
SET ProcessedFlag = 'Yes'
WHERE ProcessedFlag = 'No' AND LoginId = @LoginId
FETCH MAIL_CURSOR INTO @mail1, @sender, @content1
END
CLOSE MAIL_CURSOR
DEALLOCATE MAIL_CURSOR
END