MGinternet, Web Design Ireland
 
Welcome Guest, Please Login Here
 
RESOURCES
MY ACCOUNT
Home > Resources > ASP Source Code > Form to Mail
ASP Source Code - ASP CMS - Irish Web Hosting - Web Design Ireland - MGinternet

Rated:
by Aspin.com users
What do you think?
This form to e-mail script allows you to post any form to it and have it send an e-mail using hidden field values. Update now orders field entries for the email on the basis of how they are ordered in the form. Uses CDONTS. Easy to install and use.

Installation: Follow the instructions commented in the source code.

ASP Part

<%
'*******************************************************
'Background to this script
'*******************************************************
'
'Script: ASP Form to Mail Script
'Version: 1.2
'
'Version 1.2 Updates:
' - added a bit of script to check the default settings and
'notify the admin if they weren't changed. Did this because
'test emails kept coming into us and users didn't know why
'it was not sending the mail to them. This update, tells them
'of the problem and how to fix it.
'
'Version 1.1 Updates:
' - Script now orders the fields on the basis of
' how it receives them.
' - Unnecessary submit field removed from the email
' - Addition of the sent from link to tell you where the
' mail was sent from. Good if being used from different
' places on a website or different website completely
' - Addition of the "mail_send" hidden field so the
' script can run on the same page as the feedback form
'
'Date: 23 September 2003
'Company: MGinternet, Meath, Ireland
'E-Mail: scripts@mginternet.com
'Web Site: http://www.mginternet.com
'
'*******************************************************
'INSTALLATION AND CONFIGURATION
'*******************************************************
'
'This script is provided as is. Modify the code to
'suit your needs but it is set up to be as easy to
'install and use as possible.
'
'This script should be placed on a windows server
'with support for CDONTS and ASP. The idea of this
'script is that you can send a mail to whoever you
'want, like a feedback form without having to
'rewrite the script that actually sends the e-mail
'each time you want to do this.
'
'Place this file on any directory and call it
'sendmail.asp. Set up as many other pages as you
'want that will use this script to send feedback.
'Even allow multiple sites to use the same script on
'your web site to send feedback. Set up a form on
'another page with whatever text boxes, select
'boxes, text areas, etc.
'
'Included in your form should be the following
'hidden fields: mail_from, mail_to, mail_cc,
'mail_bcc, mail_subject, mail_importance,
'mail_redirect, mail_send. The values of these hidden fields
'will determine who the mail goes to, who it's from,
'subject, redirection to thank you page, etc.
'There are defaults if values are empty but if you
'do not enter a value in the mail_to hidden field
'the mail WILL NOT send.
'
'All that is left to do is point your feedback
'form to this page and let the script do the rest.
'Follow these instructions and you won't have to
'edit any asp code at all. There is a sample feedback
'form below to get you started as well.
'
'ENJOY!!!!
'
'*******************************************************

'*******************************************************
'If form was submitted then send the mail
'*******************************************************

'The hidden variable called "mail_send" must be set to "y" for the mail to send
IF Request.Form("mail_send") = "y" THEN

 

'*******************************************************
'Get values from hidden fields for sending the mail
'*******************************************************

form_from = Request.Form("mail_from") 'who is the mail from, e.g. feedback@yoursite.com
form_to = Request.Form("mail_to") 'who is the mail to, e.g. info@yourcompany.com
form_cc = Request.Form("mail_cc") 'leave blank if not needed! who is the carbon copy to be sent to, e.g. joe@yourcompany.com
form_bcc = Request.Form("mail_bcc") 'leave blank if not needed! who is the blind carbon copy to be sent to, e.g. karen@yourcompany.com
form_subject = Request.Form("mail_subject") 'text to appear in subject line of the e-mail
form_importance = Request.Form("mail_importance") 'importance of the e-mail. Must be a number 0, 1 or 2. 2 = High, 1 = Normal, 0 = Low
form_redirect = Request.Form("mail_redirect") 'page to redirect to after sending e-mail, e.g. http://yoursite.com/thankyou.htm

 

'*******************************************************
'If values are still the defaults, tell the user and stop the script
'*******************************************************

IF form_from = "feedback@yoursite.com" THEN
errorno = errorno + 1
errortext = errortext & "<b>" & errorno & ".</b> Default setting for the from address has not been changed in the hidden field<br>"
END IF

IF form_to = "scripts@mginternet.com" THEN
errorno = errorno + 1
errortext = errortext & "<b>" & errorno & ".</b> Default setting for the to address has not been changed in the hidden field<br>"
END IF

IF form_subject = "Greetings from Ireland!!" THEN
errorno = errorno + 1
errortext = errortext & "<b>" & errorno & ".</b> Default setting for the mail subject has not been changed in the hidden field<br>"
END IF

IF errorno > 0 THEN
Response.Write("<b>ERROR:</b> You have not changed some of the default settings in the sample html form. You need to fix the errors below before the script will run correctly. Once all errors are fixed, the script will run as it is supposed to.<br><br>")
Response.Write(errortext)
Response.Write("<br>Script ended. No mail has been sent.")
Response.End
END IF

 

'*******************************************************
'If values are empty, put in some defaults
'*******************************************************

IF form_from = "" THEN form_from = "Webmaster <you@yourwebsite.com>"
IF form_to = "" THEN form_to = ""
IF form_cc = "" THEN form_cc = ""
IF form_bcc = "" THEN form_bcc = ""
IF form_subject = "" THEN form_subject = "Dynamic Feedback"
IF form_importance = "" THEN form_importance = 1
IF form_redirect = "" THEN form_redirect = ""


'*******************************************************
'Get all form elements and structure the e-mail
'*******************************************************

FOR x = 1 TO Request.Form.Count
IF Request.Form.Key(x) = "mail_from" OR Request.Form.Key(x) = "mail_to" OR Request.Form.Key(x) = "mail_cc" OR Request.Form.Key(x) = "mail_bcc" OR Request.Form.Key(x) = "mail_subject" OR Request.Form.Key(x) = "mail_importance" OR Request.Form.Key(x) = "mail_redirect" OR Request.Form.Key(x) = "mail_send" OR Request.Form.Key(x) = "Submit" THEN
form_variables = form_variables
ELSE
form_variables = form_variables & Request.Form.Key(x) & ": " & vbcrlf & Request.Form.Item(x) & vbcrlf & vbcrlf
END IF
NEXT

DIM body_text
body_text = vbcrlf & "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & vbcrlf
body_text = body_text & form_subject & vbcrlf
body_text = body_text & "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & vbcrlf & vbcrlf
body_text = body_text & form_variables
body_text = body_text & "Sent from: " & vbcrlf & Request.ServerVariables("HTTP_REFERER") & vbcrlf & vbcrlf
body_text = body_text & "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & vbcrlf
body_text = body_text & "Script Provided by MGinternet" & vbcrlf
body_text = body_text & "http://www.mginternet.com" & vbcrlf

'*******************************************************
'Get values from hidden fields for sending the mail
'*******************************************************
SET objMail = Server.CreateObject("CDONTS.NewMail")
objMail.BodyFormat = 1
objMail.MailFormat = 1
objMail.From = form_from
objMail.To = form_to
objMail.CC = form_cc
objMail.BCC = form_bcc
objMail.Subject = form_subject
objMail.Importance = form_importance
objMail.Body = body_text
objMail.Send 'This line actually sends the e-mail
SET objMail = NOTHING


'*******************************************************
'Redirect to thank you page or write thank you
'*******************************************************
IF form_redirect = "" THEN
Response.Write("Thank You. Mail Sent.")
ELSE
Response.Redirect(form_redirect)
END IF

'*******************************************************
'If form was submitted then show what is below "END IF"
'*******************************************************
END IF

'*******************************************************
'There is a sample Feedback Form below to start you off.
'That's it. Hope you benefit from it. Happy Mailing!
'*******************************************************

%>

HTML Part

<!-- Copy this form for use in your web site - modify as you require -->
<!-- Be sure to use the hidden fields properly as they govern the mail script -->

<form name="feedbackform" method="post" action="sendmail.asp">
<table width="400" border="0" cellspacing="0" cellpadding="5">
<tr>
<td valign="top">Message:</td>
<td><textarea name="Message" cols="24" rows="8" id="Message"></textarea></td>
</tr>
<tr>
<td width="120" valign="top">Name:</td>
<td width="280"><input name="Name" type="text" id="Name" size="24"></td>
</tr>
<tr>
<td width="120" valign="top">Address:</td>
<td width="280"><textarea name="Address" cols="24" rows="5" id="Address"></textarea></td>
</tr>
<tr>
<td width="120" valign="top">Phone:</td>
<td width="280"><input name="Phone" type="text" id="Phone" size="24"></td>
</tr>
<tr>
<td width="120" valign="top">Fax:</td>
<td width="280"><input name="Fax" type="text" id="Fax" size="24"></td>
</tr>
<tr>
<td width="120" valign="top">E-Mail:</td>
<td width="280"><input name="E-Mail" type="text" id="E-Mail" size="24"></td>
</tr>
<tr>
<td width="120" valign="top">Web Site:</td>
<td width="280"><input name="Web Site" type="text" id="Web Site" size="24"></td>
</tr>
<tr>
<td width="120" height="50"> <input name="mail_from" type="hidden" id="mail_from" value="feedback@yoursite.com">
<input name="mail_to" type="hidden" id="mail_to" value="scripts@mginternet.com">
<input name="mail_cc" type="hidden" id="mail_cc"> <input name="mail_bcc" type="hidden" id="mail_bcc">
<input name="mail_subject" type="hidden" id="mail_subject" value="Greetings from Ireland!!">
<input name="mail_importance" type="hidden" id="mail_importance" value="2">
<input name="mail_redirect" type="hidden" id="mail_redirect">
<input name="mail_send" type="hidden" id="mail_send" value="y"></td>
<td width="280" height="50"><input type="submit" name="Submit" value="Send"></td>
</tr>
<tr>
<td width="120">&nbsp;</td>
<td width="280">&nbsp;</td>
</tr>
</table>
</form>