# Samples uses of CFMAIL

An application page with the CFMAIL tag dynamically generates email messages based on the tag's settings. Some of the things you can accomplish with CFMAIL are:

- Send a mail message whose recipient and contents are determined by data the user enters in an HTML form.
- Use a query to send a mail message to a database-driven list of recipients.
- Use a query to send a customized mail message, such as a billing statement to a list of recipients that is dynamically populated from a database.
- Send a MIME file attachment along with a mail message.

## Sending form-based email

In the example below, the contents of a customer inquiry form submittal are forwarded to the marketing department. Note that the same application page could also insert the customer inquiry into the database.

    <CFMAIL FROM="#Form.EMailAddress#"
        TO="marketing@allaire.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 email

In the example below, a query ("ProductRequests") is run to retrieve a list of the customers who have inquired about a product over the last seven days. This list is then sent, with an appropriate header and footer, to the marketing department:

    <CFMAIL QUERY="ProductRequests"
        FROM="webmaster@allaire.com"
        TO="marketing@allaire.com"
        SUBJECT="ColdFusion status report">

    Here is a list of people who have inquired about
    Allaire ColdFusion over the last seven days:

    <CFOUTPUT>
    #ProductRequests.FirstName# #ProductRequests.LastName#
    (#ProductRequests.Company#) - #ProductRequests.EMailAddress#
    </CFOUTPUT>

    Regards,
    The WebMaster
    webmaster@allaire.com

    </CFMAIL>

Note the use of the nested CFOUTPUT tag to present a dynamic list embedded within a normal CFMAIL message. The text within the CFOUTPUT is repeated for each row in the "ProductRequests" query, while the text above and below it serve as the header and footer (respectively) for the mail message.

## Sending email to multiple recipients

In the following example, a query ("CFBetaTesters") is run to retrieve a list of people who are beta testing ColdFusion. This query is then used to send a notification to each of these testers that a new version of the beta release is available:

    <CFMAIL QUERY="CFBetaTesters"
        FROM="beta@allaire.com"
        TO="#TesterEMail#"
        SUBJECT="ColdFusion Beta Four Available">

    To all ColdFusion beta testers:

    ColdFusion Beta Four is now available
    for downloading from the Allaire site.
    The URL for the download is:

    http://beta.allaire.com

    Regards,
    ColdFusion Technical Support
    beta@allaire.com

    </CFMAIL>

Note that in this example, the contents of the CFMAIL tag are not dynamic, that is, they don't use any # delimited dynamic parameters. What is dynamic is the list of email addresses to which the message is sent. Note the use of the "TesterEMail" column from the "CFBetaTesters" query in the TO attribute.