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