Geoff Allen ASP Developer & Webmaster Homepage

Free ASP Email Processing Script

by geoff

This script allows you to email from your website, such as a membership confirmation or a feedback form processor, where the fields from your form are automatically included.  Supports HTML or TEXT emails, CC, BCC, and you can even attach files.

This script supports the 4 main email processing components; Jmail, ASPmail, ASPemail and CDONTS.  The benefit here is you can use the script for all your sites, and simply change the component to suit the web hosting.  I highly recommend you use JMAIL as the component as the way it handles HTML is superior to all the others.

To download the script called "clSendEmail.asp" see the form at the foot of this page, it not only acts as a demo of the script in action (displaying you the full DEBUG report) but it will also send you the script via email.

Quick Email

Use the code on your ASP page to call the script and send a simple email.

<!--#include file="clSendEmail.asp"-->
<%
Dim MyMail
set MyMail = new clSendEmail

 '''Set the SMTP server and the email component you wish to use
 MyMail.strSMTPServer = "smtp.YOURDOMAIN.com"
 MyMail.strComponent = "jmail"
 
 '''if the email is FROM YOU to a user then use this format
 MyMail.strFromEmail = "you@YOURDOMAIN.com"
 MyMail.strFromName = "YOUR NAME"
  
 '''Add the recipients of the email
 MyMail.AddAddress someone@someaddress.com,"RECIPIENT NAME"

 '''Set the Subject of the email
 MyMail.strSubject = "Test Email from clSendEmail"
 
 '''Set the TEXT based body of the email
 MyMail.strBody = "Hello, this is a test email using the " &_
    
MyMail.strComponent & " component." & vbcrlf & vbcrlf &_
    "If you are reading this then it seems to have worked!" & vbcrlf & vbcrlf
 
 '''This line sends the email
 MyMail.SendEmail
 
set MyMail = nothing
%>

Adding Multiple Recipients, CC and BCC recipients

When adding recipients, the Email address is required, but the real name is optional. Simply repeat the line for each recipient e.g.

 MyMail.AddAddress "someone@anotheraddress.com"
 MyMail.AddAddress "someoneelse@anotheraddress.com" 

Adding CC and BCC recipients is just as easy, just change the method involved accordingly e.g.

MyMail.AddCC "someone@anotheraddress.com" 
MyMail.AddBCC "someone@anotheraddress.com"

Attach a File

Numerous files can be attached to your email using a similar method.  The files must exist on your webserver and the full correct path specified e.g.
"c:\domains\mydoman.com\wwwroot\files\downloadfile.zip"
If you are unsure of the exact location use the ASP Server.Mappath command e.g.

MyMail.AddAttachment Server.mappath("downloadfile.zip")

Sending HTML Emails

If you wish to send the email in HTML format (not generally recommended) then use this code :

 MyMail.blnUseHTML = true
 MyMail.strHTMLBodyTop = "<html><body bgcolor=#ffff00>" &_
     "<H1>You have Feedback!</h1>"
 MyMail.strHTMLBody = "<p>Hello, this is a test HTML email using the " &_ 
    MyMail.strComponent & " component.</p>"
 MyMail.strHTMLBodyBottom = "</body></html>"


Note this is in addition to the strBody method, so you can set both TEXT and HTML body side by side.  If you use the JMAIL component then it actually sends both formats on the same email and displyas HTML if the client has it, or TEXT if not.  For this reason I highly recommend you use JMAIL rather than the other components supported by the script. 

User/Error/Debug Reporting

The script provides 3 levels of reporting which should be inlcuded AFTER the MyMail.SendMail command ;

if MyMail.ReportToUser <> "" then response.write MyMail.ReportToUser
if MyMail.ReportError <> "" then response.write MyMail.ReportError
if MyMail.ReportDebug <> "" then response.write MyMail.ReportDebug

ReportToUser is a short message like "Email Sent" or a non discript error such as "Email not sent due to an error".

ReportError will tell you what the error is (if any), and is included automatically in the ReportDebug which tells you as a developer exactly what is going on with the script which is very handy if something isnt working correctly.

Automatically process Feedback Forms

The script can automatically detect and include Form Data from any html based form you choose to create on your website.  The script does NOT produce the form itself, do that in Dreamweaver or similar

To include this feature simply call the method:

MyMail.CollectFormData

If you wish to specify only certain fields from your form, or simply wish to sort the data in a specific way then preceed the above line with something similar to this, replacing the form field names to the same as you use on your own form;

MyMail.strFormFields = "Name,Email,Message"
MyMail.CollectFormData

You can (optionally) reset the default templates used to include each form entry, to totally customise your email;

MyMail.strFormFieldsTemplate  = "%%FIELD%% : %%DATA%%"

MyMail.strFormFiledsHTMLTemplateTop = "<table border=1 width=100% " &_ 
        "cellpadding=5><tr><Td>Field Name</td><td>Data</td>"
MyMail.strFormFieldsHTMLTemplate ="<tr><td>%%FIELD%%</td><td>%%DATA%%</tr>"
MyMail.strFormFiledsHTMLTemplateBottom = "</table>"

Comprehensive Email

Using all the methods outlined above, here is the full code to send a TEXT and HTML email, complete with attached file, to a recipient with a CC and BCC recipient.

<!--#include file="clSendEmail.asp"-->
<%
Dim MyMail
set MyMail = new clSendEmail
 
'''Set the SMTP server and the email component you wish to use
 MyMail.strSMTPServer = "smtp.YOURDOMAIN.com"
 MyMail.strComponent = "jmail"
 
 '''if the email is FROM YOU to a user then use this format
 MyMail.strFromEmail = "you@YOURDOMAIN.com"
 MyMail.strFromName = "YOUR NAME"
 
'''or if this is a feedback form then the email is from user TO YOU
 '''change the request.form fields to represent the correct fields on your form.
 if request.form("Email") <> "" then MyMail.strFromEmail = request.form("Email")
 if request.form("Name") <> "" then MyMail.strFromName = request.form("Name")
  
 '''Add the recipients of the email
 MyMail.AddAddress "someone@someaddress.com","RECIPIENT NAME"
 
'''Add CC and/or BCC recipients
MyMail.AddCC "someone@anotheraddress.com" ,"Fred Boggs"
MyMail.AddBCC "someone@anotheraddress.com"
 
'''Add a file to the email
MyMail.AddAttachment Server.mappath("downloadfile.zip")
 
'''Set the Subject of the email
 MyMail.strSubject = "Test Email from clSendEmail"
 
 '''Set the TEXT based body of the email
 MyMail.strBody = "Hello, this is a test email using the " &_
    
MyMail.strComponent & " component." & vbcrlf & vbcrlf &_
    "If you are reading this then it seems to have worked!" & vbcrlf & vbcrlf
 
'''Set the HTML based body of the email
 MyMail.blnUseHTML = true
 MyMail.strHTMLBodyTop = "<html><body bgcolor=#ffff00>" &_
     "<H1>You have Feedback!</h1>"
 MyMail.strHTMLBody = "<p>Hello, this is a test HTML email using the " &_ 
    MyMail.strComponent & " component.</p>"
 MyMail.strHTMLBodyBottom = "</body></html>"
 
'''Auto collect and include Feedback Form data
MyMail.CollectFormData 
 
 '''This line sends the email
 MyMail.SendEmail
 
'''REPORT back
if MyMail.ReportToUser <> "" then response.write MyMail.ReportToUser
'if MyMail.ReportError <> "" then response.write MyMail.ReportError
'if MyMail.ReportDebug <> "" then response.write MyMail.ReportDebug
 
set MyMail = nothing
%>

Download the script

To see a demo of the script in action, including full DEBUG reporting, then please complete the form below, in the process you will receive an email from me with the script attached.

Your Name :

Your Email :

If you prefer simply download and extract this file clSendEmail.zip

    
Comments about this page
No comments posted as yet ...

Log In to add a comment ...



Home Page  Member Area  Bookmark Page  Email a Friend  Print Friendly  Change Template  Your Cart  Search  A to Z  Site Map  Feedback Form 

Powered by the ASPBite v8
Content Management System