Click here to Skip to main content
15,893,588 members
Articles / Web Development / HTML

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 496.1K   9.4K   60  
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.
<%
'---------------------------------------------------------------------------------------------------
'FORM MAIL SCRIPT
'----------------
'usage:
'<form ACTION="sendmail.asp" ...>
'
'Hidden fields to use in the form:
'	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) Templates 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.
'---------------------------------------------------------------------------------------------------

option explicit

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")
cc = request.form("cc")
bcc = request.form("bcc")
mailfrom = request.form("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("CDONTS.NewMail")
if usetemplate and lcase(request.form("html")) = "yes" then
	msg.bodyformat = 0 '(html)
	msg.mailformat = 0 '(mime)
end if
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

msg.body = body
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
%>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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