home Links Articles Books Past Meetings Photos SiteMap
The MDCFUG is sponsored by TeraTech. Visit us at www.TeraTech.com

Please send
comments/questions to

michael@
teratech.com

 

CPCUG Monitor

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>, <CFIF> and <CFLOOP>.  ColdFusion was introduced by Allaire in 1996 and is currently on version 4.0

 

CFMAIL

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:

 

<HTML>

<BODY>

Request more information.<BR>

<FORM ACTION="RequestInfoSub.cfm" METHOD="POST">

Name: <INPUT NAME="FirstName" TYPE="text">

     <INPUT NAME="LastName" TYPE="text"><BR>

Email: <INPUT NAME="Email" TYPE="text"><BR>

Subject: <INPUT NAME="Subject" TYPE="text">

Details: <TEXTAREA NAME="InquiryText"></TEXTAREA>

<INPUT NAME="sumbit" TYPE="submit">

<INPUT NAME="Clear" TYPE="reset">

</FORM>

</BODY>

</HTML>

 

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.

 

<CFMAIL FROM="#Form.EMailAddress#"

TO="[email protected]"

CC="#Form.EMailAddress#"

SUBJECT="Customer Inquiry">

The following inquiry was posted to our Web site:

     

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, Email, Subject, InquiryText, LeadDate) VALUES(“#Form.FirstName#”,”#Form.LastName#, “#Form.EmailAddress#”, “#Form.Subject#”, “#Form.InquiryText#”, #CreateODBCDate(now())#)

</CFQUERY>

 

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:

 

<CFQUERY NAME="GetEmail" DATASOURCE="CompanyDB">

SELECT FirstName, LastName, Email

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 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 <CFMAIL>:

 

<CFSET ListSent = ''>

<CFLOOP QUERY="GetEmail">

<!--- for testing uncomment the next line --->

     <!--- cfset ThisEmail="[email protected]"--->

     <CFSET ThisEmail=email>

<CFMAIL TO="#ThisEmail#"

FROM="<[email protected]> TeraTech"

SUBJECT="Programming News"

SERVER="smtp.mycompany.com">

Dear #FirstName#,

Here are this month's programming tips

</cfmail>

 

     <CFSET ListSent = ListSent & " #FirstName# #LastName#

mailto:#ThisEmail# " & CHR(13) &CHR(10) & CHR(13) &CHR(10)>

</CFLOOP>

 

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:

 

<CFMAIL TO="[email protected]"

FROM="[email protected]"

SUBJECT="Auto emails"

SERVER="smtp.mycompany.com">

Sending followup email on #dateformat(now())#

 

Name  Email

#listsent#

</cfmail>

 

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 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 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)

<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 email to one or more people and can be used with a query for mail merge programs.

 

Text Box:  Testing and Debugging your Application 
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 Cold Fusion 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.
What else can I do with ColdFusion?
Future articles will cover how ColdFusion can:
·	Run any SQL query including INSERT, UPDATE and DELETE queries
·	Send customized email with CFMAIL
·	Loop over queries, list or do For Next loops
·	Handle errors and relocate to different pages
Automatically read pages from other websites using CFHTTP
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:

 

Allaire Corporation

1 Alewife Center

Cambridge, MA 02140

 

Tel: 617.761.2000 voice

Fax: 617.761.2001 fax

Toll Free: 888.939.2545

Email: [email protected]

Web: www.allaire.com

 

CPCUG Discount

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

ColdFusion Resources

Allaire also maintain an extensive knowledge basis and tech support forums on their website.

CPCUG and TeraTech ColdFusion Conference http://www.teratech.com/cfconf/

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 Info Center.

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 directions.

Bio

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 protected] or 301-424-3903.


Home | Links | Articles | Past Meetings | Meeting Photos | Site Map
About MDCFUG | Join | Mailing List |Forums | Directions |Suggestions | Quotes | Newbie Tips
TOP

Copyright © 1997-2024, Maryland Cold Fusion User Group. All rights reserved.
< >