Sample uses of cfmail

An application page containing the cfmail tag dynamically generates e-mail messages based on the tag's settings. Some of the tasks that you can accomplish with cfmail include the following:

Sending form-based e-mail

In the following example, the contents of a customer inquiry form submittal are forwarded to the marketing department. You could also use the same application page to insert the customer inquiry into the database. You include the following code on your form so that it executes when users enter their information and submit the form:

<cfmail
  from="#Form.EMailAddress#"
  to="marketing@MyCompany.com,sales@MyCompany.com"
  subject="Customer Inquiry">

A customer inquiry was posted to our web site:

Name: #Form.FirstName# #Form.LastName#
Subject: #Form.Subject#

#Form.InquiryText#
</cfmail>

Sending query-based e-mail

In the following example, a query (ProductRequests) retrieves a list of the customers who inquired about a product during the previous seven days. The list is then sent, with an appropriate header and footer, to the marketing department:

<cfmail
  query="ProductRequests"
  from="webmaster@MyCompany.com"
  to="marketing@MyCompany.com"
  subject="Widget status report">

Here is a list of people who have inquired about
MyCompany Widgets during the previous seven days:

<cfoutput>
#ProductRequests.FirstName# #ProductRequests.LastName# (#ProductRequests.Company#) - #ProductRequests.EMailAddress#&##013;
</cfoutput>

Regards,
The WebMaster
webmaster@MyCompany.com

</cfmail>

Reviewing the code

The following table describes the code:
Code
Description
<cfoutput>
#ProductRequests.FirstName#
#ProductRequests.LastName#
(#ProductRequests.Company#) -
#ProductRequests.EMailAddress#&##013;
</cfoutput>
Presents a dynamic list embedded within a normal cfmail message, repeating for each row in the ProductRequests query. The &##013; forces a carriage return between output records.

Sending e-mail to multiple recipients

In addition to simply using a comma-delimited list in the to attribute of the cfmail tag, you can send e-mail to multiple recipients by using the query attribute of the cfmail tag.

In the following example, a query (BetaTesters) retrieves a list of people who are beta testing ColdFusion. This query then notifies each beta tester that a new release is available. The contents of the cfmail tag body are not dynamic. What is dynamic is the list of e-mail addresses to which the message is sent. Using the variable #TesterEMail#, which refers to the TesterEmail column in the Betas table, in the to attribute enables the dynamic list:

<cfquery name="BetaTesters" datasource="myDSN">
  SELECT * FROM BETAS
</cfquery>

<cfmail query="BetaTesters"
  from="beta@MyCompany.com"
  to="#BetaTesters.TesterEMail#"
  subject="Widget Beta Four Available">

To all Widget beta testers:

Widget Beta Four is now available 
for downloading from the MyCompany site.
The URL for the download is:

http://beta.mycompany.com

Regards,
Widget Technical Support
beta@MyCompany.com

</cfmail>

Comments