In this article we continue to look at what ColdFusion is and how you can use it for dynamic website creation. This article covers displaying and sending form-based and customized e-mail and looping control in ColdFusion.
What is ColdFusion? In case you missed the last article that introduced ColdFusion, let me explain what it is. ColdFusion, which was introduced by Allaire in 1995 and is currently on version 4.0, is a programming language based on standard HTML (Hyper Text Markup 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 any other criteria you dream up. ColdFusion pages consist of standard HTML tags such as <FONT SIZE="+2"> together with CFML (ColdFusion Markup Language) tags such as <CFQUERY>, <CFIF> and <CFLOOP>.
CFMAIL The <CFMAIL> tag lets you send e-mail 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 e-mail is TO, who it is FROM, a CC and SUBJECT. The text body of the e-mail 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 e-mails the inquiry to the marketing department before saving it in a database for future use.
Sending form-based e-mail The customer enters their name, e-mail, subject of the inquiry and the message in a straight HTML input form:
The output page uses the <CFMAIL> tag to send an e-mail from the customer to the marketing department. We also CC the e-mail back to the customer so that they will have a confirmation of their request.
<CFMAIL FROM="#Form.E-mailAddress#"
TO="marketing@allaire.com"
CC="#Form.E-mailAddress#"
SUBJECT="Customer Inquiry">
The following inquiry was posted to our Website:
Name: #Form.FirstName# #Form.LastName#
Subject: #Form.Subject#
#Form.InquiryText#
</CFMAIL>
Note that the same page could also insert the customer inquiry into the database using the following <CFQUERY> tag.
Customized e-mail mail merge The above code sends one e-mail at a time. What if we want to e-mail every lead in our Leads table from seven days ago with a follow up e-mail? 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:
<CFQUERY NAME="GetE-mail" DATASOURCE="CompanyDB">
SELECT FirstName, LastName, E-mail
FROM Leads
WHERE int(LeadDate) = int(#now()#)-7
</CFQUERY>
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 GetE-mail 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 GetE-mail query that we created above and in each loop iteration we send one e-mail using <CFMAIL>:
<CFSET ListSent = ''>
<CFLOOP QUERY="GetE-mail">
<!--- for testing uncomment the next line --->
<!--- cfset ThisE-mail="michael@teratech.com"--->
<CFSET ThisE-mail=e-mail>
<CFMAIL TO="#ThisE-mail#"
FROM="<info@teratech.com> TeraTech"
SUBJECT="Programming News"
SERVER="smtp.mycompany.com">
Dear #FirstName#,
Here are this month's programming tips
</cfmail>
<CFSET ListSent = ListSent & " #FirstName# #LastName#
mailto:#ThisE-mail# " & CHR(13) & CHR(10) & CHR(13) & CHR(10)>
</CFLOOP>
Testing and Debugging Applications As you build your ColdFusion application pages, you can test pages by simply opening them in a browser. There is no need to compile or link your pages. You can make a tiny change and see the results of your change immediately by simply opening the page in your browser. Most ColdFusion developers run ColdFusion and a Web server locally, on their own computers, and test applications by editing and viewing or running pages side-by-side. Once your application is ready, you can very easily deploy your pages to a remote server.
ColdFusion provides several debugging options to help you troubleshoot your application. For every ColdFusion transaction — that is, every time a browser requests a ColdFusion page — debugging data can be viewed that provides information about the operation to help you track down problems and coding errors. With debugging activated, this information is displayed in your Web browser at the bottom of every application page.
We also keep a running list of all people the e-mail was sent to in the variable ListSent. We keep adding the name and e-mail of those people onto this variable using the <CFSET> tag. A carriage return/linefeed pair (ASCII character 13 and 10) is added too to separate the lines in the output. This is so that after the loop is complete we can e-mail ourselves an e-mail showing all the people e-mailed to using a final <CFMAIL> tag:
<CFMAIL TO="michael@teratech.com"
FROM="info@teratech.com"
SUBJECT="Auto e-mails"
SERVER="smtp.mycompany.com">
Sending followup e-mail on
#dateformat(now())#
Name E-mail
#listsent#
</cfmail>
Why do we bother sending this final e-mail? Well, our plan is to run the automated reminder e-mail 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 e-mail 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 database.
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 e-mails to every record in a result set, but I prefer the above CFLOOP method, as it allows for more control over the e-mails sent and lets me keep a list of sent e-mails more easily.
Other Internet protocols In addition to sending e-mail, ColdFusion lets you get and put data via several standard internet protocols. This includes receiving e-mail from a POP server, getting data from other web pages and directory lookup. Here is a summary.
Standard Internet Protocols
Send E-mail (SMTP) <CFMAIL>
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 applications.
Retrieve E-mail <CFPOP>
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 <CFHTTP>
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.
Directories <CFLDAP>
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.
Conclusion In conclusion, ColdFusion's CFMAIL tag lets you easily send e-mail to one or more people and can be used with a query for mail merge programs.
Creating Dynamic Websites With ColdFusion — The CF Apprentice Series
About Michael Smith Michael Smith is president of TeraTech (www.teratech.com/), an 11-year-old Rockville, Maryland-based consulting company that specializes in ColdFusion, database, and Visual Basic development.
Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice: