Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

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
0
LZ1
Asked:
LZ1
  • 6
  • 6
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now