ASP Script to Send an email which generates into a HTML format

R

Robin1979

Hi

I have an asp script working with Frontpage which collects the data from a
form and send them in an email. The plain text version works fine but I am
also able to use an email html template to collect this data by adding
[$field_name$] into the html so that it populates the form data into the html
email template thus all making it a lot easier to read.

The only trouble is when I setup the html template it does not always pull
through all of the fields that are populated in the form and does not seem to
have any reasoning behind it, even if I create a blank template with just
only the [$field_name$] on it to test they still don't come through. The asp
script is below.

Any help would be appreciated, many thanks.

<%
option explicit

'---------------------------------------------------------------------------------------------------
'FORM MAIL SCRIPT
'----------------
'usage:
'<form ACTION="sendmail.asp" ...>
'
'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. <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%", "(e-mail address removed)"
pde.add "%salesenquiry%", "(e-mail address removed)
'---------------------------------------------------------------------------------------------------
Dim gtTextFromFile
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("Booking Request")
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
%>
 
R

Ronx

Are you actually setting up the fields in the template file, or using
the default [$field_name$] for each field?

For example:

<!-- Start Template -->
<p>[$name$]</p>
<p>[$email$]</p>
<h1>[$subject$]</h1>
<pre>[$comments$]</pre>
<!-- End Template -->

If using the template, only the fields listed on the template will be
used (apart from those directly concerned with the email headers).
--
Ron Symonds - Microsoft MVP (FrontPage)
Reply only to group - emails will be deleted unread.

http://www.rxs-enterprises.org/fp




Hi

I have an asp script working with Frontpage which collects the data from a
form and send them in an email. The plain text version works fine but I am
also able to use an email html template to collect this data by adding
[$field_name$] into the html so that it populates the form data into the html
email template thus all making it a lot easier to read.

The only trouble is when I setup the html template it does not always pull
through all of the fields that are populated in the form and does not seem to
have any reasoning behind it, even if I create a blank template with just
only the [$field_name$] on it to test they still don't come through. The asp
script is below.

Any help would be appreciated, many thanks.

<%
option explicit

'---------------------------------------------------------------------------------------------------
'FORM MAIL SCRIPT
'----------------
'usage:
'<form ACTION="sendmail.asp" ...>
'
'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. <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%", "(e-mail address removed)"
pde.add "%salesenquiry%", "(e-mail address removed)"
'---------------------------------------------------------------------------------------------------
Dim gtTextFromFile
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("Booking Request")
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
%>
 
R

Robin1979

All of the form fields are uniquly named eg Town, Company, Site, Requested_By
etc etc.... I then put these into the template [$Town$] [$Requested_By$] etc
etc

Some of them pull through, others don't.... Here is an example of a very
basic test I did to try and find out why it is not working:

<p>[$ZSO$] [$Site$] [$Company$] [$town$]</p>

When I get the email results I only get ZSO, Site, Company but town does not
come through at all even though the form field is called town! It is not
just these fields that I have a problem with quite a few don't seem to work.

Thanks
Robin


Ronx said:
Are you actually setting up the fields in the template file, or using
the default [$field_name$] for each field?

For example:

<!-- Start Template -->
<p>[$name$]</p>
<p>[$email$]</p>
<h1>[$subject$]</h1>
<pre>[$comments$]</pre>
<!-- End Template -->

If using the template, only the fields listed on the template will be
used (apart from those directly concerned with the email headers).
--
Ron Symonds - Microsoft MVP (FrontPage)
Reply only to group - emails will be deleted unread.

http://www.rxs-enterprises.org/fp




Hi

I have an asp script working with Frontpage which collects the data from a
form and send them in an email. The plain text version works fine but I am
also able to use an email html template to collect this data by adding
[$field_name$] into the html so that it populates the form data into the html
email template thus all making it a lot easier to read.

The only trouble is when I setup the html template it does not always pull
through all of the fields that are populated in the form and does not seem to
have any reasoning behind it, even if I create a blank template with just
only the [$field_name$] on it to test they still don't come through. The asp
script is below.

Any help would be appreciated, many thanks.

<%
option explicit

'---------------------------------------------------------------------------------------------------
'FORM MAIL SCRIPT
'----------------
'usage:
'<form ACTION="sendmail.asp" ...>
'
'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. <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%", "(e-mail address removed)"
pde.add "%salesenquiry%", "(e-mail address removed)"
'---------------------------------------------------------------------------------------------------
Dim gtTextFromFile
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("Booking Request")
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
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top