Wednesday, 2 September 2015

Sending Mail via SQL server using Escalation matrix


create PROC [dbo].[UDSP_Escalation_SendMail]
as
DECLARE EscalationMail Cursor FOR  
              SELECT
 PendingDays,
 CreatedOn  ,EmailID
 FROM table1



 declare @Email_to varchar(100)=''
 declare @Email_ToFare varchar(100)
 declare @Email_CC varchar(max)=''
 declare @MailSubject varchar(1000) = ''
 DECLARE @tableHTML  NVARCHAR(MAX) ;
 declare @URL varchar(50)=''

 declare @PendingDays int=0
 declare @EmailID varchar(100)
 declare @CreatedOn varchar(30)=''


   
 
 Open EscalationMail
 fetch next from EscalationMail into @PendingDays,@CreatedOn
  while (@@fetch_status = 0)
  begin
  if(@PendingDays <>0)
  begin

set @URL = 'http://ABC.in/'
if(@PendingDays >= 1 and @PendingDays <= 3)
begin
if(@EmailID <> '')
begin
set @Email_ToFare=@EmailID + ';' + (select EmailTo from Table2 where EscalationLevel=1 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=1 and Status='1')

end
else
begin
   set @Email_ToFare=(select EmailTo from Table2 where EscalationLevel=1 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=1 and Status='1')

end
end
else
begin
if(@PendingDays >= 4 and @PendingDays <= 6)

begin
if(@EmailID <> '')
begin
set @Email_ToFare=@EmailID + ';' + (select EmailTo from Table2 where EscalationLevel=2 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=2 and Status='1')

end
else
begin
   set @Email_ToFare=(select EmailTo from Table2 where EscalationLevel=1 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=1 and Status='1')

end
end
else
begin
if(@PendingDays >= 7 and @PendingDays <= 10)

begin
if(@EmailID <> '')
begin
set @Email_ToFare=@EmailID + ';' + (select EmailTo from Table2 where EscalationLevel=3 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=3 and Status='1')

end
else
begin
   set @Email_ToFare=(select EmailTo from Table2 where EscalationLevel=3 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=3 and Status='1')

end
end
end
   end
 
 if(@PendingDays > 11)
begin
if(@EmailID <> '')
begin
set @Email_ToFare=@EmailID + ';' + (select EmailTo from Table2 where EscalationLevel=4 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=4 and Status='1')

end
else
begin
   set @Email_ToFare=(select EmailTo from Table2 where EscalationLevel=4 and Status='1')
set @Email_CC=(select EmailCC from Table2 where EscalationLevel=4 and Status='1')

end
 end

 
   SET @tableHTML = '';
   SET @tableHTML =
    N'<table>' +
    N'<tr><th colspan="2" align="left">Dear Sir/Madam,</th></tr>' +
    N'<tr><th colspan="2" align="left"></th></tr>' +
    N'<tr><td>Pending Day:-</td><td>' + CONVERT(varchar(20), @PendingDays) +'</td></tr>' +
    N'<tr><td>Assign Date:-</td><td>' + ISNULL(@CreatedOn, '1900-01-01 00:00:00.000')  +'</td></tr>' +
    N'<tr><td>Type:-</td><td>Escalation</td></tr>' +
    N'<tr><td>Priority:-</td><td>High</td></tr>' +
    N'<tr><td>Description:-</td><td>This Transaction is open.To know more details please log on to <a><font color="#0000cc">'+@URL+'</font></a></td></tr>' +
    N'<tr><th colspan="2" align="left"></th></tr>' +
    N'<tr><th colspan="2" align="left"><img src="http://SiteName/Images/logo.JPG"/></th></tr>' +
    N'<tr><td colspan ="2">Regards</td></tr>' +
    N'<tr><td colspan ="2">Admin Team</td></tr>' +
    N'<tr><th colspan="2" align="left"></th></tr>' +
    N'<tr><td colspan ="2"><i><u>Please do not reply.This in an autogenerated mail.</u></i></td></tr>' +
    N'</table>' ;
    set @MailSubject =  'Please ignore testing mail'
       
    EXEC [msdb].[dbo].[sp_send_dbmail]
    @profile_name = 'Your SQL server Mail server Profile Name will come here',
    @recipients = @Email_to,
    @copy_recipients = @Email_CC,
    @subject = @MailSubject,
    @body = @tableHTML,    
    @body_format = 'HTML'
    set @MailSubject = ''
    end
   fetch next from EscalationMail into @PendingDays,@CreatedOn
   end  
 CLOSE EscalationMail
 DEALLOCATE EscalationMail  

No comments:

Post a Comment