Example 7.3: Sending Bulk Email

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 7.  Using Email with ColdFusion MX


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.

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

    [View full width]

     <!--- File:             BulkEmail.cfm  Description:      Demos the use of bulk email  Author:  Created:  --->  <HTML>    <HEAD>        <TITLE>Bulk Email Example</TITLE>    </HEAD>    <BODY>        <!--- query the database for employee information --->        <CFQUERY NAME="qStaffMail" DATASOURCE="staff">              SELECT FirstName, LastName, Email              FROM         Employees        </CFQUERY>        <!--- add your own email address to the CC attribute                so you will recv a copy of each email --->        <CFMAIL QUERY="qStaffMail"                    TO="#Email#"                    CC="your.emailaddress.here"                    FROM="admin@work.com"                    SUBJECT="New Leave Policy"                    SERVER="your.mailserver.here">              <!--- 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  graphics/ccc.gifdocument.        </CFMAIL>        <CFOUTPUT>              Your bulk email has been sent to <B>#qStaffMail.recordcount#</B> recipients.        </CFOUTPUT>    </BODY>  </HTML>

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

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

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

    graphics/07fig09.gif


    Team-Fly    
    Top
     



    ColdFusion MX. From Static to Dynamic in 10 Steps
    ColdFusion MX: From Static to Dynamic in 10 Steps
    ISBN: 0735712964
    EAN: 2147483647
    Year: 2002
    Pages: 140
    Authors: Barry Moore

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net