Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP HTML Form Help

Posted on 2011-09-15
6
Medium Priority
?
302 Views
Last Modified: 2012-05-12
I'm trying to run a simple asp script that will email the contents of a HTML form. I have some experience with PHP, but ASP is pretty much a mystery to me.

I found a script online, which seems to work, but the emails are getting stuck in the BADMAIL directory. It seems like there's a problem with the "from" address - error  0xC00402C7.

Also, the host doesn't appear to have MSSQL or MySQL setup, so I can't insert the form contents into a database. Can the contents be added to a text file? I don't foresee there being many submissions - probably under 100. Or, is there a better solution?

Any help would be greatly appreciated.
<form action="contact/sendmail_cdo.asp" id="frm_request"  name="frm_request" method="post" onsubmit="return checkFields();">
					<label for="firstName">First Name:</label>
					<input type="text" name="firstName" id="firstName" />
					<label for="lastName">Last Name:</label>
					<input type="text" name="lastName" id="lastName" />				
					<label for="City">City:</label>
					<input type="text" name="City" id="City" />
					<label for="State">State:</label>
					<input type="text" name="State" id="State" />
					<label for="mailfrom">Email:</label>
					<input type="text" name="mailfrom" id="mailfrom" />
					<label for="mailfrom">Email Again (confirm):</label>
					<input type="text" name="email_again" id="email_again" />                
					<label for="Message">Message:</label><br />
					<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
					<input name="submit" class="submit-button" type="submit" value="Send" />
					<input name="redirect" type="hidden" id="redirect" value="thanks.html" /> 
					<input name="mailto" type="hidden" id="mailto" value="%info%" /> 
					<input name="bcc" type="hidden" id="bcc" value="%infoBCC%" /> 
					<input name="subject" type="hidden" id="subject" value="Form Submission" /> 
					<input name="template" type="hidden" id="template" value="emailtemplate.html" /> 
					<input name="html" type="hidden" id="html" value="yes" />
                        
				</form>

Open in new window

<%
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%", "info@mysite.com"
pde.add "%info%", "my@email.com"
pde.add "%infoBCC%", "mybcc@email.net"
'---------------------------------------------------------------------------------------------------

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"))="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"
		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
%>

Open in new window

0
Comment
Question by:jej07
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:pateljitu
ID: 36543923
Please check this article:
http://classicasp.aspfaq.com/email/why-do-i-get-c00402ce/c00402c7-errors.html


Also in your code you have line:
if mailfrom = "" then mailfrom = pde("%mailfrom%"), but there is not defined malfrom in your dictionary, add this line

pde.add "%mailfrom%", "myfromemail@email.net" where you created object for dictionary
i.e. dim pde : set pde = createobject("scripting.dictionary")
0
 
LVL 1

Author Comment

by:jej07
ID: 36544358
Thank you. Defining mailfrom did allow the email to go through to the primary %info% address. However, the email to %infoBCC% got stuck in the queue.

Looking at that email, it looks like it tried to send it to %infoBCC% rather than the value mybcc@email.net.
0
 
LVL 15

Expert Comment

by:pateljitu
ID: 36544729
Did this error 0xC00402C7 get resolved.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jej07
ID: 36544762
Yes, it did.
0
 
LVL 1

Author Comment

by:jej07
ID: 36552124
The BCC is still ending up in the badmail directory.
0
 
LVL 15

Accepted Solution

by:
pateljitu earned 2000 total points
ID: 36568828
Try this article please:

http://classicasp.aspfaq.com/email/why-do-cdonts-messages-end-up-in-the-badmail-folder.html

Also for testing create a simple page with hard-coded values for To, From, Bcc, Subject and body and try sending email, if that does ends up in badmail on that case it has to be with SMTP setup issue.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question