Customizing e-mail for multiple recipients

In the following example, a query (GetCustomers) retrieves the contact information for a list of customers. The query then sends an e-mail to each customer to verify that the contact information is still valid:

<cfquery name="GetCustomers" datasource="myDSN">
  SELECT * FROM Customers
</cfquery>

<cfmail query="GetCustomers"
  from="service@MyCompany.com"
  to="#GetCustomers.EMail#"
  subject="Contact Info Verification">

Dear #GetCustomers.FirstName# -

We'd like to verify that our customer
database has the most up-to-date contact
information for your firm. Our current
information is as follows:

Company Name: #GetCustomers.Company#
Contact: #GetCustomers.FirstName# #GetCustomers.LastName#

Address:
  #GetCustomers.Address1#
  #GetCustomers.Address2#
  #GetCustomers.City#, #GetCustomers.State# #GetCustomers.Zip#

Phone: #GetCustomers.Phone#
Fax: #GetCustomers.Fax#
Home Page: #GetCustomers.HomePageURL#

Please let us know if any of the above
information has changed, or if we need to
get in touch with someone else in your
organization regarding this request.

Thanks,
Customer Service
service@MyCompany.com

</cfmail>

Reviewing the code

The following table describes the code and its function:
Code
Description
<cfquery name="GetCustomers"
datasource="myDSN">
  SELECT * FROM Customers
</cfquery>
Retrieves all data from the Customers table into a query named GetCustomers.
<cfmail query="GetCustomers"
  from="service@MyCompany.com"
  to="#GetCustomers.EMail#"
  subject="Contact Info
Verification">
Uses the to attribute of cfmail, the #GetCustomers.Email# query column causes one message to be sent to the address listed in each row of the query. Therefore, the mail body does not use a cfoutput tag.
Dear #GetCustomers.FirstName# 
...
Company Name: #GetCustomers.Company#
Contact: #GetCustomers.FirstName#
#GetCustomers.LastName#

Address:
  #GetCustomers.Address1#
  #GetCustomers.Address2#
  #GetCustomers.City#,
#GetCustomers.State#
#GetCustomers.Zip#

Phone: #GetCustomers.Phone#
Fax: #GetCustomers.Fax#
Home Page: #GetCustomers.HomePageURL#
Uses other query columns (#GetCustomers.FirstName#, #GetCustomers.LastName#, and so on) within the cfmail section to customize the contents of the message for each recipient.

Comments