Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP, CDO.Message, Validation, Captcha: Putting it all together?

Status
Not open for further replies.

shanoinsano

Technical User
Dec 10, 2006
3
0
0
AU
I am a Graphic Designer / Web Designer and am designing a HTML Contact Form using ASP / CDO.Message for processing & sending...

I have successfully implemented ASP CDO.Message Email Script, Required Fields Validation via the ASP Email Script and Client Side Email Address Validation Javascript...

..problem is, I am having no success adding ASP Server Side Email Validation and ASP Captcha :-|

ASP Email Script using CDO.Message:

Code:
<%
Dim strName, strEmail, strSubject, strComment

strName = Trim(Request.Form("First"))
strEmail = Trim(Request.Form("Email"))
strSubject = Trim(Request.Form("Subject"))
strComment = Trim(Request.Form("Comment"))

Dim strMessage

strMessage=strMessage &"Title: " & Request.Form("Title") & VbCrLf
strMessage=strMessage &"First Name: " & Request.Form("First") & VbCrLf
strMessage=strMessage &"Last Name: " & Request.Form("Last") & VbCrLf
strMessage=strMessage &"Business Address: " & Request.Form("Business") & VbCrLf
strMessage=strMessage &"Residential Address: " & Request.Form("Residential") & VbCrLf
strMessage=strMessage &"PO Box: " & Request.Form("POBox") & VbCrLf
strMessage=strMessage &"Work Phone: " & Request.Form("Work") & VbCrLf
strMessage=strMessage &"Home Phone: " & Request.Form("Home") & VbCrLf
strMessage=strMessage &"Mobile Phone: " & Request.Form("Mobile") & VbCrLf
strMessage=strMessage &"Comment: " & Request.Form("Comment") & VbCrLf

IF strName <> "" AND strSubject <> "" AND strComment <> "" AND strEmail <> "" THEN
   
  Set objMail = CreateObject("CDO.Message")

  	objMail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL]      = 2
	objMail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL]     = "mail.host.com"
	objMail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
	objMail.Configuration.Fields.Update
  
  objMail.From = strEmail
  objMail.To = "me@email.com"
  objMail.Subject = strSubject
  objMail.TextBody = strMessage
  objMail.Send

ELSE

Response.Redirect("error.htm")
   
  END IF
  
  Set objMail = Nothing

  Response.Redirect("thankyou.htm")
%>

I would like to incorporate this into above script...

Server Side validation Script:

Code:
set DNSobject = Server.CreateObject("CompanyABC.DNS")
	emailaddress = Request.Form("emailaddress")
	thedomainname =Mid(emailaddress,InStr(1,emailaddress,"@",vbTextCompare)+1,Len(emailaddress))
	DNSobject.domain = thedomainname
	DNSobject.server = "XXX.XXX.XXX.XXX"
	goodorbad = DNSojbect.dolookup

	If goodorbad = "" Then	'Keep in mind some DNS object may return something other than ""
		Response.Redirect("badaddress.html")
	Else
		<continue form processing>
	End If

...as well as this but not as a separate HTML Page (all inclusive to ASP Email Script at top) :)

Captcha Script:

Code:
<%
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
%>
<%
Function CheckCAPTCHA(valCAPTCHA)
	SessionCAPTCHA = Trim(Session("CAPTCHA"))
	Session("CAPTCHA") = vbNullString
	if Len(SessionCAPTCHA) < 1 then
        CheckCAPTCHA = False
        exit function
    end if
	if CStr(SessionCAPTCHA) = CStr(valCAPTCHA) then
	    CheckCAPTCHA = True
	else
	    CheckCAPTCHA = False
	end if
End Function
%>
<html>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<title></title>
</head>
<body>
<!-- Some layout HTML stripped out for length -->
<form id="f_example" name="f_example" method="post" action="example.asp">
  <img src="aspcaptcha.asp" alt="This Is CAPTCHA Image" width="86" height="21" />
<%
if Request.ServerVariables("REQUEST_METHOD") = "POST" then
	strCAPTCHA = Trim(Request.Form("strCAPTCHA"))
	if CheckCAPTCHA(strCAPTCHA) = true then
		%>
Verified (<%=strCAPTCHA%>)
		<%
	else
		%>

Not Verified. Please Retry.
		<%
	end if 
end if
%>
<input name="strCAPTCHA" type="text" id="strCAPTCHA" maxlength="8" />
<input type="submit" name="Submit" value="Test Input" />
</form>
</body>
</html>

Any Help, Suggestions very much appreciated.

Cheers

-----
Shano Insano

Always ready to tell a fictitious story,
dispense bad advice or lend an inexpert hand...
 
Your captcha script should be on the page before the page that sends the email, that will reduce the number unnecessary emails by forcing the captcha to be filled out first.
You could add the rest of your inputs to this form as well, in order to use it to also collect the email information, name, etc.
If the validation on the captcha passes, then you should Server.Transfer("emailPage.asp"). Server.Transfer transfers to the given page without losing the form information (unlike Response.Redirect).

Now, the server-side validation should occur right after you read the Form inputs into variables in your first script, right before you start building your message. you will need to substitute some variables, obviously, because your email field is called strEmail while the one in your validation script is called emailaddress.

Hope that helps,
-T

 
OK, I now have the Captcha component sorted (below), but using server.transfer causes the email script to throw a very non descript error "An error occurred on the server when processing the URL. Please contact the system administrator." The form data still gets processed and sent, but the redirection to thank you page from within it does not work...

Code:
<%
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
%>
<%
Function CheckCAPTCHA(valCAPTCHA)
	SessionCAPTCHA = Trim(Session("CAPTCHA"))
	Session("CAPTCHA") = vbNullString
	if Len(SessionCAPTCHA) < 1 then
        CheckCAPTCHA = False
        exit function
    end if
	if CStr(SessionCAPTCHA) = CStr(valCAPTCHA) then
	    CheckCAPTCHA = True
	else
	    CheckCAPTCHA = False
	end if
End Function
%>

<script language = "Javascript">

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Your email address does not seem to be valid! Please enter a valid email address.")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Your email address does not seem to be valid! Please enter a valid email address.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Your email address does not seem to be valid! Please enter a valid email address.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Your email address does not seem to be valid! Please enter a valid email address.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Your email address does not seem to be valid! Please enter a valid email address.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Your email address does not seem to be valid! Please enter a valid email address.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Your email address does not seem to be valid! Please enter a valid email address.")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.contactForm.Email
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
</script>


    <form name="contactForm" id="contactForm" method="POST" action="contact.asp" onSubmit="return ValidateForm()">
      Fields marked with an asterix (<span class="asterix">*</span>) are required!</p>
      <fieldset>
      <legend> Name </legend>
      <label class="first"> Title:
      <input name="Title" type="text" size="35">
      </label>
      <label> First Name:&nbsp;<span class="asterix">*</span>
      <input type="text" name="First" size="35">
      </label>
      <label> Last name:
      <input type="text" name="Last" size="35">
      </label>
      </fieldset>
      <fieldset>
      <legend> Address </legend>
      <label class="first"> Business:
      <input type="text" name="Business" size="35">
      </label>
      <label> Residential:
      <input type="text" name="Residential" size="35">
      </label>
      <label> PO Box:
      <input type="text" name="POBox" size="35">
      </label>
      </fieldset>
      <fieldset>
      <legend> Phone </legend>
      <label class="first"> Work:
      <input type="text" name="Work" size="35">
      </label>
      <label> Home:
      <input type="text" name="Home" size="35">
      </label>
      <label> Mobile:
      <input type="text" name="Mobile" size="35">
      </label>
      </fieldset>
      <fieldset>
      <legend> Message </legend>
      <label class="first"> Email:&nbsp;<span class="asterix">*</span>
      <input type="text" name="Email" size="35">
      </label>
      <label> Subject:&nbsp;<span class="asterix">*</span>
      <input type="text" name="Subject" size="35">
      </label>
      <label> Comment:&nbsp;<span class="asterix">*</span>
      <textarea name="Comment" cols="45" rows="10"></textarea>
      </label>
	  </fieldset>
      <fieldset>
	  <legend> Validate and submit your details </legend>
	  <label> &nbsp;
	  <input style="display: none" type="text" />
	  </label>
	  <label> &nbsp;
	  <img src="captcha.asp" alt="CAPTCHA Image" width="150" height="50" />
	  </label>
	  <label>
	  <%
if Request.ServerVariables("REQUEST_METHOD") = "POST" then
	strCAPTCHA = Trim(Request.Form("strCAPTCHA"))
	if CheckCAPTCHA(strCAPTCHA) = true then
		
			Server.Transfer ("contact.asp")
		%>

	   Type the characters shown in image for verification.
	  <%
	else
		%>
		It seems you have miss typed the characters shown in above image. Please retry...
				<%
	end if 
end if
%>
	  <input name="strCAPTCHA" type="text" id="strCAPTCHA" maxlength="8" />
	  </label>
	  <input class="submit" type="submit" value="Submit Details" />
   	  </fieldset>
    </form>

Code:
<%
Dim strName, strEmail, strSubject, strComment

strName = Trim(Request.Form("First"))
strEmail = Trim(Request.Form("Email"))
strSubject = Trim(Request.Form("Subject"))
strComment = Trim(Request.Form("Comment"))

Dim strMessage

strMessage=strMessage &"Title: " & Request.Form("Title") & VbCrLf
strMessage=strMessage &"First Name: " & Request.Form("First") & VbCrLf
strMessage=strMessage &"Last Name: " & Request.Form("Last") & VbCrLf
strMessage=strMessage &"Business Address: " & Request.Form("Business") & VbCrLf
strMessage=strMessage &"Residential Address: " & Request.Form("Residential") & VbCrLf
strMessage=strMessage &"PO Box: " & Request.Form("POBox") & VbCrLf
strMessage=strMessage &"Work Phone: " & Request.Form("Work") & VbCrLf
strMessage=strMessage &"Home Phone: " & Request.Form("Home") & VbCrLf
strMessage=strMessage &"Mobile Phone: " & Request.Form("Mobile") & VbCrLf
strMessage=strMessage &"Comment: " & Request.Form("Comment") & VbCrLf

IF strName <> "" AND strSubject <> "" AND strComment <> "" AND strEmail <> "" THEN
   
  Set objMail = CreateObject("CDO.Message")

  	objMail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL]      = 2
	objMail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL]     = "mail.blablabla"
	objMail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
	objMail.Configuration.Fields.Update
  
  objMail.From = strEmail
  objMail.To = "@blablabla"
  objMail.Subject = strSubject
  objMail.TextBody = strMessage
  objMail.Send

ELSE

Response.Redirect("error.htm")
   
  END IF
  
  Set objMail = Nothing

  Response.Redirect("thankyou.htm")
%>

I tried adding a response.end at very end, but same behaviour?

Also, if i don't enter required fields i get redirected to error page from within email script and when i go back and fill in rest of form and submit then the captcha script gives the "It seems you have miss typed the characters shown in above image. Please retry..." as it should, but this is annoying.

Cheers


-----
Shano Insano

Always ready to tell a fictitious story,
dispense bad advice or lend an inexpert hand...
 
Update!

The above does work!! :-D

Thankyou Tarwn for pointing me in the right direction...

The prob was, and I feel stoooopid :) was that for some reason after implementing server.transfer the smtp server i was using became very slow to connect? I used another external server and it all came good.

If u or anyone else might happen to know the reason or have experienced something like this i would appreciate hearing about it :)

Cheers

-----
Shano Insano

Always ready to tell a fictitious story,
dispense bad advice or lend an inexpert hand...
 
It sounds like the slowdown might be server specific if you started using another one and it was responding as quickly as usual. Glad you got it worked out though,

-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top