Send email before or after inserting to DB

Hey Experts!!

I have a small application(classic ASP/VBScript) that inserts a record into a SQL Server 2000 DB. What I'd like to do is fire off an email to 1 person either before or after the insert. I'm using Jquery/AJAX to perform the insert. I was just wondering if there was a way to do it with JS rather than ASP?

Thanks in Advance
LVL 30
LZ1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Not really since javascript is restricted to the browser.  The same ASP page that receives the insert could send the email from the server.
0
ncooCommented:
As has already been said you can't do it in Javascript alone.

When it comes to the Ajax page, always do the database write first, then send the email.

If something goes wrong when sending the email, you won't have the database record if you attempt to write it after, the script may have terminated before this.
0
LZ1Author Commented:
I understand that JS can't do it alone.  I was wondering how/if I could do it in the same ajax call?? I've never used ASP to mail anything. PHP yes, but not ASP.  

I have no idea where to even start.  Any direction/help/tutorials are all greatly appreciated.

Here is my current AJAX script:
$(function() {
  $('.error').hide();

  $(".submit").click(function() {
	var dataString =  $('form#idea').serialize();
		 
		//alert (dataString);return false;
		
	$.ajax({
      type: "POST",
      url: "/ideaCenter/insertNew.asp",
      data: dataString,
      success: function() {
        $('#idea').html("<div id='message'></div>");
        $('#message').html("<div id='thankyou'><img id='checkmark' src='/images/check.png' /><h3> Thank You! </h3></div>   <p>Thank you for taking time to provide feedback. A response to each comment will be decided once all responses are received.</p>")
        .hide()
        .fadeIn(500, function() {
          $('#message').append("");
        });
      }
     });
    return false;
	});
});

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Dave BaldwinFixer of ProblemsCommented:
What web server and hosting are you running on and does it have a SMTP server installed on it?
0
LZ1Author Commented:
It's an internal application running on Server 2003 with IIS 6. SMTP is installed and currently running on that server as well.
0
Dave BaldwinFixer of ProblemsCommented:
This demo runs on IIS7 and will probably run on IIS6.  Put your own server in there along with your own email addresses and try.  May have to find a setting to use localhost.
<%@ LANGUAGE = VBScript %>
<%  Option Explicit		%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>CDO-Email</title>
</head>
<body>
<h1>CDO-Email</h1>
<p>Godaddy demo code.</p>
<%
Dim sendUrl, smtpUrl, objConfig, objMail

sendUrl="http://schemas.microsoft.com/cdo/configuration/sendusing"
smtpUrl="http://schemas.microsoft.com/cdo/configuration/smtpserver"

' Set the mail server configuration
Set objConfig=CreateObject("CDO.Configuration")
objConfig.Fields.Item(sendUrl)=2 ' cdoSendUsingPort
objConfig.Fields.Item(smtpUrl)="your.host"
objConfig.Fields.Update

' Create and send the mail
Set objMail=CreateObject("CDO.Message")
' Use the config object created above
Set objMail.Configuration=objConfig
objMail.From="yourFrom@email.net"
objMail.ReplyTo="yourFrom@email.net"
objMail.To="yourTo@email.net"
objMail.Subject="This are the subject"
objMail.TextBody="And this are the bodybody."
objMail.Send
%> 
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Here http://www.w3schools.com/asp/asp_send_email.asp is a page on using CDO email.  I think Microsoft has a more detailed page.
0
LZ1Author Commented:
So how would I call this in my Jquery?
0
Dave BaldwinFixer of ProblemsCommented:
You can figure that out after you test it as a stand alone demo.  But basically you add the email code to the end of your database code that you are already calling.
0
LZ1Author Commented:
I did test it and it sends perfectly. Just a few modifications to make it pull the POST data and we should be good.

So this would go at the end of my INSERT file that I'm referencing in the Jquery?
0
Dave BaldwinFixer of ProblemsCommented:
As @ncoo suggested, I would put the ASP email code After the database code in that file.  I would also make it so it doesn't get sent if the INSERT doesn't work for some reason.  Or it could send an error message if you wanted.
0
LZ1Author Commented:
Here is the ASP code I have. The insert is firing, but the email is not. I'm not receiving any errors client side either.  

Any suggestions?
<%
	If IsNumeric(Request("start")) and Request("start")<>"" then
		   strStart = CInt(Request("start"))
	Else
		   strStart = Null
	End if
	
	If IsDate(Request("submitDate")) and Request("submitDate")<>"" then
		   strDate = CDate(Request("submitDate"))
	Else
		   strDate = Null
	End if
	
	If IsDate(Request("modifiedDate")) and Request("modifiedDate")<>"" then
		   strDate = CDate(Request("modifiedDate"))
	Else
		   strDate = Null
	End if
	
	
	Conn = "Provider=SQLOLEDB; Server=***; Uid=***; Pwd=***; Database=***;"
	Set MM_editCmd = Server.CreateObject("ADODB.Command")
	MM_editCmd.ActiveConnection = Conn
	MM_editCmd.CommandText = "INSERT INTO dbo.tblideaCenter (name, cont, stop, [start], submitDate, modifiedDate) VALUES (?, ?, ?, ?, ?, ?)" 
	MM_editCmd.Prepared = true
	MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 201, 1, 50, Request.Form("name")) ' adLongVarChar
	MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 201, 1, 1000, Request.Form("cont")) ' adLongVarChar
	MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 201, 1, 1000, Request.Form("stop")) ' adLongVarChar
	MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 201, 1, 1000, Request.Form("start")) ' adLongVarChar
	MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param5", 201, 1, 25, Request.Form("submitDate")) ' adLongVarChar
	MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param6", 201, 1, 25, Request.Form("modifiedDate")) ' adLongVarChar
	MM_editCmd.Execute
	
	
	Dim sendUrl, smtpUrl, objConfig, objMail
	
	sendUrl="http://schemas.microsoft.com/cdo/configuration/sendusing"
	smtpUrl="http://schemas.microsoft.com/cdo/configuration/smtpserver"
	
	' Set the mail server configuration
	Set objConfig=CreateObject("CDO.Configuration")
	objConfig.Fields.Item(sendUrl)=2 ' cdoSendUsingPort
	objConfig.Fields.Item(smtpUrl)="10.1.1.21"
	objConfig.Fields.Update
	
	' Create and send the mail
	Set objMail=CreateObject("CDO.Message")
	' Use the config object created above
	Set objMail.Configuration=objConfig
	objMail.From="***@***.com"
	objMail.ReplyTo="***@***.com"
	objMail.To="***@***.com"
	objMail.Subject="This is the subject"
	objMail.TextBody=("Name:")
	objMail.TextBody=Request.Form("name")
	objMail.TextBody=("Continue:")
	objMail.TextBody=Request.Form("cont")
	objMail.TextBody=("Stop:")
	objMail.TextBody=Request.Form("stop")
	objMail.TextBody=("Start:")
	objMail.TextBody=Request.Form("start")
	objMail.Send
	
	MM_editCmd.ActiveConnection.Close

%>

Open in new window

0
LZ1Author Commented:
Just had to specify the right host.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.