Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Submit form crashes but is saving to database but not sending email

    Hi,

    Before delving into the code itself, I have a friend for whom I've built a form which saves the details to a database and then continues to send out an email. Nothing difficult, but he has terrible internet connection and sometimes once he has filled in the form and click submit the post page is an error page. (I need to get the exact error from him but I guess its just a 404 no internet page)

    Anyway, when this happens the information is still somehow saved into the database but the email code which follows afterwards never gets sent out. How can this happen?

    Thanks
    D.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    The problem is on line 732, where you have a comma in place of a semi-colon.

    No? You don't have a line 732?

    Ah, well, someday that will be the right answer.

    HINT: No way to possibly guess without seeing the code.

    I will say that if the data is saved to the database *AND* the code to send the email is on the same ASP page, then the problem can't be a 404 error if the data indeed gets saved.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Fair play.. sarcasm noted.

    I thought there might be some instance where database entries might work and email systems not when embedded on the same page and a internet connection is broken. Was looking for a generalised response. I'll post the code shortly.

    thanks

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    It does occur to me that you might be getting a 404 because you have error detection code in the email sending part that then sends the user to a page to report the error and that reporting page doesn't exist. A little far-fetched, but possible.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    To be honest Pedant, I don't know what error he is getting now. The end user can't confirm.. but I've asked him to print screen the error and send it to me the next time it occurs.

    The script is as below:

    Code:
    <% @LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%Session.LCID = 2057%>
    <%Server.ScriptTimeout = 200 %>
    <!--#include virtual="/Connections/my_connDB.asp" -->
    <%
    '--- Set Variables
    Dim strDate, strName, strSurname, strWantsToGo, strEmail 
    
    '--- request variables
    
    strEmail = Request("Email")
    strDate = year(Request("DateAdded"))  & "/" & month(Request("DateAdded"))  & "/" & day(Request("DateAdded"))  
    strName = Request("FirstName")  
    strSurname = Request("SurName")
    
    strWantsToGo = Request("canICome")
    if strWantsToGo = "" then
    strWantsToGo = "false"
    end if
    
    '--- check for valid email
    If strEmail="" OR instr(strEmail,"@")=0 OR instr(strEmail,".")=0 Then
    response.redirect("/manager/client_invalidemail.asp")
    Else
    
    '--- add record
    Dim oRS
    set oRS = server.createobject("ADODB.Recordset")
    
    oRS.source = "myDB"
    oRS.activeconnection = objConn
    oRS.CursorType = 2 ' Use 2 instead of adCmdTable
    oRS.LockType = 3 ' Use 3 instead of adLockOptimistic
    oRS.open
    			
    			oRS.AddNew
    		
    '--- Insert into recordset
    
    			oRS.Fields("DateAdded").Value = strDate
    			oRS.Fields("FirstName").Value = strName
    			oRS.Fields("SurName").Value = strSurname
    			oRS.Fields("ComingAlong").value = strWantsToGo
                                          oRS.Fields("emailAddress").value = strEmail 
    	
    			'--- Update the recordset
    			On Error Resume Next
    				oRS.Update
    				If Err.Number <> 0 Then
    					response.redirect("/manager/error.asp")
    				else
    				End If
    				End If
    			On Error Goto 0
    			
    '---end check valid email
    end if
    
    '--- email 
    
    Dim ObjSendMail
    Set ObjSendMail = CreateObject("CDO.Message") 
         
    'This section provides the configuration information for the remote SMTP server.
         
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="HIDDEN"
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
         
    ' If your server requires outgoing authentication uncomment the lines bleow and use a valid email address and password.
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="HIDDEN"
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="HIDDEN"
         
    ObjSendMail.Configuration.Fields.Update
    
    ObjSendMail.To = Request("Email")
    ObjSendMail.Subject = "Am I coming"
    ObjSendMail.From = "HIDDEN"
    
    ObjSendMail.HTMLBody = "html here"
         
    ObjSendMail.Send
    
    If Err = 0 Then
    Response.redirect "/manager/added_ok.asp"
    else
    Response.Write "The details were added successfully but the auto email was not sent. The error repsonse is: " & Mailer.Response
    End if
    
    
    Set ObjSendMail = Nothing
    
    '--- Close ADO objects
    			oRS.Close
    			Set oRS = Nothing
    
    			objConn.Close
    			Set objConn = Nothing
    
    %>
    Hate the way I have to catch the date, but it seems to be the only way it adds it in the correct format.
    Last edited by dommy; 07-01-2012 at 02:40 PM.

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Anyone got any idea as to how or why the code above would fail to send the email but still record the data other than the email server or its information not working. I know this is all correct and working as this issue seems random - at times both parts of the script will send at others it wont.

    Perhaps I just need to enclose the two parts so that if the email succeeds, complete the submission of info to the database... but how best to tackle that? At least this way either nothing works or everything works. Any help is appreciated.

  • #7
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Code:
    If Err.Number <> 0 Then
    response.redirect("/manager/error.asp")
    what is the value of Err if no error is thrown? is it 0 or is it null? (I don't know if it is answer or not but... null != 0)

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Quote Originally Posted by alykins View Post
    Code:
    If Err.Number <> 0 Then
    response.redirect("/manager/error.asp")
    what is the value of Err if no error is thrown? is it 0 or is it null?
    It is zero. So that code, per se, is okay. But maybe the problem is that the page "/manage/error.asp" doesn't exist?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Thanks OldPedant- I don't like to assume anything...
    @OP; I think using debug would go along way :| for you...
    for instance the chunk I quoted would redirect if Err is not 0, if it wasn't 0 then it would/should throw out this
    Code:
    If Err = 0 Then
    Response.redirect "/manager/added_ok.asp"
    else
    Response.Write "The details were added successfully but the auto email was not sent. The error repsonse is: " & Mailer.Response
    End if
    ... so what does that say?
    do these pages exist?
    /manager/added_ok.asp
    /manager/error.asp

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    So many things wrong in that ASP code it's almost impossible to tell where the error is coming from.

    The WORST part is this line:

    Response.Write "The details were added successfully but the auto email was not sent. The error repsonse is: " & Mailer.Response

    Apparently, this code used to be using some other email sending product, using the name Mailer. But it isn't any longer, and there is no such thing as Mailer.

    So the very line that would tell you there is an error *HAS* an error, so it will never execute. The code will just stop on that line, with an error.

    But there are many many other things wrong about that page. It is just crying out to be rewritten.

    Question: What kind of database are you using? Access? SQL Server? MySQL?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    New Coder
    Join Date
    May 2006
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow that bad hey?

    Ok it seems somewhere along the line I've mixed scripts here so my apologies, not sure what happened.. the Mailer.Response is wrong yes and I'll fix that.

    Pendant its writing to access, its all quite old school now.

    "But there are many many other things wrong about that page. It is just crying out to be rewritten." - if you have time I'd appreciate you explaining further. Thanks
    Last edited by dommy; 07-12-2012 at 01:07 PM.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    One of the really ugly things wrong is that you have too many END IFs! Count the IFs, count the END IFs. They don't match. But your incorrect usage of ON ERROR is hiding that!

    Other than that, your EMAIL validation is next to useless and you have no validation at all of the names or the date.

    So...

    Code:
    <% @LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%Session.LCID = 2057%>
    <%Server.ScriptTimeout = 200 %>
    <%
    
    '--- request variables
    ' using TRIM ensures that you *DO* get a string from Request
    strEmail = Trim(Request("Email"))
    dateAdded = Trim(Request("DateAdded"))
    strName = Trim(Request("FirstName"))
    strSurname = Trim(Request("SurName"))
    strWantsToGo = Trim(Request("canICome"))
    
    ' now, validate the fields:
    ' easy one first:
    if strWantsToGo = "" then
        strWantsToGo = "false"
    end if
    
    ' this regular expression is used to remove all non-letter characters from the names
    Set reName = New RegExp
    reName.pattern = "[^a-z]"
    reName.global = True
    reName.ignoreCase = True
    
    ' now see if the names, alphabetic only, are long enough:
    If    Len( reName.Replace( strName, "" ) ) < 3 _
       OR Len( reName.Replace( strSurname, "" ) ) < 3 _
    Then
        response.Redirect "/manager/client_invalidname.asp" ' ??? add this one
        ' when you REDIRECT you do so *NOW* and no other statements get executed
        ' so there is no reason to use ELSE!
    End If
    
    ' --- check for valid date
    On Error Resume Next
        dateAdded = CDate( dateAdded )
        If Err.Number > 0 Then
            response.Redirect "/manage/client_invaliddate.asp" ' ??? and this one
        End If
    On Error GoTo 0
    
    ' -- and check for valid email...the GOOD way, using a pattern:
    Set reMail = New RegExp
    reMail.pattern = "^[a-z][\w\'\-\.]+\@([a-z][\w\'\-]*\.)+[a-z]{2,6}$"
    reMail.IgnoreCase = True
    
    If Not reMail.Test(strEmail) Then
        response.redirect("/manager/client_invalidemail.asp")
    End If 
    
    ' -- don't open the connection until you know it will be used:
    
    %><!--#include virtual="/Connections/my_connDB.asp" --><%
    
    '--- add record
    Dim oRS
    set oRS = server.createobject("ADODB.Recordset")
    
    oRS.source = "myDB"
    oRS.activeconnection = objConn
    oRS.CursorType = 3 ' adOpenStatic -- do NOT use adOpenDynamic
    oRS.LockType = 3 ' adLockOptimistic
    oRS.open
    			
    oRS.AddNew
    		
    '--- Insert into recordset
    
    ' THIS IS A BAD IDEA!  
    ' If the field DATEADDED in the DB *is* a DATETIME field, then you should NOT do this!
    strDate = year(DateAdded)  & "/" & month(DateAdded)  & "/" & day(DateAdded)  
    oRS("DateAdded") = strDate
    
    ' if the field *IS* a DATETIME field, as it should be, then just do this:
    oRS("DateAdded") = DateAdded
    
    oRS("FirstName") = strName
    oRS("SurName") = strSurname
    oRS("ComingAlong") = strWantsToGo
    oRS("emailAddress") = strEmail 
    	
    '--- Update the recordset
    On Error Resume Next
        oRS.Update
        If Err.Number <> 0 Then
            oRS.Close
            objConn.Close
    	response.redirect("/manager/error.asp")
        End If
    On Error Goto 0
    
    ' done with db stuff...close it *NOW*
    oRS.Close
    objConn.Close
    			
    ' **********************************
         
    ' This section provides the configuration information for the remote SMTP server.
    Set objConfig=CreateObject("CDO.Configuration")
    CONST SCHEMA = "http://schemas.microsoft.com/cdo/configuration/"  
    
    objConfig.Fields.Item (SCHEMA & "sendusing") = 2 
    objConfig.Fields.Item (SCHEMA & "smtpserver") ="HIDDEN"
    objConfig.Fields.Item (SCHEMA & "smtpserverport") = 25 
    objConfig.Fields.Item (SCHEMA & "smtpusessl") = False 'Use SSL?
    objConfig.Fields.Item (SCHEMA & "smtpconnectiontimeout") = 60
    objConfig.Fields.Item (SCHEMA & "smtpauthenticate") = 1 'basic (clear-text) authentication
    objConfig.Fields.Item (SCHEMA & "sendusername") ="HIDDEN"
    objConfig.Fields.Item (SCHEMA & "sendpassword") ="HIDDEN"
         
    objConfig.Fields.Update
    
    ' now the email itself
    Set ObjSendMail = CreateObject("CDO.Message") 
    
    Set objSendMail.Configuration=objConfig
    
    ObjSendMail.To = Request("Email")
    ObjSendMail.Subject = "Am I coming"
    ObjSendMail.From = "HIDDEN"
    
    ObjSendMail.HTMLBody = "html here" ' ??? I hope this was only to show us in forum??
    
    On Error Resume Next     
        ObjSendMail.Send
        Set ObjSendMail = Nothing
        If Err = 0 Then
            Response.redirect "/manager/added_ok.asp"
        End If
        Response.Write "The details were added successfully but the auto " _
                       & " email was not sent.<br/> The error repsonse is: " & Err.Description
    On Error GoTo 0
    %>
    Some of the rewrite I did there wasn't necessary, but I did it just to make the code more readable.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Oh, by the by...you apparently thought that the 2 in
    Code:
    oRS.CursorType = 2
    meant "adCmdTable". Nope. Not true at all. For a CursorType, 2 means "adOpenDynamic". But ASP and VBScript code can't use adOpenDynamic in any useful way (it requires that you provided a CALLBACK function in case somebody else modifies the record before you do, but neither ASP nor VBScript provide a way to handle callbacks). So you should instead always use "adOpenStatic" which is the value 3.

    adCmdTable would be used for CommandType, which you aren't supplying here (and which isn''t needed).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    May 2006
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey Old Pedant, thank you very much for your time and knowledge. I really appreciate it! I've been learning ASP over the years in spare time as a bit of a hobby and I have managed to do some great things with it but alas there's always more to understand and having written some stuff and then getting it reviewed is a great way to progress so thank you.

    P.S. I was editing this code down so there's a good chance I left an unnecessary end if in there. P.S. Would be so nice if there was a better way of dealing with if's especially when they span the length of a page before being closed. Gets confusing.

    I'll read over this tonight.

  • #15
    New Coder
    Join Date
    May 2006
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Old Pedant

    This info is very useful, I can see how it cleans up relay errors and prevents scripts from running without pre checks. I don't think my code was too 'error' full, but its ordering is where I think I needed to pay closer attention.
    One thing though, what if you have a form thats 20-25 fields large? Do you really need to treat each one when capturing data and if so surely there's a better way with some presets?? Is this just ASP showing its age?

    I can't get the shorthand SCHEMAS to work though.. kicks up an error the first line it hits which is

    Code:
    objConfig.Fields.Item (SCHEMA & "sendusing") = 2
    Why use
    Code:
    CONST SCHEMA =
    and not simply
    Code:
    Dim SCHEMA
    SCHEMA = ...
    Also I finally found out that the reason this code is sometimes adding the information to the database but not sending out the email is because I have the error code now and it seems to be mail server related. ERROR: '80040211' and it happens on
    Code:
    ObjSendMail.Send
    Thanks


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •