CFMAIL and Dynamic Message Content

Standard

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.

4 thoughts on “CFMAIL and Dynamic Message Content

  1. Will Sansbury

    Just out of curiosity, if you encode the @s in the e-mail addresses in the first example as %40, does it work?

  2. URLEncodedFormat which converts all of the spaces and special characters… and it choked on that as well. No matter what I did to try and do something with CFINCLUDE, it wouldn’t work. I’m glad the CFHTTP method ended up working for me, because I was seriously about to dropkick my machine if it didn’t.

  3. Will Sansbury

    Just out of curiosity, if you encode the @s in the e-mail addresses in the first example as %40, does it work?

  4. It would not. I didn’t mention but I did try to use a ColdFusion function called URLEncodedFormat which converts all of the spaces and special characters… and it choked on that as well. No matter what I did to try and do something with CFINCLUDE, it wouldn’t work. I’m glad the CFHTTP method ended up working for me, because I was seriously about to dropkick my machine if it didn’t.

Comments are closed.