Click here to Skip to main content
Click here to Skip to main content

A versatile HTML form mail script for classic ASP

By , 15 Mar 2006
 

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

About the Author

Nathan Ridley
Web Developer
Australia Australia
Member
Web application developer, graphic designer, aspiring entrepreneur, snowboarder and electronic music afficianado.
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionError when submit from non IE browsermemberinfogmt15 May '12 - 21:53 
Hi Guys,
Do you have any issue when submitting the form under chrome, safari or android phone? I have issue with an error 500. IE is working fine. I can't afford to have an important part to have any error during submitting using a non IE browser cause there are more than 40% of the user are on non IE browser.
 
Let me know. Thanks!
GeneralMy vote of 5membermanoj kumar choubey27 Apr '12 - 1:28 
Nice
GeneralForm submit to servermemberAjay Kale New18 Oct '10 - 20:48 
Hi ,
 
I am submitting a html form in ASP.NET 1.1, to a websphere server. I am browsing excel file and submitting through object to Websphere server, by sybmitting form with action as Websphere hosted address.
 
Whenever I login to app, and try to upload the file with above functionality, for the first time, I get redirected to Login.aspx automatically. But when I re-login again and does the same thing as above, the file is uploaded successfully.
 
Can you please help me regarding this, as I have maintained releative values of timeout for session and form-authentication in web.config file.
 
- Ajay K
GeneralMy vote of 5memberRoy Ritchie12 Jul '10 - 23:47 
easy to use any very detailed for use.
GeneralStop using this script! It is YEARS out of date!memberNathan Ridley24 Aug '08 - 15:30 
Guys, I wrote this script years ago. It uses classic ASP, which is SO out of date it's not funny. If you want to send mail, go look on google for formmail.cgi, or find a php mail script, or find an ASP.Net script. Trying to resolve ASP issues at this point is like asking for support on an old Apple IIE computer.
 
NATHAN RIDLEY
Web Application Developer | www.contentcanvas.com
email: snowdevil [@] gmail.com

GeneralRe: Stop using this script! It is YEARS out of date!memberadstatus24 Aug '08 - 15:54 
Much appreciated!
GeneralRe: Stop using this script! It is YEARS out of date!memberteddio15 Sep '09 - 9:56 
not compatible with Windows 2003
QuestionLaunching Script problemsmemberadstatus24 Aug '08 - 13:50 
I have the script installed, html form and email template set-up correctly , but when I select "Send" on the form, the browser simply opens the asp text file rather than executing the script.
 
I'm certain there is something very simple i've overlooked, but I'm banging my head against the wall right now.
 
Anyone have some insight?
 
Thanks
QuestionCDO.Message.1 error '80040220'memberDesmond chee20 Aug '08 - 18:10 
i encountered the problem after clicked Send.
CDO.Message.1 error '80040220'
 
The "SendUsing" configuration value is invalid.
 
/sendmail_cdo.asp, line 120
 
Please guide me to solve this problem
 
Thanks
AnswerRe: CDO.Message.1 error '80040220'memberalimzhan2 Feb '09 - 9:46 
try this
 
http://www.codeproject.com/KB/asp/cdoex.aspx
AnswerRe: CDO.Message.1 error '80040220'membershant19764 Nov '09 - 11:44 
I was facing same problem, try the code posted on: -
http://prabhat.me/2009/11/04/send-e-mail-using-cdo/[^]
GeneralHelp with this asapmemberNoughty2 Aug '08 - 8:54 
This is what i got after including all necessary things like smtpserver, port, senduing:
 
CDO.Message.1 error '80040213'
 
The transport failed to connect to the server.
 
Pls help solve this problem.
GeneralWhere to add smtp server name within asp filemembersonal130723 May '08 - 15:37 
I am new to ASP and went through the sample code thoroughly but do not know where I am to add my smtp server or if I need to add it at all. I get the thanks.htm page but no email sent when email is placed. Can anyone specify where I would need to change code to test this out to go to my email.
 
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. <input type="hidden" name="mailto" value="%stratdepartment%">
'ALSO, in the same way, you can use %mailfrom% to hide the originating email address
pde.add "%contactform%", "helpme@yahoo.com"
pde.add "%salesenquiry%", "helpme@yahoo.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("redirect")
mailto = request.form("mailto")
if pde.exists(mailto) then mailto = pde(mailto)
cc = request.form("cc")
bcc = request.form("bcc")
mailfrom = request.form("mailfrom")
if mailfrom = "" then mailfrom = pde("%mailfrom%")
subject = request.form("subject")
message = request.form("message")
template = request.form("template")
testmode = lcase(request.form("testmode"))="yes"
 
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"
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,"<br>"))
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 "<pre>" & 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 & string(80,"-") & "</pre>"
response.write body
else
response.write "<html><head><title>Sendmail.asp Test Mode</title></head><body><pre>" & 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**</pre></body></html>"
end if
else
msg.send
response.redirect redir
end if
set msg = nothing
%>
GeneralRe: Where to add smtp server name within asp filememberNathan Ridley23 May '08 - 15:42 
Wow... you realise this article is about 5 years old, right? I honestly can't remember enough to answer the question. Classic ASP isn't really something you should be learning in this day and age. Go check out ASP.Net or PHP.
 
NATHAN RIDLEY
Web Application Developer
email: snowdevil [@] gmail.com
 
!! Are you a skilled ASP.Net programmer or web designer and would like to help bring the best new ASP.Net CMS (http://www.sprocketcms.com[^]) into the forefront of the ASP.Net community? Please contact me and let me know.

QuestionRobots filling out formmemberjadams746930 Nov '07 - 10:17 
I have your form up and running on my site and it works great. It works so great that I get robots filling it out and sending me sales pitches for viagra and the like Mad | :mad: . What can I do to prevent this? Can some sort of validation be added that is not machine readable? The form is on my main page, I suppose moving it would help, but I like it where it is. Any ideas?
 
John in PA
AnswerThe &quot;SendUsing&quot; configuration value is invalid. [modified]memberPR_Worldofrugs22 Oct '07 - 7:31 
Has anybody found a solution to this error?
At my current domain www.worldofrugs.com the sendmail_cdo.asp script works wonderful!, but at my new hosting I get this error (see www3.worldofrugs.com/Password.html, submit the form and see the error)
 
Any help would be highly appriciated, as I am lost with this!
Thanks!
 

-- modified at 15:22 Monday 22nd October, 2007
 
Ok, found the solution!
I added:
msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.www.myserver.com"
 
This worked perfect!
Hope it will help others ;o)
Questioncan form output be saved to a file aswell?memberdannif29 Aug '07 - 5:27 
Hi,
I've found your script really useful. However, I would like to now be able to save the details filled in the form to a file as well as emailing them on. Is this possible ? If so, I'd appreciate any help with this?
Thanks
Danielle
Questionerror '8004020d'membermccallchick8 Aug '07 - 14:44 

I've never done a form before so please excuse me if I'm being thick... I keep getting an error message:
 
CDO.Message.1 error '8004020d'
 
At least one of the From or Sender fields is required, and neither was found.
 
/lightfoot/contact/sendmail_cdo.asp, line 120
 
*******************************
 
I've got the form set up, see code: http://www.lightfootfoundation.com/contact/form.htm
 
And the form results see code: http://www.lightfootfoundation.com/contact/formresults.htm
 
I'm using sendmail_cdo.asp (and am probably messing up here):
http://www.lightfootfoundation.com/contact/CDO.htm
 
********************************
Any help would be greatly appreciated.
 
Brenda
 

AnswerRe: error '8004020d'memberalimzhan2 Feb '09 - 9:47 
try this one
 
http://www.codeproject.com/KB/asp/cdoex.aspx[^]
QuestionEmail not sending.memberdeinemuse26 Jun '07 - 5:47 
I am very new to ASP and am trying to get this to work. The CDO code itself would submit, but I would get no email so I added the send mail configs as follows under msg.from = mailfrom on line 66. I have the correct sendmail.server.com in there as well. Now the page submits, but gives me HTTP 500 internal error.
 
msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="sendmail.mjmi.com"
msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
msg.Configuration.Fields.Update
 
What am I doing wrong here?
 
Thanks for any help.
GeneralNot all fields populating in emailmemberkenny78220 Apr '07 - 20:18 
Well first nice script man Smile | :) Very easy to setup
 
I just set it up on a form I had.
I added the correct field names in the format shown in the sample email template.
I also set the neccessary hidden fields in my form.
 
I get the message however the last 5 fields come up empty even though I field them out.
 
I have triple checked the field names and they are fine.
 
Thanks, Kenny
GeneralRe: Not all fields populating in emailmemberkenny78222 Apr '07 - 11:43 
I figured it out myself, hopefully this helps someone.
 
The problem was the carriage returns etc in the remaing fields which were all text areas.
 
I found nice little script which I call before submitting it replaces all my carriage returns with

 
http://javascript.internet.com/forms/removereturns.html
 

I also put my destinations in my email template within PRE tags. That saved any extra spaces tabs etc that was in the info.
For Example:
[$TextArea1]
 

And once again great job on this script man!
I'm gonna look like a genius when I show them this Smile | :)
GeneralRe: Not all fields populating in emailmemberkenny78222 Apr '07 - 11:44 
Above example should be:
 
[$TextArea1$]

QuestionDo not know how to usememberChee Shai Choon3 Apr '07 - 7:47 
I am new to ASP. Can you help me on how to work on it. Because i read through your note and found not really understand.
I worked on it but it doesn't work. program just jump to your data text mode of sendmail_cdo.asp.Confused | :confused:
AnswerRe: Do not know how to usememberadstatus24 Aug '08 - 13:06 
I have the exact same issue. Did you ever get it resolved?
Chee Shai Choon wrote:
program just jump to your data text mode of sendmail_cdo.asp. [Confused]

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 16 Mar 2006
Article Copyright 2003 by Nathan Ridley
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid