Click here to Skip to main content
15,917,875 members
Articles / Web Development / HTML
Article

A versatile HTML form mail script for classic ASP

Rate me:
Please Sign up or sign in to vote.
4.68/5 (26 votes)
15 Mar 20063 min read 498.6K   9.5K   60   117
This script allows you to email the contents of an HTML form to one or more specified addresses. It supports HTML emails, plain text emails, and preformatted email templates.

Introduction

I submitted this article because I found it to be of great use and it does a good job of supporting all my form emailing needs. If you find it useful, then great! :)

Usage

It's quite simple to use, all you need to do is the following:

  1. The sendmail.asp file.
  2. An HTML page with a form that needs to be emailed (making sure certain required hidden fields are included).
  3. A thanks page to redirect to when the mail send is complete.
  4. An optional email template to define how the form contents should be formatted in the email that is sent.

The script supports plain text and HTML email formats. If you want to send an HTML email, you must create an email template first. You can use a plain text email template instead if you like, and if you prefer not to use HTML but still want to make sure the email is formatted nicely.

In your email template, you should put placeholders for each form element submitted. The placeholders should be formatted like this: [$form-field-name$]. For example, if you have a text field called "surname", your placeholder in the template should read: [$surname$]. The zip file for this article contains a sample email template for you to look at.

A set of hidden fields is required in your form so that the sendmail script knows what to do with the contents of the form, and where and how to send it. If applicable, you can replace some of the hidden fields with real fields to allow users to customize, for example, whom to send the mail to, or what the subject line in the email should be. The fields that the sendmail script understands are:

  • redirect - required

    The URL to redirect to when the mail has been sent.

  • mailto - required

    The email address of the recipient (separate multiple recipients with commas).

  • cc

    The email address of the cc recipient (separate multiple recipients with commas).

  • bcc

    The email address of the bcc recipient (separate multiple recipients with commas).

  • mailfrom - required

    The email address of the sender.

  • subject - Required, if no email template is specified.

    The subject line of the email.

  • message

    An optional message to include before the contents of the form in the email. Only used if no email template is specified.

  • template

    A relative path to a plain text or HTML file to use as an email template.

  • html

    Must be "yes" or "no". If yes, the email will be sent as an HTML email, otherwise, it'll be sent as a plain-text email.

  • testmode

    Must be "yes" or "no". If yes, the email will not be sent. Instead, the email will be written to the screen, with no redirection to the thanks page specified by the "redirect" field. This should be used to test the form and the email templates to make sure you are happy with the results.

Make sure that the form points to the sendmail.asp file, and that the method is set to "post". e.g. <form action="sendmail.asp" method="post">

The included zip file contains a sample form with validation, a sample email template, thanks page and of course, the script.

History

  • 18th November, 2003
    • Article posted.
  • 15th March, 2006
    • A CDO version of the script has now been included that should work on most new servers where the old one was failing. It also has a feature to hide email addresses from the hidden fields if desired. Just open the ASP file for a detailed explanation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
Web application developer, graphic designer, aspiring entrepreneur, snowboarder and electronic music afficianado.


Comments and Discussions

 
AnswerRe: not getting the result I should from this Pin
Nathan Ridley8-Sep-06 20:15
Nathan Ridley8-Sep-06 20:15 
GeneralRe: not getting the result I should from this Pin
aaaronnn20-Sep-06 7:31
aaaronnn20-Sep-06 7:31 
GeneralRe: not getting the result I should from this Pin
rdriskill12-Nov-07 8:57
rdriskill12-Nov-07 8:57 
Generalproblem sorted - thanks anyway Pin
steve-site10028-Jul-06 14:13
steve-site10028-Jul-06 14:13 
Generalalmost got it working - help please Pin
steve-site10025-Jul-06 13:42
steve-site10025-Jul-06 13:42 
Generalwhere to insert the smtp-server.... Pin
anderslundsgard17-Jul-06 0:24
anderslundsgard17-Jul-06 0:24 
GeneralRe: where to insert the smtp-server.... Pin
id10t22-Jan-07 7:53
id10t22-Jan-07 7:53 
Generalsendmail asp Pin
bigjohn324-Jun-06 7:39
bigjohn324-Jun-06 7:39 
Installed this script on a GODADDY server with the minimal ASP support. It doesn;t do anything.
Is this likely some GODADDY restriction?

my (Your) script >>
<%
option explicit

'---------------------------------------------------------------------------------------------------
'FORM MAIL SCRIPT
'----------------
'usage:
'
'
'hidden fields:
' redirect - the url to redirect to when the mail has been sent (REQUIRED)
' mailto - the email address of the recipient (separate multiple recipients with commas) (REQUIRED)
' cc - the email address of the cc recipient (separate multiple recipients with commas) (OPTIONAL)
' bcc - the email address of the bcc recipient (separate multiple recipients with commas) (OPTIONAL)
' mailfrom - the email address of the sender (REQUIRED)
' subject - the subject line of the email (REQUIRED)
' message - the message to include in the email above the field values. not used when a template is being used. (OPTIONAL)
' template - specifies a text or html file to use as the email template, relative to the location of the sendmail script. (e.g. ../email.txt)
' A template should reference form fields like this: [$Field Name$]
' html - if this has the value "yes", the email will be sent as an html email. only used if a template is supplied.
' testmode - if this is set to "yes", the email contents will be written to the screen instead of being emailed.
'---------------------------------------------------------------------------------------------------
dim pde : set pde = createobject("scripting.dictionary")
'---------------------------------------------------------------------------------------------------
'PREDEFINED ADDRESSES for the "mailto" hidden field
'if you don't want to reveal email addresses in hidden fields, use a token word instead and specify
'below which email address it applies to. e.g.
'ALSO, in the same way, you can use %mailfrom% to hide the originating email address
pde.add "%contactform%", "myemail@someaddress.com"
pde.add "%salesenquiry%", "anotheremail@someaddress.com"
'---------------------------------------------------------------------------------------------------

function getTextFromFile(path)
dim fso, f, txt
set fso = createobject("Scripting.FileSystemObject")
if not fso.fileexists(path) then
getTextFromFile = ""
exit function
end if
set f = fso.opentextfile(path,1)
if f.atendofstream then txt = "" else txt = f.readall
f.close
set f = nothing
set fso = nothing
getTextFromFile = txt
end function

dim redir, mailto, mailfrom, subject, item, body, cc, bcc, message, html, template, usetemplate, testmode
redir = request.form("http://www.jmm3.biz/default.html")
mailto = request.form("jmmagruder3@sbcglobal.net")
if pde.exists(mailto) then mailto = pde(mailto)
cc = request.form("cc")
bcc = request.form("bcc")
mailfrom = request.form("email")
if mailfrom = "" then mailfrom = pde("%mailfrom%")
subject = request.form("HELPME")
message = request.form("message")
dayphone = request.form("day")
nightphone = request.form("night")
template = request.form("template")
testmode = lcase(request.form("testmode"))="no"

if len(template) > 0 then template = getTextFromFile(server.mappath(template))
if len(template) > 0 then usetemplate = true else usetemplate = false
dim msg : set msg = server.createobject("CDO.Message")
msg.subject = subject
msg.to = mailto
msg.from = mailfrom
if len(cc) > 0 then msg.cc = cc
if len(bcc) > 0 then msg.bcc = bcc

if not usetemplate then
body = body & message & vbcrlf & vbcrlf
else
body = template
end if
for each item in request.form
select case item
case "redirect", "mailto", "cc", "bcc", "subject", "message", "template", "html", "testmode","buyer","seller","fname","lname","day","night","comments"
case else
if not usetemplate then
if item <> "mailfrom" then body = body & item & ": " & request.form(item) & vbcrlf & vbcrlf
else
body = replace(body, "[$" & item & "$]", replace(request.form(item),vbcrlf,"
"))
end if
end select
next

if usetemplate then 'remove any leftover placeholders
dim rx : set rx = new regexp
rx.pattern = "\[\$.*\$\]"
rx.global = true
body = rx.replace(body, "")
end if

if usetemplate and lcase(request.form("html")) = "yes" then
msg.htmlbody = body
else
msg.textbody = body
end if
if testmode then
if lcase(request.form("html")) = "yes" then
response.write "
" & vbcrlf
		response.write "Mail to: " & mailto & vbcrlf
		response.write "Mail from: " & mailfrom & vbcrlf
		if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
		if len(dayphone) > 0 then response.write "Dayphone: " & day & vbcrlf
		if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
		response.write "Subject: " & subject & vbcrlf & string(80,"-") & "
"
response.write body
else
response.write "<title>Sendmail.asp Test Mode
" & vbcrlf
		response.write "Mail to: " & mailto & vbcrlf
		response.write "Mail from: " & mailfrom & vbcrlf
		if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
		if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
		response.write "Subject: " & subject & vbcrlf & vbcrlf
		response.write string(80,"-") & vbcrlf & vbcrlf & "<span style=""color:blue;"">"
		response.write body & "</span>" & vbcrlf & vbcrlf
		response.write string(80,"-") & vbcrlf & "**END OF EMAIL**
"
end if
else
msg.send
response.redirect redir
end if
set msg = nothing
%>

My html form >







<title>Magruder Web Feedback













   

             <span
style='font-size:20.0pt'> Your Information Will Be Kept  CONFIDENTIAL
<span
style='font-size:24.0pt'>
<span
style='mso-tab-count:1'>


         
<span
style='font-size:13.5pt'> Please select INTEREST category  
           
<span
style='font-size:18.0pt'> 
BUYER       
SELLER     



            
<span
style='font-size:13.5pt'>Please provide your email      
  
like  abc @hotmail.com



               
  
Please provide your
name    First  
 
Last 

      

               
   Please provide 1 or more
phones 
  Day<span
style='font-size:13.5pt'>        
Night 

         

               
   Please ask a question of state needs
below – PLEASE be specific !



               




      
        
<span
style='mso-tab-count:2'>             I will reply as soon as possible. <span
style='mso-spacerun:yes'> 



<o:p> 



                                                                                                            <a
href="http: www.jmm3.biz="" default.html"="">RETURN

      











Thanks
John


John
QuestionHow to attach a file to email via sendmail Pin
kenfresher1-Jun-06 10:11
kenfresher1-Jun-06 10:11 
QuestionForm results display Pin
richard derrick5-May-06 2:33
richard derrick5-May-06 2:33 
Generalworks in testmode, doesn't outside of testmode Pin
hypnokizer28-Apr-06 11:29
hypnokizer28-Apr-06 11:29 
Questionsending via other server Pin
johnrobert25-Apr-06 5:25
johnrobert25-Apr-06 5:25 
General405 error with post Pin
morgan8522-Apr-06 16:12
morgan8522-Apr-06 16:12 
GeneralRe: 405 error with post Pin
Nathan Ridley22-Apr-06 20:27
Nathan Ridley22-Apr-06 20:27 
GeneralHelp with remote SMTP server Pin
theace2320-Apr-06 6:32
theace2320-Apr-06 6:32 
QuestionOption for multiple mailto:? Pin
rmfire19-Apr-06 12:29
rmfire19-Apr-06 12:29 
GeneralAlternative Script Pin
Bengal Tiger14-Apr-06 5:46
Bengal Tiger14-Apr-06 5:46 
Questionasp error Pin
dannif6-Apr-06 23:51
dannif6-Apr-06 23:51 
AnswerRe: asp error Pin
Nathan Ridley7-Apr-06 0:15
Nathan Ridley7-Apr-06 0:15 
GeneralYou saved me!!! Pin
patriciamcd29-Mar-06 21:58
patriciamcd29-Mar-06 21:58 
GeneralRe: You saved me!!! Pin
Nathan Ridley30-Mar-06 13:19
Nathan Ridley30-Mar-06 13:19 
GeneralWonderful!!! Pin
btomich24-Mar-06 4:40
btomich24-Mar-06 4:40 
QuestionMailfrom? Pin
malloreigh14-Mar-06 14:53
malloreigh14-Mar-06 14:53 
AnswerRe: Mailfrom? Pin
Nathan Ridley14-Mar-06 15:12
Nathan Ridley14-Mar-06 15:12 
QuestionValidate if using &lt;select> Pin
UTNeflyte13-Mar-06 4:50
UTNeflyte13-Mar-06 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.