Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-08-2013, 07:18 PM   PM User | #1
SteveH
Regular Coder

 
Join Date: Nov 2005
Posts: 618
Thanks: 92
Thanked 1 Time in 1 Post
SteveH is an unknown quantity at this point
Problem sending email form

Hello

I am trying to test an email form on a Web page template here:

http://www.proofreading4students.com/contact_us1.asp

but the form is not being submitted and I can't see what the problem is, especially as in its rudiments it works on another site (same server). All the files have read/write permissions (screenshot). I am required to consider SMTP authentication and I have checked all those details (password, etc). The form has three fields: name, email, and message.

Here is the ASP code I have:

Code:
<%@ LANGUAGE="VBSCRIPT" %>
<% option explicit %>

<%
'Declaring Variables
Dim smtpserver,youremail,yourpassword,myCopy,ContactUs_Name,ContactUs_Email
Dim ContactUs_Body,Action,IsError
	
' Edit these 3 values
smtpserver = "smtp.wherever.com"
youremail = "info@wherever.com"
yourpassword = "myPWD%%"
myCopy = "myEmail@whatever.co.uk"


' Get variables from form
ContactUs_Name = Request("ContactUs_Name")
ContactUs_Email = Request("ContactUs_Email")
ContactUs_Body = Request("ContactUs_Body")
Action = Request("Action")


' Check email format
Function IsValidEmail(Email)
	Dim ValidFlag,BadFlag,atCount,atLoop,SpecialFlag,UserName,DomainName,atChr,tAry1
	ValidFlag = False
		If (Email <> "") And (InStr(1, Email, "@") > 0) And (InStr(1, Email, ".") > 0) Then
			atCount = 0
			SpecialFlag = False
			For atLoop = 1 To Len(Email)
			atChr = Mid(Email, atLoop, 1)
				If atChr = "@" Then atCount = atCount + 1
				If (atChr >= Chr(32)) And (atChr <= Chr(44)) Then SpecialFlag = True
				If (atChr = Chr(47)) Or (atChr = Chr(96)) Or (atChr >= Chr(123)) Then SpecialFlag = True
				If (atChr >= Chr(58)) And (atChr <= Chr(63)) Then SpecialFlag = True
				If (atChr >= Chr(91)) And (atChr <= Chr(94)) Then SpecialFlag = True
			Next
			If (atCount = 1) And (SpecialFlag = False) Then
				BadFlag = False
				tAry1 = Split(Email, "@")
				UserName = tAry1(0)
				DomainName = tAry1(1)
			If (UserName = "") Or (DomainName = "") Then BadFlag = True
			If Mid(DomainName, 1, 1) = "." then BadFlag = True
			If Mid(DomainName, Len(DomainName), 1) = "." then BadFlag = True
				ValidFlag = True
			End If
		End If
		If BadFlag = True Then ValidFlag = False
		IsValidEmail = ValidFlag
End Function
%>


<%
If Action = "SendEmail" Then
	
	' Validate the information entered
	If IsValidEmail(ContactUs_Email) = "False" Then
		IsError = "Yes"
		Response.Write("<font color=""white"">You did not enter a valid email address.<br>Please enter a valid email address<br></font><br>")
	End If
	
	If ContactUs_Name = "" Then
		IsError = "Yes"
		Response.Write("<font color=""white"">You did not enter a Name.<br>Please complete the Name field<br></font><br>")
	End If
	
	If ContactUs_Body = "" Then
		IsError = "Yes"
		Response.Write("<font color=""white"">You did not enter a Message.<br>Please complete the Message field<br></font><br>")
	End If

	End If
	
' If no input errors send the email

If Action = "SendEmail" And IsError <> "Yes" Then
	
	Dim strBody
	
	' Html body for the email
	strBody = strBody & "<font face=""Arial"">Contact bayingwolf form submitted at " & Now() &  vbCrLf & "<br><br>"
	strBody = strBody & "<b>From</b> http://" & Request.ServerVariables("HTTP_HOST") &  vbCrLf & "<br><br>"
	strBody = strBody & "<b>IP</b> " & Request.ServerVariables("REMOTE_ADDR") & vbCrLf & "<br><br>"
	strBody = strBody & "<b>Name</b>" & " : " & " " & Replace(ContactUs_Name,vbCr,"<br>") & "<br><br>"
	strBody = strBody & "<b>Email</b>" & " : " & " " & Replace(ContactUs_Email,vbCr,"<br>") & "<br><br>"
	strBody = strBody & "<br>" & Replace(ContactUs_Body,vbCr,"<br>") & "<br><br>"
	strBody = strBody & "</font>"
	
	Dim ObjSendMail
	Set ObjSendMail = CreateObject("CDO.Message") 
     
	'Configure SMTP
     
	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") = smtpserver
	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
     
	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") = youremail
	ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = yourpassword
     
	ObjSendMail.Configuration.Fields.Update
     
	'End remote SMTP server configuration section==
     
	ObjSendMail.To = youremail
        ObjSendMail.CC = ContactUs_Email
        ObjSendMail.BCC = myCopy
	ObjSendMail.From = ContactUs_Email
     
	' we are sending a html email.. simply switch the comments around to send a text email instead
	ObjSendMail.HTMLBody = strBody
	'ObjSendMail.TextBody = strBody
     
	ObjSendMail.Send
     
	Set ObjSendMail = Nothing 
	
' Change success messages below to say whatever or do a response.redirect or offer a hyperlink somewhere...
%>

<font size="2" color="#8AF3C8">Your message as seen below has been sent. Thank You.
<br><br>

<% =Replace(ContactUs_Body,vbCr,"<br>") %>

</font>
<% Else %>

<form action="contact_us1.asp" method="post">
<input type="hidden" name="Action" value="SendEmail">



<div class="form_settings">

<p><span>Name</span><input type="text" class="input" name="ContactUs_Name" value="<% =ContactUs_Name %>"></p>
            <p><span>Email Address</span><input type="text" class="input" name="ContactUs_Email" value="<% =ContactUs_Email %>"></p>
			<p><span>Message</span><textarea rows="8" class="input" name="ContactUs_Body" cols="50"><% =ContactUs_Body %></textarea></p>
           

<p style="padding: 10px 0 10px 0;">Please enter the answer to this simple maths question (to prevent spam)</p>
			<p><span>Maths Question: 9 + 3 = ?</span><input type="text" name="user_answer" class="contact" /><input type="hidden" name="answer" value="4d76fe9775" /></p>
            <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="Send" /></p>
          </div><!--close form_settings-->		
  
</form>

<% End If %>
I am just wondering if the maths equation at the bottom is preventing the email from being sent? All I get is a 500 - Internal server error.

Many thanks.

Steve
SteveH is offline   Reply With Quote
Old 03-08-2013, 09:12 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
DEBUG DEBUG DEBUG

Since the page comes up correctly, before the form submittal, there are no syntax errors.

So the problem is a runtime error.

So start adding in debug.

The easiest way to do this:
Code:
<%
If Action = "SendEmail" Then
    ON ERROR RESUME NEXT
    ...
And then, after every operation that *MIGHT* cause an error, add a debug line. Just one example:
Code:
' If no input errors send the email
If Err.Number > 0 Then Response.Write "after error check, " & Err.Description : Response.End
...
You can just put in a few such lines to start with. If that doesn't find it, then add more, until you narrow it down.

I would personally expect the error to be in the actual sending code, but that email validation check looks ugly enough that who knows, the problem might be right there.
__________________
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.
Old Pedant is online now   Reply With Quote
Old 03-08-2013, 09:20 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Here. Kill that ugly email test and use this:
Code:
Function IsValidEmail(Email)
   Dim emRE = New RegExp
   emRE.Pattern = "^\w[\w\-\'\.]*[\w\-\']\@(\w[\w\-\']+\.)+[a-z]{2,12}$"
   emRE.IgnoreCase
   isValidEmail = emRE.Test( Trim(Email) )
End Function
It's not perfect, but it's better than what you have.

If it's too restrictive, we can loosen it up.

And for heavens sake, stop doing this:
Code:
	If IsValidEmail(ContactUs_Email) = "False" Then
The function returns a *BOOLEAN* value, True or False. It does *NOT* return a *STRING* "True" or "False".

Just do this:
Code:
	If Not IsValidEmail(ContactUs_Email) Then
And while you are at it, stop doing this:
Code:
		IsError = "Yes"
Learn to use boolean values, thus:
Code:
		IsError = True
__________________
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
SteveH (03-08-2013)
Old 03-08-2013, 10:42 PM   PM User | #4
SteveH
Regular Coder

 
Join Date: Nov 2005
Posts: 618
Thanks: 92
Thanked 1 Time in 1 Post
SteveH is an unknown quantity at this point
Hello OP

Many thanks for your valuable suggestions.

I have resolved the original problem - there was a reference in the code to an email subject line which I hadn't deleted.

I have incorporated
Code:
If Not IsValidEmail(ContactUs_Email) Then
and
Code:
IsError = "Yes"
and I can now send the form off OK. I get a copy as does the Webmaster.

What the server does not like - I get a 500 error - is the email test, so I have gone back and put my old one in and that 500 error does not now appear.

I have deliberately sent myself an erroneous message (leaving out the @ in the email form field) and that takes me to the 500 error again. What that means is that this:

Code:
If Action = "SendEmail" Then

	' Check information entered

	If Not IsValidEmail(ContactUs_Email) Then
		IsError = True
		Response.Write("<font color=""white"">You did not enter a valid email address.<br>Please enter a valid email address<br></font><br>")
	End If
	
	If ContactUs_Name = "" Then
		IsError = True
		Response.Write("<font color=""white"">You did not enter a Name.<br>Please complete the Name field<br></font><br>")
	End If
	
	If ContactUs_Body = "" Then
		IsError = True
		Response.Write("<font color=""white"">You did not enter a Message.<br>Please complete the Message field<br></font><br>")
	End If

End If
is not working.

Ideally, these messages 'you did not enter.....etc' ought to appear above their respective form fields, rather than send me to a 500 error.

I am not sure if I have been too clear - I hope so.

Thank you.
SteveH is offline   Reply With Quote
Old 03-08-2013, 11:19 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What can I say? Put the debugging back in and find out where the real problem is.

Put in *MORE* debugging.

After every statement in that problem block, if needed.

Ehhh...never mind. I just put in a test, first with a bogus email address and then with a good one. Only the bogus one cause the 500 error.

So clearly the bug is in the email test.

Let me fix mine. It's probably just a typo.
__________________
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.
Old Pedant is online now   Reply With Quote
Old 03-08-2013, 11:26 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Sorry...I must have been tired. A couple of obvious bonehead mistakes.

This works, now. Tested:
Code:
Function IsValidEmail(Email)
   Dim emRE 
   Set emRE = New RegExp
   emRE.Pattern = "^\w[\w\-\'\.]*[\w\-\']\@(\w[\w\-\']+\.)+[a-z]{2,12}$"
   emRE.IgnoreCase = True
   isValidEmail = emRE.Test( Trim(Email) )
End Function
__________________
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
SteveH (03-08-2013)
Old 03-08-2013, 11:43 PM   PM User | #7
SteveH
Regular Coder

 
Join Date: Nov 2005
Posts: 618
Thanks: 92
Thanked 1 Time in 1 Post
SteveH is an unknown quantity at this point
Hello OP

Many thanks!

Yes, that is fine. If I miss the @ in an email address, I get the 500 error, so I will debug as to why I do not get the 'You did not enter a valid.....' messages, as you propose.

At least the 'contact' page loads now and your email test is a lot, well, neater to say the least!

Thank you for all your help.

Steve
SteveH is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:45 AM.


Advertisement
Log in to turn off these ads.