• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Step 7. Using Email with ColdFusion MX > Example 7.3: Sending Bulk Email

Example 7.3: Sending Bulk Email

In this example, we are going to use the QUERY attribute of the <CMAIL> tag, along with the <CFMAILPARAM> tag, to send a mass email to all employees to inform them of the company’s new leave policy.

Open your text editor and type the code in Listing 7.4 or open the BulkEmail.cfm file from the CompletedFiles\Examples\Step07 folder.
Listing 7.4. BulkEmail.cfm

File:             BulkEmail.cfm 
Description:      Demos the use of bulk email 
      <TITLE>Bulk Email Example</TITLE> 
      <!--- query the database for employee information ---> 
      <CFQUERY NAME="qStaffMail" DATASOURCE="staff"> 
            SELECT FirstName, LastName, Email 
            FROM         Employees 

      <!--- add your own email address to the CC attribute 
              so you will recv a copy of each email ---> 
      <CFMAIL QUERY="qStaffMail" 
                  SUBJECT="New Leave Policy" 
            <!--- add an attachment from the server's C: drive 
                  if you installed the exercises on another drive 
                  amend the path information---> 
          <CFMAILPARAM FILE="c:\CFMX10Steps\Examples\Step07\docs\LeavePolicy.pdf"> 
Dear #FirstName# #LastName#, 

There has been a recent change to the company's leave policy. Please review the attached document. 
            Your bulk email has been sent to <B>#qStaffMail.recordcount#</B> recipients. 


Save the file as BulkEmail.cfm in your Examples\Step07 folder.

Let’s look at what is happening in this template. First we are querying the Staff database to get the name and email address of each employee. We are calling this query qStaffMail.

Next we use the QUERY attribute of the <CFMAIL> tag so that we can use the information from qStaffMail to build some email. The <CFMAIL> tag will then loop through the query results, using our employees’ email addresses in the TO attribute and their first and last names in the message body. We are also attaching a copy of the new leave policy for each employee to review. Put your own address in the CC attribute so that you can test the template.

If you have installed the exercise files on some drive other than C:, you will need to amend the path information in the template to reflect the correct drive letter.

Browse to the BulkEmail.cfm file to activate the bulk email.

After a few moments, check your email client. An email should have been sent to each employee in the database, and you should have received a copy of each email. Figure 7.9 shows a copy of the email.

Figure 7.9. A copy of the bulk email message.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint