As part of the ColdFusion 8.0.1 updater, the CFMailParam tag can now attach files to an email using the Content attribute. When using this, the CFMailParam takes a File attribute and the Content attribute. In this case, the File attribute provides the name of the file as it will appear in the mail attachments list, not the path of the file on the server. The content attribute can take any ColdFusion variable that can be converted to binary data. Like the ColdFusion CFContent tag, this content attribute must use binary data; however, unlike the CFContent tag, the CFMailParam tag will automatically convert variables to binary data for you (when possible). Pretty awesome!
Let's take a look at an example. In this demo, I am going to attach a ColdFusion image object as well as a text value:
Launch code in new window » Download code as text file »
Notice that we are passing the objImage variable directly to the CFMailParam tag. My first instinct was that I had to pass the ImageGetBlob( objImage ) value since that would give me the binary image data (which did work as expected), but after some testing, I found out that the CFMailParam tag will try automatically convert the value to binary for me. Of course, this doesn't always work. If you try to attach a ColdFusion query, for example:
Launch code in new window » Download code as text file »
ColdFusion will throw the following exception:
Could not convert the value of type class coldfusion.sql.QueryTable to binary.
As you can see, it is trying to do the binary conversion for you.
Anyway, running the above code resulted in the following GMail email:
| | | | ||
| | ![]() | | ||
| | | |
Notice also that for my signature file, I am not attaching a variable, but rather a trimmed text value. The documentation states that this has to be a ColdFusion variable, but it simply has to be a value that can be converted to binary (which can be an inline text value). Also, I was able to use the CFMailParam and Content tag to create attached files as well as files with an inline disposition (the image displayed within the content of the email itself). This is insanely cool. This, combined with the new Remove attribute is going to make the ColdFusion CFMailParam tag a force to be reconned with! I think these small tweaks are going to have fantastic improvements in the usability of these tags.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
CFDirectory Filtering Uses Single Character Wild Card
I think it is correctly spelled "reckoned," as in "I reckon you right about that."
In the email generation that we have done from Coldfusion server, we have more routinely relied on <img> tags that call back to our server, rather than sending the image content in the actual email. But, I guess that is because we aren't explicitly sending pictures, more just images for look and feel.
While it is sort of outside the range of your exploration of the content attribute, under what circumstances is it appropriate to send content via email and when should links be provided to pull content off a server?
Posted by Phil on May 22, 2008 at 4:49 PM
@Phil,
I am not sure there are any good rules on when to do this. I like the fact that with an embedded image, the images get loaded in the email without prompting the user for permission. This allows me to deliver my brand more effectively (I think).
On the other hand, the attachment does cause problems sometimes. For instance, right now, I am having some mail delivery problems for emails that have embedded images... Adobe is working on a hotfix for this.
Posted by Ben Nadel on May 22, 2008 at 6:10 PM