ColdFusion Mail (cfmail)

You can use ColdFusion's <cfmail> tag to dynamically send emails to one or more recipients. You might, for example, offer a subscription based email newsletter, or you might like to send users a welcome email when they register on your site.

You can build the email dynamically using CFML, so you can personalise things like the message body, the email format (HTML or text) etc

Send Email using ColdFusion

This example assumes that a user has just submitted a form, where they were required to fill out their first name and email address.

The action page contains the following code (well, realistically you would have more than this on an action page, but let's keep it simple). When this code runs, an email will be sent to the recipient. The recipient's email address and first name have been provided as form variables.

Send Email to Multiple Recipients

If you have a mailing list, chances are your users' details (email address, first name etc) are in a database. Don't panic - look how easy it is to get these details using ColdFusion!

The <cfmail> tag allows you to loop over the results of a database query. All you need to do is provide the query name using the query attribute.

Sending Attachments

The <cfmail> tag includes the mimeattach attribute. This attribute allows you to specify an attachment, such as a PDF or Word file, to be sent with the email.

How does ColdFusion send email?

First of all, you need to have a SMTP (Simple Mail Transfer Protocol) mail server available for your use. You tell ColdFusion which server to use by adding it to your <cfmail> tag, or by specifying it in the ColdFusion Administrator. In the above example, because we didn't specify a mail server, ColdFusion will use the mail server as specified in the ColdFusion Administrator (assuming that has been done).

When this template is executed, ColdFusion will generate a file with a .cfmail extension and place it in the ColdFusion spool folder. ColdFusion polls the spool folder at regular intervals (as specified in the ColdFusion Administrator) to see if any mail messages are queued to be sent. If there are, ColdFusion passes the mail file to the mail server. If the mail server is unavailable (or none has been specified) ColdFusion moves the mail file to the UnDelivr folder.

ColdFusion's spool folder and UnDelivr folders are located in the \{cfusion_directory}\Mail folder (where {cfusion_directory} is your ColdFusion installation directory), or if you are using the J2EE implementation of ColdFusion, the default is the C:\JRun4\servers\{cfusion}\cfusion-ear\cfusion-war\WEB-INF\cfusion\Mail\Undelivr (where {cfusion} is the name of the ColdFusion instance).

Via the ColdFusion Adminstrator, you can configure log files to log mail status and errors. These can be useful for troubleshooting any issues you might encounter from time to time. You can also specify the frequency you'd like ColdFusion to poll the spool directory, and if you have the Enterprise version of ColdFusion, you can specify how many threads ColdFusion should use to send mail. This can improve performance enormously, however, you need to make sure the mail server can handle it!