Use ASPMaker function to send email on new row inserted

Tips submitted by ASPMaker users

Use ASPMaker function to send email on new row inserted

Postby squeek » Fri Dec 20, 2013 10:33 am

I've seen examples of sending email on an event, but they all create their own mail object, etc. In aspfn9.asp (or 10 or 11), there is an email function that handles all of that for you, ew_SendEmail, which defaults to using CDO (unless you change it in your template), and checks for that component, w3JMail, and then ASPEmail. It will use whichever one is available.

' Row Inserted event
Sub Row_Inserted(rsold, rsnew)
'dimension my own variables for recordset, sql string, recipients, and contractor
dim myRS, strsql, recipients, contractor
set myRS=server.createobject("adodb.recordset")

'recordset "rsnew" holds the values of the fields in the newly inserted record
strsql="select firstname & ' ' & lastname as contractor from contractors where id=" & rsnew("contractorid")
myRS.CursorLocation = EW_CURSORLOCATION
myRS.Open strsql, Conn, 1, EW_RECORDSET_LOCKTYPE
contractor=myRS("contractor")
myRS.close

strsql="select email from employees where active and receiveemails and customerid=(select customerid from tickets where id=" & rsnew("ticketid") & ") union select email from contractors where active"
myRS.CursorLocation = EW_CURSORLOCATION
myRS.Open strsql, Conn, 1, EW_RECORDSET_LOCKTYPE

'make a comma delimited string of all recipients
do while not myRS.eof
recipients=recipients & myRS("email")
myRS.movenext
if not myRS.eof then recipients=recipients & ","
loop
myRS.close
set myRS=nothing

'sMail contains the body of the message...notes that the contractor entered, and his/her first and last name, with
'a few VBCRLFs in there to make it prettier
sMail=rsnew("notes") & vbcrlf & vbcrlf & contractor & vbcrlf & vbcrlf

'Let ASPMaker send the mail
'parameters for ew_sendmail function:
'ew_SendEmail sFrEmail, sToEmail, sCcEmail, sBccEmail, sSubject, sMail, sFormat, sCharset

return ew_SendEmail("woodybill@kconsulting.com",recipients, "", "", "New Time entered on ticket# " & rsnew("ticketid") & " by " & contractor, sMail, "", "")
End Sub
squeek
 
Posts: 31
Joined: Fri Nov 22, 2013 2:31 am

Return to User Submitted Tips (ASPMaker)