PDA

View Full Version : ASP FormMail script error (sorry not php!)


Snooze Control
01-23-2007, 02:32 PM
Greetings all once again,

I know the ASP is a dirty word on here judging by the "you should do it with php" replies to most past ASP queries I see. Sadly my host doesn't currently support php and I don't like CGI/Perl so I'm lumbered with ASP.

Anyway, hopefully there'll be some ASP wiz floating around the forums to help!

I've found the ASP version of Matt Wright's CGI FormMail from HERE (http://support.acmeinternet.com/powerfaqs/formmail.htm). I've done exactly as said and confirmed from my host that CDONTS are right for the mailComp but when I set it going I get the following asp error back :



Response object error 'ASP 0156 : 80004005'

Header Error

/scripts/formmail.asp, line 19

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.



I've done a search for the error msg on google and I see it's usually to do with the placement of the "Response.Buffer = true" bit, but mine is at the top of the script as suggested so I'm out of ideas here...

Here's the code for my htm page for your perusal:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<form action="scripts/formmail.asp" method="post">
<p>
<input name="_recipients" type="hidden" value="snoozecontrol@yahoo.co.uk" />
<input name="_requiredFields" type="hidden"
value="Name,Customer ID,Email,Comments" />
Name: <input name="Name" type="text" /><br />
Customer ID: <input name="Customer ID" type="text" /><br />
Email Address: <input name="Email" type="text" /><br />
Comments:<br />
<textarea name="Comments" rows=5 cols=50></textarea>
<input type="submit" value="Submit" />
<input type="reset" value="Clear" />
</p>
</form>

</head>

<body>
</body>
</html>


and now the asp page:

<%@ LANGUAGE="VBScript" %>

<% '************************************************* **************************
'* ASP FormMail *
'* *
'* Copyright 1999, 2000 by Mike Hall. *
'* Please see http://www.brainjar.com for documentation and terms of use. *
'************************************************* **************************

'- Customization of these values is required, see documentation. -----------

referers = Array("www.seattle-deliveries.com", "seattle-deliveries.com")
mailComp = "CDONTS"
smtpServer = "smtp.ntlworld.com"
fromAddr = "contact@seattle-derliveries.com"

'- End required customization section. -------------------------------------

Response.Buffer = true
errorMsgs = Array()

'Check for form data.

if Request.ServerVariables("Content_Length") = 0 then
call AddErrorMsg("No form data submitted.")
end if

'Check if referer is allowed.

validReferer = false
referer = GetHost(Request.ServerVariables("HTTP_REFERER"))
for each host in referers
if host = referer then
validReferer = true
end if
next
if not validReferer then
if referer = "" then
call AddErrorMsg("No referer.")
else
call AddErrorMsg("Invalid referer: '" & referer & "'.")
end if
end if

'Check for the recipients field.

if Request.Form("_recipients") = "" then
call AddErrorMsg("Missing email recipient.")
end if

'Check all recipient email addresses.

recipients = Split(Request.Form("_recipients"), ",")
for each name in recipients
name = Trim(name)
if not IsValidEmail(name) then
call AddErrorMsg("Invalid email address in recipient list: " & name & ".")
end if
next
recipients = Join(recipients, ",")

'Get replyTo email address from specified field if given and check it.

name = Trim(Request.Form("_replyToField"))
if name <> "" then
replyTo = Request.Form(name)
else
replyTo = Request.Form("_replyTo")
end if
if replyTo <> "" then
if not IsValidEmail(replyTo) then
call AddErrorMsg("Invalid email address in reply-to field: " & replyTo & ".")
end if
end if

'Get subject text.

subject = Request.Form("_subject")

'If required fields are specified, check for them.

if Request.Form("_requiredFields") <> "" then
required = Split(Request.Form("_requiredFields"), ",")
for each name in required
name = Trim(name)
if Left(name, 1) <> "_" and Request.Form(name) = "" then
call AddErrorMsg("Missing value for " & name)
end if
next
end if

'If a field order was given, use it. Otherwise use the order the fields were
'received in.

str = ""
if Request.Form("_fieldOrder") <> "" then
fieldOrder = Split(Request.Form("_fieldOrder"), ",")
for each name in fieldOrder
if str <> "" then
str = str & ","
end if
str = str & Trim(name)
next
fieldOrder = Split(str, ",")
else
fieldOrder = FormFieldList()
end if

'If there were no errors, build the email note and send it.

if UBound(errorMsgs) < 0 then

'Build table of form fields and values.

body = "<table border=0 cellpadding=2 cellspacing=0>" & vbCrLf
for each name in fieldOrder
body = body _
& "<tr valign=top>" _
& "<td><font face=""Arial,Helvetica"" size=2><b>" _
& name _
& ":&nbsp;</b></font></td>" _
& "<td><font face=""Arial,Helvetica"" size=2>" _
& Request.Form(name) _
& "</font></td>" _
& "</tr>" & vbCrLf
next
body = body & "</table>" & vbCrLf

'Add a table with any environmental variables.

if Request.Form("_envars") <> "" then
body = body _
& "<p>" _
& "<table border=0 cellpadding=2 cellspacing=0>" & vbCrLf
envars = Split(Request.Form("_envars"), ",")
for each name in envars
name = Trim(name)
body = body _
& "<tr valign=top>" _
& "<td><font face=""Arial,Helvetica"" size=2><b>" _
& name _
& ":&nbsp;</b></font></td>" _
& "<td><font face=""Arial,Helvetica"" size=2>" _
& Request.ServerVariables(name) _
& "</font></td>" _
& "</tr>" & vbCrLf
next
body = body & "</table>" & vbCrLf
end if

'Send it.

str = SendMail()
if str <> "" then
AddErrorMsg(str)
end if

'Redirect if a URL was given.

if Request.Form("_redirect") <> "" then
Response.Redirect(Request.Form("_redirect"))
end if

end if %>

<html>
<head>
<title>Form Mail</title>
</head>
<body bgcolor="#ffffff">
<font face="Arial,Helvetica" size=2>

<center>
<% if UBound(errorMsgs) >= 0 then %>
<table border=0><tr><td><font color="#cc0000" face="Arial,Helvetica" size=2><b>
Form could not be processed due to the following errors:
<p>
<ul>
<% for each msg in errorMsgs %>
<li><% = msg %>
<% next %>
</b></font></td></tr></table>
<% else %>
<table bgcolor="#000000" border=0 cellpadding=1 cellspacing=0 width=450><tr><td>
<table border=0 cellpadding=4 cellspacing=1 width="100%">
<tr bgcolor="#cccccc" valign=bottom>
<th colspan=2><font face="Arial,Helvetica" size=2>
Thank you, the following information has been sent:
</font></th>
</tr>
<% for each name in fieldOrder %>
<tr bgcolor="#ffffff" valign=top>
<td><font face="Arial,Helvetica" size=2><b><% = name %>&nbsp;</b></font></td>
<td><font face="Arial,Helvetica" size=2><% = Request.Form(name) %>&nbsp;</font></td>
</tr>
<% next %>
</table>
</td></tr></table>
<% end if %>
<p>
<a href="<% = Request.ServerVariables("HTTP_REFERER") %>">Return</a>
</center>

</font>
</body>
</html>

more...... >>>

Snooze Control
01-23-2007, 02:33 PM
<% '---------------------------------------------------------------------------
' Subroutines and functions.
'---------------------------------------------------------------------------

sub AddErrorMsg(msg)

dim n

'Add an error message to the list.

n = UBound(errorMsgs)
Redim Preserve errorMsgs(n + 1)
errorMsgs(n + 1) = msg

end sub

function GetHost(url)

Dim i, s

GetHost = ""

'Strip down to host or IP address and port number, if any.

if Left(url, 7) = "http://" then
s = Mid(url, 8)
elseif Left(url, 8) = "https://" then
s = Mid(url, 9)
end if
i = InStr(s, "/")
if i > 1 then
s = Mid(s, 1, i - 1)
end if

getHost = s

end function

function IsValidEmail(email)

dim names, name, i, c

'Check for valid syntax in an email address.

IsValidEmail = true
names = Split(email, "@")
if UBound(names) <> 1 then
IsValidEmail = false
exit function
end if
for each name in names
if Len(name) <= 0 then
IsValidEmail = false
exit function
end if
for i = 1 to Len(name)
c = Lcase(Mid(name, i, 1))
if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
IsValidEmail = false
exit function
end if
next
if Left(name, 1) = "." or Right(name, 1) = "." then
IsValidEmail = false
exit function
end if
next
if InStr(names(1), ".") <= 0 then
IsValidEmail = false
exit function
end if
i = Len(names(1)) - InStrRev(names(1), ".")
if i <> 2 and i <> 3 then
IsValidEmail = false
exit function
end if
if InStr(email, "..") > 0 then
IsValidEmail = false
end if

end function

function FormFieldList()

dim str, i, name

'Build an array of form field names ordered as they were received.

str = ""
for i = 1 to Request.Form.Count
for each name in Request.Form
if Left(name, 1) <> "_" and Request.Form(name) is Request.Form(i) then
if str <> "" then
str = str & ","
end if
str = str & name
exit for
end if
next
next
FormFieldList = Split(str, ",")

end function

function SendMail()

dim mailObj
dim addrList

'Send email based on mail component. Uses global variables for parameters
'because there are so many.

SendMail = ""

'Send email (CDONTS version), doesn't support reply to address and has
'no error checking.

if mailComp = "CDONTS" then
set mailObj = Server.CreateObject("CDONTS.NewMail")
mailObj.BodyFormat = 0
mailObj.MailFormat = 0
mailObj.From = fromAddr
mailObj.To = recipients
mailObj.Subject = subject
mailObj.Body = body
mailObj.Send
end if

'Send email (JMail version).

if mailComp = "JMail" then
set mailObj = Server.CreateObject("JMail.SMTPMail")
mailObj.Silent = true
mailObj.ServerAddress = smtpServer
mailObj.Sender = fromAddr
mailObj.ReplyTo = replyTo
mailObj.Subject = subject
addrList = Split(recipients, ",")
for each addr in addrList
mailObj.AddRecipient Trim(addr)
next
mailObj.ContentType = "text/html"
mailObj.Body = body
if not mailObj.Execute then
SendMail = "Email send failed: " & mailObj.ErrorMessage & "."
end if
end if

'Send email (ASPMail version).

if mailComp = "ASPMail" then
set mailObj = Server.CreateObject("SMTPsvg.Mailer")
mailObj.FromAddress = fromAddr
mailObj.RemoteHost = smtpServer
mailObj.ReplyTo = replyTo
for each addr in Split(recipients, ",")
mailObj.AddRecipient "", Trim(addr)
next
mailObj.Subject = subject
mailObj.ContentType = "text/html"
mailObj.BodyText = body
if not mailObj.SendMail then
SendMail = "Email send failed: " & mailObj.Response & "."
end if
end if

end function %>


Look forward to any suggestions/solutions!

Cheers,

SC