ColdFusion CFMailParam's New "Content" Attribute Is Awesome

Posted May 7, 2008 at 9:23 AM

Tags: ColdFusion

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 »

  • <!---
  • Grab an image from flickr.com and create a ColdFusion image
  • object via the URL.
  • --->
  • <cfset objImage = ImageNew(
  • "http://farm3.static.flickr.com/2304/2335117561_513d9f8a76_b.jpg"
  • ) />
  •  
  • <!--- Resize the image so that it's not too big. --->
  • <cfset ImageScaleToFit( objImage, 300, 300 ) />
  •  
  • <!--- Add a border. --->
  • <cfset ImageAddBorder( objImage, 2, "##333333" ) />
  •  
  •  
  • <!--- Create a signature variable. --->
  • <cfsavecontent variable="strSignature">
  • Ben Nadel
  • Kinky Solutions
  • www.kinkysolutions.com / www.bennadel.com
  • </cfsavecontent>
  •  
  •  
  •  
  • <!--- Send out email. --->
  • <cfmail
  • to="xxx@yyyyyyy.com"
  • from="yyy@zzzzz.com"
  • subject="Is that your wife???"
  • type="html">
  •  
  • Dude, I was doing some research for a business project
  • and I came across the attached file. Call me crazy,
  • but is that your wife making out with another chick??<br />
  • <br />
  •  
  • You better lock that down!<br />
  • <br />
  •  
  • <img src="cid:kissing" /><br />
  • <br />
  •  
  • Cheers,<br />
  • Ben
  •  
  • <!---
  • Attach file for inline usage. Use the contents of the
  • image object for the value of the CFMailParam. Unlike
  • the Content attribute in CFContent, we do not need to
  • use a binary variable here.
  • --->
  • <cfmailparam
  • file="wife_kissing_girl.jpg"
  • contentid="kissing"
  • content="#objImage#"
  • disposition="inline"
  • />
  •  
  • <!--- Attach signature text. --->
  • <cfmailparam
  • file="signature.txt"
  • content="#Trim( strSignature )#"
  • />
  •  
  • </cfmail>

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 »

  • <!--- Add query to email. --->
  • <cfmailparam
  • file="query.txt"
  • content="#QueryNew( 'id, name, value' )#"
  • />

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:


 
 
 

 
CFMailParam Uses Content Attribute To Attach Email Attachements - Image And Text  
 
 
 

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 (0)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting