Thursday, May 17, 2018

uspSendEmail

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.uspSendEmail') AND type in (N'P', N'PC'))
DROP PROCEDURE dbo.uspSendEmail
GO

--------------------------------------------------------------------------------
-- =============================================
-- Author: Simon You
-- Create date: Feb 2018
-- Description:
-- =============================================
create procedure dbo.uspSendEmail
(
@EmailCode dbo.EmailCode,
@EmailProfile dbo.EmailProfile,
@EmailFrom dbo.EmailFrom,
@EmailTo dbo.EmailTo,
@EmailCc dbo.EmailCc,
@Subject varchar(max),
@body varchar(max)
)
as
begin
set nocount on;

  EXEC msdb.dbo.sp_send_dbmail
@profile_name = @EmailProfile,
@body = @body,
@body_format ='HTML',
@recipients = @EmailTo,
@copy_recipients = @EmailCc,
@subject = @Subject;

insert into dbo.EmailLog(EmailCode, EmailProfile, EmailFrom, EmailTo, EmailCc, EmailSubject, EmailBody)
values (@EmailCode, @EmailProfile, @EmailFrom, @EmailTo, @EmailCc, @Subject, @body)
end
--------------------------------------------------------------------------------
GO

No comments: