PaulSadowski.com 

PaulSadowski.com
Email Form Processor
Download the code

If you intend to use this code please use the download code links to get the code. This page
is presented to illustrate the code and copying and pasting directly from the webpage could
result in errors from formatting or other factors.

DOCUMENTATION & USAGE

Please read this section carefully. It is the sole documentation for this Active Server Page (ASP) script.

This script requires you set certain information in the configuration section. This is in order to provide
basic security to prevent a spammer or others from using your site to spam or for other nefarious
activities.

In the configuration section you must set one or both of these two items

  AllowedHosts
  AllowedReferrers

AllowedHosts should contain a comma separated list of all the domain names that are allowed
to use this script. The list should contain nothing but domain names, no http:// or other
path information. For example, www.mydomain.com,  mydomain.com, myhost.mydomain.com

AllowedReferrers should contain a comma separated list of the full URLs of the web pages
that are allowed to use this script. For example, http://www.mydomain.com/feedback.htm,
http://mydomain.com/feedback.htm, http://myhost.mydomain.com/feedback/psmail.htm

Note that in both cases you must include the domain names of all possible domains that
your site is known by. So if your site is reachable by URLs with and without the www. part
be certain to include both. If you are using a URL referring service that uses a domain name to
forward browsers to your site, include the URL of the actual name as well as the forwarded
domain to make sure it works correctly.

Important! AllowedHosts supersedes AllowedReferrers! If you put myhost.com in AllowedHosts
then any page on myhost.com can use the script. If other users share your domain leave AllowedHosts
empty and fill-in only AllowedReferres with the full URL of your mail page.

Also in the configuration section is the FromAddress variable. This variable holds a single
email address that will be used only when the mail form does not include a field indicating
a specific email address to use as the send, or From, field of the email.

Do not remove the double quotation marks that enclose each of these configuration field options.

USAGE

To use the script on your site, first configure it per the directions above and then place it in a
directory that either you or your host has set to allow for execution of scripts.

Create an html page that includes a form whose data you wish to email. Set the form's
Action to the full URL of the script, for example, http://www.mydomain.com/scripts/psmail.asp
Set the form's method to POST.

The script uses the following predefined fields from your form

psmto
psmfrom
psmsubject
psmredirect

psmto is the email address of the recipient. (normally a hidden field) REQUIRED
psmfrom is the email address of the sender REQUIRED
psmsubject is the text to be used as the subject of the email (normally a hidden field)
psmredirect is an optional field that can contain a URL of a web page that the script
will direct the user's browser to after the email is sent. (a hidden field)

The field names are not case-sensitive.

Any other form fields will be included automatically into the email as sets of name value pairs
(see the example mail).

If you do not include psmsubject then the script will use "Website feedback from <REFERRING URL>"
as the subject line.

If you do not include a redirect URL, psmredirect, then the script will display a simple
"Message sent" page.


Sample email

This is what the message body of a message might look like.

  to: steve@my.com
  from: me@my.com
  subject: testing

  Body: This is some test email
  sent using psmail.asp from
  http://www.paulsadowski.com/WSH/psmail.htm

  B1: Submit


  date: 6/5/2004 4:42:51 PM
  remote host: 127.0.0.1
  source URL: http://www.mydomain.com/scripts/psmail.htm
  hostname: www.mydomain.com


Note the date and time stamp represents the time on the server which may be different from your own.


<%
'PSMAIL.asp
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' COPYRIGHT, WAIVER OF LIABILITY, TERMS OF USE AND REDISTRIBUTION
'
'THE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS," AND "WITH ALL FAULTS".
'YOU UNDERSTAND AND EXPRESSLY AGREE THAT USE OF THE SOFTWARE/CODE ARE AT
'YOURSOLE RISK, THAT ANY MATERIAL AND/OR DATA DOWNLOADED OR OTHERWISE
'OBTAINED IS AT YOUR OWN DISCRETION AND RISK AND THAT YOU WILL BE SOLELY
'RESPONSIBLE FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT
'RESULTS FROM THE USE OF SUCH MATERIAL AND/OR DATA.
'
'IN NO EVENT WILL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
'SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES OF ANY KIND RESULTING HOWEVER
'THEY MAY ARISE AND EVEN IF THE AUTHOR HAS BEEN PREVIOUSLY ADVISED OF THE
'POSSIBILITY OF SUCH DAMAGES.
'
'YOU MAY NOT COPY, MODIFY, REPUBLISH, REPRODUCE, TRANSMIT, OR DISTRIBUTE
'IN WHOLE OR IN PART THIS APPLICATION/CODE. THIS COPYRIGHT DOES NOT
'SUPERSEDE ANY COPYRIGHTS THAT MAY EXIST IN THIRD-PARTY MATERIALS.
'
'YOU MAY NOT SELL, BARTER OR TRADE THIS SOFTWARE/CODE ITSELF OR AS PART OF
'ANOTHER APPLICATION, PACKAGE OR COLLECTION.
'
'THE AUTHOR MAY OR MAY NOT PROVIDE SUPPORT FOR THIS APPLICATION/CODE AT HIS
'SOLE DISCRETION. AT NO TIME WILL FREE SUPPORT BE PROVIDED TO OR ON BEHALF OF
'ISPS, DEVELOPERS, DESIGNERS, THIRD-PARTIES OR ANY OTHER COMMERCIAL ENTITIES.
'
'THIS NOTICE AND COPYRIGHT MAY NOT BE REMOVED FROM ANY COPY OR COPIES OF
'THIS APPLICATION/CODE.
'
' COPYRIGHT (C)(P) 2004 BY Paul R. Sadowski <scripting@paulsadowski.com>.
' ALL RIGHTS RESERVED.


' -- CONFIGURATION SECTION --
'
' THIS IS THE DEFAULT RETURN FROM ADDRESS USED ONLY WHEN THE
' FORM DOES NOT SPECIFY ANOTHER FROM ADDRESS IN THE FORM'S 'FROM' FIELD.

FromAddress = ""

' ALLOWEDHOSTS IS A COMMA SEPARATED LIST OF DOMAIN NAMES THAT ARE ALLOWED
' TO USE THIS FORM PROCESSOR. FOR EXAMPLE:
' www.mydomain.com, mydomain.com, www.mydomain.net, mydomain.net

AllowedHosts = ""

' ALLOWEDREFERRERS IS A COMMA SEPARATED LIST OF URLS WHO ARE
' ALLOWED TO USE THIS FORM. THIS SHOULD BE THE COMPLETE URL LIKE
' http://myhost.com/support/feedback.htm, http://www.myhost.com/support/feedback.htm
' IT SHOULD INCLUDE ALL DOMAIN NAMES YOUR PAGE CAN BE ADDRESSED BY.

AllowedReferrers = ""

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' MAKE NO CHANGES BELOW THIS LINE

Allow = False
AllowedHosts = Replace(AllowedHosts, " ", "")
AllowedReferrers = Replace(AllowedReferrers, " ", "")
AllowedHosts = Replace(AllowedHosts, Chr(9), "")
AllowedReferrers = Replace(AllowedReferrers, Chr(9), "")

Hosts = Split(AllowedHosts, ",")
Referrers = Split(AllowedReferrers, ",")

strTo = Trim(CStr(Request.Form("psmto")))
strFrom = Trim(CStr(Request.Form("psmfrom")))
strSubject = Trim(CStr(Request.Form("psmsubject")))
strRedirect = Trim(CStr(Request.Form("psmredirect")))

if strSubject = "" then
 strSubject = "Website feedback from " & Request.ServerVariables ("HTTP_REFERER")
end if

if strFrom = "" then
 strFrom = FromAddress
end if

strTo = Replace(strTo, ";", ",")
Rcpt = Split(strTo, ",")

RebuildToString(Rcpt)

Allow = Allowed(Hosts, BaseHost(Request.ServerVariables ("HTTP_REFERER")))
if Allow <> True then
 Allow = Allowed(Referrers, Request.ServerVariables ("HTTP_REFERER"))
end if

if strTo = "" or strFrom = "" then
 Response.Write("<br><b>Invalid or missing to or from field(s)</b><br>")
elseif AllowedHosts = "" and AllowedReferrers = "" then
 Response.Write("<br><b>Configuration error: AllowedHosts or AllowedReferrers must not be empty</b><br>")
elseif Allow = False then
 Response.Write("<br><b>You are not authorized to use this script. [referrers]</b><br>" & _
  Request.ServerVariables ("HTTP_REFERER"))
elseif strFrom <> "" and IsEmail(strFrom) = false then
 Response.Write("<br><b>Invalid sender.</b><br>")
else
 strBody = strBody & "to: " & strTo & vbCRLF
 strBody = strBody & "from: " & strFrom & vbCRLF
 strBody = strBody & "subject: " & strSubject & vbCRLF & vbCRLF
 for each name in Request.Form
  if LCase(name) <> "psmto" and LCase(name) <> "psmfrom" and LCase(name) <> _
   "psmsubject" and LCase(name) <> "psmredirect" then
   strBody = strBody & name & ": " & CStr(Request.Form(name)) & vbCRLF & vbCRLF
  end if
 next

 strBody = strBody & vbCRLF & vbCRLF
 strBody = strBody & "date: " & Now & vbCRLF
 strBody = strBody & "remote host: " & Request.ServerVariables ("REMOTE_ADDR") & vbCRLF
 strBody = strBody & "source URL: " & Request.ServerVariables ("HTTP_REFERER") & vbCRLF
 strBody = strBody & "hostname: " & Request.ServerVariables ("HTTP_HOST") & vbCRLF

 Set objMessage = Server.CreateObject("CDO.Message")
 objMessage.Subject = strSubject
 objMessage.Sender = strFrom
 objMessage.To = strTo
 objMessage.TextBody = strBody
 objMessage.Send
 Set objMessage = Nothing

 if strRedirect <> "" then
  response.redirect(CStr(strRedirect))
 else
  Response.Write("<br><b>Message sent.</b><br>")
 end if
end if

Function BaseHost(host)
Dim x, tempstring
If InStr(host, "://") <> 0 Then
 host = Mid(host, InStr(host, "://")+3)
End If
host = strreverse(host)
tmpstring = host
x = Len(host)
for y = x to 1 step -1
 if mid(host, y, 1) = "/" Then
  tmpstring = mid(host, y+1)
  exit for
 end if
next
BaseHost = strreverse(tmpstring)
End Function

Function ValidateDomain(strRef)
dim x
ValidateDomain = false
for each x in Hosts
 if x <> "" and Instr(1, strRef, x, 1) > 0 then
  ValidateDomain = true
  exit for
 end if
next
End Function

Function IsEmail(sEmail)
Dim regEx, retVal
IsEmail = false
Set regEx = New RegExp

regEx.Pattern ="^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
regEx.IgnoreCase = true
retVal = regEx.Test(sEmail)
If not retVal Then
 exit function
End If
IsEmail = true
End Function

Function Allowed(ThisArray(), ThisString)
Allowed = False
for each x in ThisArray
 if LCase(ThisString)= LCase(x)then
  Allowed = True
  exit for
 end if
next
End Function

Sub RebuildToString(Rcpt())
Dim x
strTo = ""
for each x in Rcpt
 if IsEmail(trim(x)) = True then
  strTo = strTo & trim(x) & ", "
 end if
next
x = Len(strTo)
if x > 0 then
 strTo = Trim(strTo)
 strTo = ChopChar(strTo, ",")
end if
End Sub

Function ChopChar(strString, strChar)
Dim x
x = Len(strString)
if mid(strString, x) = strChar then
 ChopChar = mid(strString, 1, x -1)
else
 ChopChar = strString
end if
End Function

' COPYRIGHT (C)(P) 2004 BY Paul R. Sadowski <scripting@paulsadowski.com>.
' ALL RIGHTS RESERVED.
%>
 

Businesses, organizations and commercial users are asked to make a $20
contribution for the use of this code. Personal use users while not required
are also asked to contribute.

Click here to download this code!

© 2003 by Paul R. Sadowski 
All Rights Reserved. Used By Permission.
Comments to: Scripting