The i-Technology Media!
Register | Log in
   
 
.NET  ·  AJAX  ·  CLOUD  ·  ECLIPSE  ·  FLEX  ·  OPEN WEB  ·  iPHONE  ·  JAVA  ·  LINUX  ·  OPEN SOURCE  ·  ORACLE  ·  PBDJ  ·  SEARCH  ·  SILVERLIGHT  ·  SOA  ·  VIRTUALIZATION  ·  WEB 2.0  ·  WIRELESS  ·  XML
Comments
Google Wave Invitation Giveaway
By Aditya Banerjee
Timo Hirvonen wrote: I would really appreciate an invitation. Been desperately trying to find one :) timo [dot] hirvonen [at] gmail [dot]com
Nov. 27, 2009 11:13 AM EST
Cloud Expo on Google News
Did you read today's front page stories & breaking news?


2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts

2009 East
GOLD SPONSORS:
CA
Get Your Transactions Under Control: SOA Performance Management
Software AG
Performance Driven Adoption: The Secret to Advancing SOA
Intel
The Evolving SOA Appliance: 3 Game-Changing Innovations
SILVER SPONSOR:
Denodo
Data Mashups: Deliver Your Project Faster with Virtualized Data Services Across Internal & External Sources
POWER PANELS:
The Business Value of Service Orientation
Driving Profitability Through User Experience
Click For 2008 West
Event Webcasts
Live Google News by SYS-CON!
Top Three Links You Must Click On


CF Basics
Creating Dynamic Websites With ColdFusion
The CF Apprentice Series - Part 3: Dynamic e-mail

By: Michael Smith
Sep. 16, 2003 12:00 AM

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:

<HTML>
<BODY>
Request more information.<BR>
<FORM ACTION="RequestInfoSub.cfm" METHOD="POST">
Name: <INPUT NAME="FirstName" TYPE="text">
     <INPUT NAME="LastName" TYPE="text"><BR>
E-mail: <INPUT NAME="E-mailAddress" TYPE="text"><BR>
Subject: <INPUT NAME="Subject" TYPE="text">
Details: <TEXTAREA NAME="InquiryText"></TEXTAREA>
<INPUT NAME="submit" TYPE="submit">
<INPUT NAME="Clear" TYPE="reset">
</FORM>
</BODY>
</HTML>

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.

<CFQUERY NAME="EmployeeList" DATASOURCE="CompanyDB">
INSERT INTO Leads 
(FirstName, LastName, E-mail, Subject, InquiryText, LeadDate)
VALUES('#preservesinglequotes(Form.FirstName)#',
'#preservesinglequotes(Form.LastName)#',
'#preservesinglequotes(Form.E-mailAddress)#',
'#preservesinglequotes(Form.Subject)#',
'#preservesinglequotes(Form.InquiryText)#', 
	#CreateODBCDate(now())#)
</CFQUERY>

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

Part 1: What is ColdFusion?
Part 2: Loops and Lists

Published Sep. 16, 2003— Reads 5,371
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.

Add Your Feedback

In order to post a comment you need to be registered and logged in.

Register | Sign-in

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:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers

ADS BY GOOGLE

Breaking Java News
West Street Announces Third Quarter Results
Homeland Uranium Inc. Reprices Options
South American Silver Corp. Completes $2.78 Million Financing
Stone Resources Limited Announces Resignation of Directors
L.A.'s West 3rd Street to Launch Public Valet Service Tomorrow
Lorus Therapeutics Raises $2.46 Million in Equity Financing
Cagim Announces Increase in Results for Second Quarter of Fiscal Year 2009
Chai-Na-Ta Corp. Reports 2009 Third Quarter Results
Statement of Condolence-Nelson Leeson, President of the Nisga'a Lisims Government

ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

.NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
Open Web Developer's Journal - OPENWEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2
Apache   |   CMS   |   CRM   |   HP   |   Oracle Journal   |   Perl   |   Python   |   Red Hat   |   Ruby on Rails   |   SAP   |   SaaS

SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
SYS-CON EVENTS:   |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  Cloud Computing Conference & Expo  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
 Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.
 
close this window