|
ColdFusion 9.0 Resources |
Sample uses of the cfmail tagAn 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 form-based e-mailIn 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
from="#Form.EMailAddress#"
to="marketing@MyCompany.com,sales@MyCompany.com"
subject="Customer Inquiry">
A customer inquiry was posted to our website:
Name: #Form.FirstName# #Form.LastName#
Subject: #Form.Subject#
#Form.InquiryText#
</cfmail>
Sending query-based e-mailIn 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
query="ProductRequests"
from="webmaster@MyCompany.com"
to="marketing@MyCompany.com"
subject="Widget status report">
Here is a list of people who have inquired about
MyCompany Widgets during the previous seven days:
<cfoutput>
#ProductRequests.FirstName# #ProductRequests.LastName# (#ProductRequests.Company#) - #ProductRequests.EMailAddress#&##013;
</cfoutput>
Regards,
The webmaster
webmaster@MyCompany.com
</cfmail>
Reviewing the codeThe following table describes the code:
Sending e-mail to multiple recipientsIn 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 recipientsIn 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="beta@MyCompany.com"
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
beta@MyCompany.com
</cfmail>
Customizing e-mail for multiple recipientsIn 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="service@MyCompany.com"
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 this
information has changed, or if we must
get in touch with someone else in your
organization regarding this request.
Thanks,
Customer Service
service@MyCompany.com
</cfmail>
Reviewing the codeThe following table describes the code and its function:
Digitally signing e-mailTo 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. |