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. The following examples show how you can send the same message to multiple recipients and how you can a customize each message for the recipient.
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="[email protected]" 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 [email protected] </cfmail>
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="[email protected]" 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 [email protected] </cfmail>
The following table describes the code and its function:
Code | Description |
---|---|
<cfquery name="GetCustomers" |
Retrieves all data from the Customers table into a query named GetCustomers. |
<cfmail query="GetCustomers" from="[email protected]" to="#GetCustomers.EMail#" subject="Contact Info |
Uses the |
Dear #GetCustomers.FirstName# ... Company Name: #GetCustomers.Company# Contact: #GetCustomers.FirstName# |
Uses other query columns ( |