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!

help with a cgi script for a form

Status
Not open for further replies.

cdogstu99

MIS
Jan 17, 2005
68
US
i have a cgi file that runs a form on my webpage to email me info from the form. Problem is that something is wrong with the setup. I have following fields on form on my webpage: name, phone, email, address, city, state, zip, invest_amt, how_soon, how_found_site, and EmailBody. The form works well, and it emails me the info, but it is not in the right order and i really can't tell from the code how to fix it. I also wanted to put text to let me know what was what when i got the email. Can anyone help? Here's code:

<%@ Language=VBScript %>
<%
' configuration:
mailserver = "mail.mywebsite.com" ' Type in the name of the SMTP mail server of
' your server here

recipient = "info@mywebsite.com" ' Type your email address here

redirect = " ' Type in the name of the page to appear after
' the email has been sent
' build particulars
method = Request.ServerVariables("HTTP_METHOD")
Subject = Request("Subject")
fromemail = Request("email")
fromname = Request("username")
excludeflds = split(Request("exclude"),",")
' set defaults if not specified
if subject = "" then
Subject = "Email from my WebSite" ' If the user leaves the subject blank then
end if ' you can specify a default subject
if fromemail = "" then
fromemail = "anonymous@yahoo.com" ' If the user leaves the email address field
end if 'blank then you can specify a default address
if fromname = "" then
fromname = "Anonymous" ' If the user leaves the name field blank then
end if ' you can specify a default name
' determine the method of the form (post or get)
select case lcase(method)
case "post"
'build a delimited list of the field names
for each fld in Request.Form
' remove the fieldnames of the email configuration fields
select case lcase(fld)
case "exclude","email","subject","recipient","redirect","mailserver","username"
' do nothing
case else
tmpfldnames = tmpfldnames & fld & ","
end select
next
case "get"
'build a delimited list of the field names
for each fld in Request.QueryString
' remove the fieldnames of the email configuration fields
select case fld
case "exclude","email","subject","recipient","username"
' do nothing
case else
tmpfldnames = tmpfldnames & fld & ","
end select
next
end select
' remove the trailing comma
if mid(tmpfldnames,len(tmpfldnames),1) = "," then
tmpfldnames = mid(tmpfldnames,1,len(tmpfldnames)-1)
end if
' build an array of the fieldnames
fldarray = split(tmpfldnames,",")
' build the message header
bodytext = "<Date sent> " & now() ' Specify a header for the email body
bodytext = bodytext & invest_amt & how_soon & how_found_site & vbcrlf
' cycle through all of the fields
for each fld in fldarray
PrintFld = True
' exclude fields in the exclude list
for each checkfld in excludeflds
if trim(lcase(checkfld)) = trim(lcase(fld)) then
PrintFld = False
end if
next
' continue building the mail message with the form results
if PrintFld = True then
bodytext = bodytext & "<" & fld & "> " & request(fld) & vbcrlf ' Formatting for individual fields being inserted
end if
next
sentbin = sendmail(mailserver,fromname,fromemail,recipient,subject,bodytext)
select case sentbin
case true ' If no problems then the redirect page is loaded
Response.Redirect(redirect)
case false ' Otherwise an error is raised and the message is displayed
Response.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head><title>Error sending email...</title></head><body>")
Response.write("<!--#include file='errorhdr.html'-->") ' Use this line to display a header
Response.write("<font face='arial' size='2'>There was a problem sending the email... <br><br>Please hit the back button in your browser and try again.</font>")
Response.write("<!--#include file='errorftr.html'-->") ' Use this line to display a footer
Response.write("</body></html>")
end select
' This function actually sends the mail using ASPMail and returns true if the mail was sent
' - returns false if there was an error
Function sendmail(strservername,strfromname,strfromaddress,strRecip,strSub,strBodyText)
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
'Use one of the following objects depending on which one is installed on the server.
'If you use the second one then you must change the "Mailer" object to "NewMail"
'Do not contact me with problems that refer to line 89 or the line that creates the
'mail, because I can not install server objects and software for you - you must do that!
'Otherwise, feel free to contact me.
'Set Mailer = Server.CreateObject("CDONTS.NewMail")
Mailer.FromName= strfromname
Mailer.FromAddress= strfromaddress
Mailer.RemoteHost = strservername
Mailer.AddRecipient strrecip, strrecip
Mailer.Subject= strSub
Mailer.BodyText= strBodyText
if Mailer.SendMail then
sendmail = true
else
sendmail = false
end if
end function
%>
 
The code uses a string variable named bodytext for the body of your email. It looks like the fields are appended to bodytext inside a For loop that runs through an array of all the fields submitted from your html form.

If you will always be submitting the same html form fields then you don't necessarily need something thing flexible. In exchange for giving up the flexibility to submit anything, you could change the code to expect only specific html form fields formatted in the way you like...
..or you could do a hybrid where you expect certain ones and the rest are done in a For loop... that is actually what is going on in your existing code except that most of the fields that it always expects seem to have more to do with the "how" than the "what" of your email. For example the line:
case "exclude","email","subject","recipient","redirect","mailserver","username"
 
hi sheco, ..yes it will always be the same html form.
the thing is that the email's i'm recieving have the information all out of order and even at the end there's a submit button with some of the information imbedded in it. It is readable, but very confusing. Any suggestions if i should wipe out some of the code and replace with something else?
 
In that case you can just change the way your code builds its bodytext variable so that, rather than using the loop, it instead pulls the same field values every time and puts them in a specific order of your choosing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top