-1

I'm looking for a way to send emails (or reminders/confirmations) to users who create a new record in a web application, which is then inserted into a table. Basically a dynamic way of sending emails.

I've been reading online about Triggers and DB mail and there seems to be alot of disadvantages going with this approach.

Does anyone have any recommendations on the best way to achieve this?

Flow: New Record Inserted into DB Table ->>> At this point the email address of the user who created the record in the application should receive a mail (basically a confirmation mail).

What I've tried already:

DB mail is already configured and working. I've made the below Trigger (very basic) but from reading online using a trigger in this way will lead to load/performance issues of my DB.

But I'm on unsure on how to generate the emails and last record inserted.

CREATE TRIGGER [dbo].[INSERT_Trigger] ON [dbo].[Entity]

FOR INSERT
AS


EXEC msdb.dbo.sp_send_dbmail

   @profile_name = 'DBMail',

@recipients = 'admni@domain.com', ####Here I would need dyncamic emails based on what user enters the new record which are stored in the same table

@query = 'Select Description From Entity where 'Last inserted record'####Here I would need the last record for that user entered

@subject = 'Subject' ,

@Body = 'Message Body',

@importance = 'High',
Hugh
  • 1
  • 1

2 Answers2

1

IMHO this approach is a design flaw: the DB tier is something that should be one of the leaves of the tier tree. The fact that the MS SQL Server is actually an application server and has support for such things is a legacy, but I don't think that it should be used.

At first look:

  • you might need to switch to another RDBMS
  • your production environment might not support SMTP for any reason
  • your attempt to send the mail could fail for various reasons - resulting in user not being notified and never trying it again

Yes, indeed, you can use SQL Server even as a message bus but would not be an efficient one. This concept is actually dispatching events of "notification needed" kind. The event is implemented as insert and the trigger is the consumer. But the event is produced inside your application, in a higher tier. Why not reacting to it there? Or, use the database only as a queue: store the details there, but process them in a way where you have more control.

You have not told us about the application you are creating, but I would create a separate background task (the implementation could vary depending on the application design - can be an OS-level scheduled task, windows service, or a background worker in your application) that checks periodically for mails not yet sent, and tries to send them, storing the result in the record. Things might get of course more complicated depending on load. But this way you can retry, and you are certainly taking load of the DB server, but at least you have the possibility to do so.

ZorgoZ
  • 2,974
  • 1
  • 12
  • 34
0

I have a trigger that works as u asked First insert records in temporary table from inserted table Second, declare parameters that you need Third, add a cursor in your trigger and get parameters that u need in cursor from temporary table Inside trigger you can declare recipients as u needed to be, query, and other stuffs

CREATE TRIGGER Trigger  ON [Entity]
FOR INSERT
not for replication
AS

select ins.* into #temp from inserted ins

declare @Param1 integer, @Param2 integer
declare cursor forward only for
select Col1, Col2 from #temp order by Col1
open cursor
fetch next from cursor into @Param1, @Param2
while @@fetch status = 0
begin

declare @recipients varchar (max), @query varchar(max)

select @recipient = Col1 -- or whatever col contains the recipient address
from #temp
where Col1 = @Param1 

select @query = description
from #temp
where Col2 = @Param2 -- or whatever condition give u the description for parameter

exec sp_send_dbmail
@profile_name = 'profile',
@recipients = @recipient 
@subject = 'Subject' ,
@Body = @query, 
@importance = 'High'


fetch next from cursor into @Param1, @Param2 
end
close cursor 
deallocate cursor 

drop table #temp

--- note that the body can be formatted in html format, like bellow

declare @body varchar(max)
select @body = '<html><body>'
select @body = @body+'Hello'+'<b><b>'
select @body = @body+'Here is the description:'+@query+'<b><b>'
select @body = @body+'Regards'
select @body = @body+'</body></html>'