Creating dynamic websites with ColdFusion
Part 3: Dynamic Email
by Michael Smith, TeraTech
What is ColdFusion
In this article we continue to look at what ColdFusion is
and how you can use it for dynamic website creation. We cover displaying
sending customized email and looping control in ColdFusion. We also mention
where you can learn more about ColdFusion, including the free CPCUG ColdFusion
Conference in June.
In case you missed the last article that introduced
ColdFusion, let me explain what it is. ColdFusion is a programming language
based on standard HTML (Hyper Text Meta Language) that is used to write dynamic
webpages. It lets you create pages on the fly that differ depending on user
input, database lookups, time of day or what ever other criteria you dream up!
ColdFusion pages consist of standard HTML tags such as <FONT SIZE=”+2”>
together with CFML (ColdFusion Meta Language) tags such as <CFQUERY>,
and <CFLOOP>. ColdFusion was introduced by Allaire in 1996
and is currently on version 4.0
The <CFMAIL> tag lets you send email to one or more
people from a web page. It uses any standard SMTP (Simple Mail Transport
Protocol) server such as MS Exchange or SLmail. <CFMAIL> has parameters
for who the email is TO, who it is FROM, a CC and SUBJECT. The text body of the
email is placed in the body of the tag between the opening <CFMAIL> and
the closing </CFMAIL> tags. To make this clearer let's write a simple
customer inquiry form that emails the inquiry to the marketing department
before saving it in a database for future use.
Sending form-based email
The customer enters their name, email, subject of the
inquiry and the message in a straight HTML input form:
<INPUT NAME="Subject" TYPE="text">
The output page uses the <CFMAIL> tag to send an email
from the customer to the marketing department. We also CC the email back to the
customer so that they will have a confirmation of their request.
inquiry was posted to our Web site:
Note that the same page could also insert the customer
inquiry into the database using the following <CFQUERY> tag.
INSERT INTO Leads
Email, Subject, InquiryText, LeadDate)
“#Form.Subject#”, “#Form.InquiryText#”, #CreateODBCDate(now())#)
Customized email mail merge
The above code sends one email at a time. What if we want to
email every lead in our Leads table from 7 days ago with a follow up email? The
first step is to write an SQL query either by hand (or by cut and paste from
Access's query builder) and use it in a <CFQUERY> tag:
WHERE int(LeadDate) =
We use the int() function in the SQL because when the
LeadDate field was created with the now() function it contains the time of the
lead -- and we only want to compare the date part of it. The int() function
takes only the integer part of the date and truncates off any fractional part,
so we are only comparing days.
Next we loop over the GetEmail query result set produced
above using the <CFLOOP> tag. This tag allows looping over query result
sets as here, for doing FOR NEXT style loops and for looping through a
comma-delimited list of values. In our example below we loop over the GetEmail
query that we created above and in each loop iteration we send one email using
<CFSET ListSent = ''>
<!--- for testing
uncomment the next line --->
Here are this month's
ListSent = ListSent & " #FirstName# #LastName#
" & CHR(13) &CHR(10) & CHR(13) &CHR(10)>
We also keep a running list of all people the email was sent
to in the variable ListSent. We keep adding the name and email of onto this
variable using the <CFSET> tag. A carriage return/linefeed pair (ASCII
character 13 and 10) are added too to separate the lines in the output. This is
so after the loop is complete we can email ourselves an email showing all the
people emailed to using a final <CFMAIL> tag:
Sending followup email
Why do we bother sending this final email? Well our plan is
to run the automated reminder email from a ColdFusion scheduled task on a daily
basis. Scheduled task pages run in the background rather than by you entering
their address in a web browser. So there is no direct way to tell if they have
run successfully (or at all!). The last email we send to ourselves is therefore
to confirm that the program is working correctly. Also it helps keep track of
who is requesting information from our website without having to check the
I use a similar hidden CFMAIL to myself in error handling
code. After all if a web page in the middle of a forest of pages falls over
does anyone hear it crash? With an error handler and CFMAIL you can!
Note: <CFMAIL> does have a QUERY parameter that allows
it to send emails to every record in a result set, but I prefer the above
CFLOOP method, as it allows for more control over the emails sent and lets me
keep a list of sent emails more easily.
Other Internet Protocols
In addition to sending email ColdFusion lets you get and put
data via several standard internet protocols. This includes receiving email
from a POP server, getting data from other web pages and directory lookup. Here
is a summary.
Send E-mail (SMTP)
Dynamically build and send e-mail messages. Use static
information, form inputs or query results to control the addresses and
content of e-mail messages. Send out hundreds of customized e-mail messages
at a time. Allows easy creation of HTML e-mail for groupware and workflow
Retrieve Internet e-mail from POP servers and incorporate
them into ColdFusion applications. Allows for e-mail based application
interfaces, automated e-mail gathering and distribution, and intelligent
e-mail applications, such as 'auto-responders' and 'listservs'. Supports all
POP servers, leave mail on server, selective message retrieval and deletion,
and MIME attachments.
Get Web Page
Interface to distributed services and Web servers using
HTTP. Create distributed queries and build custom 'agent' style applications.
Supports HTTP GET and POST, including MIME attached files, and the creation
of 'recordsets' from returned results. Also supports standard Web server
authentication and SSL encryption.
Interface with directory servers that support the
Lightweight Directory Access Protocol (LDAP) such as Netscape's Directory
Server, Microsoft's Exchange Server, Windows NT directory, Novell NDS
directories, Banyan Vines, and dozens of public Internet-based directories.
Supports search, add, update, delete, authenticated access, etc.
In conclusion ColdFusion's CFMAIL tag lets you easily send
email to one or more people and can be used with a query for mail merge
To Learn More
If you are interesting in learning more about ColdFusion
CPCUG and TeraTech are holding a free ColdFusion User Conference on Saturday
6/26/99 at the Masur Auditorium from 9am to 6pm. The morning will contain
introductory sessions and the afternoon advanced ones. You can sign up for the
conference at http://www.teratech.com/cfconf/
or call 301-424-3903.
You can download a free 30 day-evaluation version of
ColdFusion from Allaire or request a free eval CD-ROM from
TeraTech. You can contact Allaire directly at:
1 Alewife Center
Cambridge, MA 02140
Tel: 617.761.2000 voice
Fax: 617.761.2001 fax
Toll Free: 888.939.2545
ColdFusion single user server costs $395 and the multi-user
one $1295 direct from Allaire. TeraTech is offering a 10% discount for all
CPCUG members and their organizations when you buy ColdFusion. Call
301-424-3903 for this
Allaire also maintain an extensive knowledge basis and
tech support forums on their website.
CPCUG and TeraTech ColdFusion
TeraTech maintains a ColdFusion code cuttings called
ColdCuts at http://www.teratech.com/ColdCuts/.
This page also has links to about a dozen ColdFusion white papers in the CF
The DC ColdFusion User Group meets the first Wednesday
each month at Figleaf , 16th and P St NW, Washington DC. See the
DCCFUG page on http://www.figleaf.com/
for details and directions.
The Maryland ColdFusion User Group meets the second
Tuesday of each month at Backstreets Cafe, 12352 Wilkins Avenue, Rockville. See http://www.cfug-md.org/ for details and
Michael Smith is president of TeraTech, a ten year old
Rockville Maryland based consulting company that specializes in ColdFusion,
Database and Visual Basic development. You can reach Michael at email@example.com or 301-424-3903.