Integrating e-mail capabilities into a Web application is a common requirement. The ubiquity of e-mail makes it an ideal way to correspond with users and customers, as well as to send files in the form of e-mail attachments. In this article, I will show how easy it is to send SMTP e-mail using ColdFusion's <cfmail> tag.
Overview of CFMAIL and what's new in ColdFusion MX 6.1
The <cfmail> tag is not a new ColdFusion tag. It's been part of the CFML language since the earliest versions of the application server. But it has been constantly improved and updated with each new release of ColdFusion. The tag has always allowed you to easily specify the basic parts of an e-mail message: the sender, receiver, title, message content, mail server, and simple attachments. In addition, CF has allowed you to specify database queries to automatically populate the content of multiple e-mails, along with their recipients.
Basically, <cfmail> is used like this:
Under the hood, CFMX 6.1 features a massively improved e-mail engine that is capable of sending over one million e-mails per hour. Thus, ColdFusion can now handle even the most brutal e-mail chores. But the improvements to <cfmail> don't stop at a revamped engine.
CFMX 6.1 introduces several new tags and tag attributes that relate directly to <cfmail>. You can now specify a user name and password to authenticate to an SMTP server. The <cfmail> tag also allows you to set fail-to e-mail addresses that will receive the message if the send attempt fails. You can define character sets, determine whether to spool a copy of the message to disk prior to sending, and set text wrapping values.
Also new is the ability to send multi-part e-mails. So you can now send both HTML and plain-text messages together in the same e-mail. Finally, the new version gives you more control in how you send attachments. Let's take a look at how easy it is to use <cfmail> to send messages, as well as try out some of the new features that CFMX 6.1 brings to the table.
We'll start by building a basic, plain-text e-mail message. Listing A is an example of how this is done.
Simply supply the required attributes (in this case, I'm using variables for things like the SMTP server, recipient, etc.) and execute the page. The user receives a plain-text e-mail, just like that. As you might guess, if the user replies to the e-mail, the message goes to the address specified by the "replyto" attribute. And if the mail system fails to deliver the message, the address specified in the "failto" attribute will receive a failure notification.
What about an HTML-formatted e-mail? It's just as easy to send; it just takes a bit more text in the message body, as shown in Listing B.
The user receives an HTML-formatted e-mail, CSS styles and all. Any HTML you want to put in here is fine: image tags, headers and footers, anything the recipient's mail client is capable of rendering.
We can combine a plain-text and HTML e-mail message together by sending a multi-part e-mail message with the new <cfmailpart> tag, as shown in Listing C.
This way, if the user's mail client can render HTML, then they see the HTML message. But if their client doesn't support HTML, or they've disabled HTML, they'll see the plain-text message.
We can also set custom headers in an e-mail. The example in Listing D would be flagged as "important" by the mail client.
Finally, we can use the <cfmailparam> tag to specify a file to attach to the e-mail, as well as the MIME type of the file, as shown in Listing E.
In this case, we've attached an Adobe Acrobat file to an HTML e-mail. It's that easy.
Ease of use
One of the strongest advantages of ColdFusion is its ease of use, and the <cfmail> tag is no exception. Where other languages can take literally pages of code or the use of third-party components to handle e-mail, CF does the job in just a few lines of code. With CFMX 6.1, the mail capabilities of ColdFusion have hit new peaks.