Ask a Jedi: Emailing CFCHART

This post is more than 2 years old.

Abhishek asks:

I've a doubt, what i'm doing is that i'm generating chart using cfchart inside cfsavecontent and then i want to send the entire data of the cfsavecontent in mail using cfmail. Mail is working fine but unable to get the chart in the mail instead of that there comes red x image and in view source it is showing a path somewhat like this /CFID/GraphData.cfm?graphCache=wc5. Please give some solution.

Well, don't forget that, by default, when you use cfhcart you are going to get object/embed tags pointing back to your server to load a dynamic Flash SWF file. I'd be surprised if any mail client would render Flash client in context.

You could switch to PNG of course. If you use HTML email you could then embed that image in one of two ways. One way would be to save the PNG to your web server. Your HTML email would then simply point to your web server. If the email is viewed offline then the image won't load, but that's probably not a huge concern.

The other option is to the cfmailparam tag and an inline image. I had never used this before but the docs made it fairly simple. Here is a complete example and then I'll go over the salient points:

<cfchart format="png" name="chart"> <cfchartseries type="pie"> <cfchartdata item="1Q Sales" value="500" /> <cfchartdata item="2Q Sales" value="400" /> <cfchartdata item="3Q Sales" value="700" /> <cfchartdata item="4Q Sales" value="200" /> </cfchartseries> </cfchart>

<cfset savedFile = getTempFile(getTempDirectory(),"foremail") & ".png"> <cfset fileWrite(savedFile,chart)>

<cfoutput>#savedFile#</cfoutput> <cfmail to="ray@camdenfamily.com" from="ray@camdenfamily.com" subject="Sales" type="html"> <cfmailparam contentID="img1" file="#savedFile#" disposition="inline">

<h2>Sales Figures</h2> <p> Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. </p>

<img src="cid:img1" />

</cfmail>

The chart (in this case a hard coded chart) is stored into a variable using the name attribute. I save the file in the temp directory. The cfmailparam tag points to the file and provides an ID that the HTML can refer to later. Notice the image tag uses cid: instead of http:

I did test this just to make sure it actually worked, and it did (which always tends to surprise me!):

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Abhishek posted on 1/15/2009 at 2:08 PM

Thank you Raymond, solution given by you worked for me. Its been a great help.
Thanks so much..

Comment 2 by Francois Levesque posted on 1/15/2009 at 7:36 PM

Hi Ray,

I've used this solution in the past also for serving up cfcharts via AJAX. That way, I only need to send the link to the image file, rather than a savedcontent of the generated markup. I can then modify the src of the img tag and the image reloads seamlessly.

Comment 3 by larry c. lyons posted on 1/15/2009 at 7:52 PM

thanks Ray, this is very useful (working on something similar) but I've a dumb question, I take it in
<cfset fileWrite(savedFile,chart)>
fileWrite is a UDF?

Comment 4 by Raymond Camden posted on 1/15/2009 at 7:54 PM

@Larry - thats a CF8 function.

Comment 5 by larry c. lyons posted on 1/15/2009 at 8:03 PM

Strange it never showed up in cfquickdocs. needless to say d'oh

Comment 6 by RobW posted on 1/16/2009 at 5:37 AM

Another good reason to use cfmailparam tag and an inline image is that, increasingly mail clients won't display images that are linked from a website in order to cut down on spam. So your recipient gets a big blank spot in the e-mail and your e-mail format gets ruined.

Comment 7 by Steve posted on 1/16/2009 at 1:00 PM

This is rad even for a man of the dark side. I will definitely be testing this tomorrow.

Comment 8 by Ray Meade posted on 1/5/2010 at 11:22 PM

I tried this and for some reason the image doesn't show up on the web page. (although it does show up in the email...go figure) When I right-click it and select "View Image" I get an error that says:

"Firefox doesn't know how to open this address, because the protocol (cid) isn't associated with any program."

Any suggestions?

Comment 9 by Raymond Camden posted on 1/5/2010 at 11:29 PM

Because the cid is valid in email only, not the web. If you want the image to show up on the web, you need to use img src with the http protocol.

Comment 10 by Sandi S posted on 2/16/2015 at 8:11 PM

Thanks Ray, still helping people all these years later!

Comment 11 (In reply to #10) by Raymond Camden posted on 2/16/2015 at 8:23 PM

Glad it was still useful for you.