An application page containing the cfmail tag dynamically generates e-mail messages based on the tag settings. Some of the tasks that you can accomplish with cfmail include the following:
- Sending a mail message in which the data the user enters in an HTML form determine the recipient and contents
- Using a query to send a mail message to a database-driven list of recipients
- Using 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
Sending form-based e-mail
In the following example, the contents of a customer inquiry form submittal are forwarded to the marketing department. You could also use the same application page to insert the customer inquiry into the database. You include the following code on your form so that it executes when users enter their information and submit the form:
<cfmail |
Sending query-based e-mail
In the following example, a query (ProductRequests) retrieves a list of the customers who inquired about a product during the previous seven days. ColdFusion sends the list, with an appropriate header and footer, to the marketing department:
<cfmail |
Reviewing the code
The following table describes the code:
Code |
Description |
|
---|---|---|
|
Presents a dynamic list embedded within a normal message, repeating for each row in the ProductRequests query. Because the cfmail tag specifies a query, the cfoutput tag does not use a query attribute. The &##013; forces a carriage return between output records. |
Sending e-mail to multiple recipients
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 customize each message for the recipient.
Sending a simple message to multiple recipients
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"> |
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"> |
Reviewing the code
The following table describes the code and its function:
Code |
Description |
|
---|---|---|
|
Retrieves all data from the Customers table into a query named GetCustomers. |
|
|
Uses the to attribute of cfmail, the #GetCustomers.Email# query column causes ColdFusion to send one message to the address listed in each row of the query. Therefore, the mail body does not use a cfoutput tag. |
|
|
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. |
Digitally signing e-mail
To add digital signature to your mail, specify the attributes sign, keystore, keystorepassword, keyalias, and keypassword as provided in the following example:
<cfmail from="Sender@Company.com" server="sendmail.myCo.com" sign="true" keystore="C:\OpenSSL\bin\hello.jks" keystorepassword="digital" to="Recepient@Company.com" keyalias="crypto" keypassword="signature" subject="Mail with Digital Signature"> |
To add digital signature to all the mails you send, instead of adding the attributes to the tag, specify the settings in the Server Settings > Settings page of the ColdFusion Administrator.
The supported keystores are JKS and PKCS12.
Due to import control restrictions in various countries, the policy files (local_policy.jar and US_export_policy.jar) support only limited cryptography. If the key strength exceeds the limit, you might encounter the error suggesting that the keystore cannot be loaded. If you are from an eligible country, you can download the unlimited strength version of the policy files and replace the default cryptography JAR files with them. The files are available on the Java SDK web site.