CFMAIL and Dynamic Message Content

Pardon me while I put on my geek hat. I’ve been hitting my head up against a wall trying to figure out the best way to dynamically include content into an email using ColdFusion.

I tried the most logical solution first, define the URL with the data I wanted and pass that onto the SEND script.

 <input type="hidden" name="msgString" value="http://example.com/msg/test.cfm?subject=Hi&sender=joe@example.com&recip=john@example.com" />

Then, I converted the string to a variable I could more easily work with…

<cfset msgStr = "#form.messageString#" />
<cfmail to="user@example.com"
 from="user@example.com"
 subject="Hey there!"
 server="mail.example.com"
 type="html">
  <cfinclude url="#msgStr#" />
</cfmail>

No luck. ColdFusion choked on the dynamic string everytime. When I tried to define the string as a variable and parse it with CFOUTPUT, ColdFusion choked again.

So, I thought, why not try using CFHTTP to grab the data, and then have it render into the body of the email I wanted to send.

<cfhttp resolveurl="yes" url="http://www.example.com/#form.messageString#" method="get"></cfhttp>
<cfmail to="user@example.com"
 from="user@example.com"
 subject="Hey there!"
 server="mail.example.com"
 type="html">
  #CFHTTP.FileContent#
</cfmail>

BINGO! That worked. In retrospect, it seems pretty easy to do it this way… but I couldn’t get it to work right for quite some time. Now it works right, and the client should be happy.