It used to be in pre-ColdFusion MX 7 (CFMX 7), to embed images in an email, to set them up so that they do not show up as attachments or external source links, I needed to use a special CFMail custom tag. Just the other day, on the House of Fusion mailing list, there was a thread about embedding images in emails.
I did not know this, but apparently in ColdFusion MX 7, the CFMailParam tag can be used to embed images directly into the email content. After some initial testing, it seems this is completely true and very exciting! It is almost too easy.
I tried it with two different methodologies. One, embeds a file from the local file system. The other embeds an image from a live Url. Both seemed to work find with Microsoft Outlook and GMail. The live Url link did not work in Hotmail, but was available as a downloadable attachment. While I could not test it, I was told that the test for live Url embedding did not work in Thunderbird.
Here is the sample code:
Launch code in new window » Download code as text file »
Download Code Snippet ZIP File
Comments (9) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Fantastic tip. If was the first one I found when googling. I am building a crm system and need to send graphics.
Here is what I have added to it.
1. I loop through the email content looking for src ( I know there won't be anything else in there with src but <img> as it's my content.
2. Add each image URL to a list
3. Loop through the list and replace the url in the mailer content with the cid: +filename (without extension)
4. Then at the bottom of the email loop through the list again and do the <cfmailparam> inside the loop.
Worked first time. Thank you!!!
Posted by Simon on Nov 28, 2007 at 1:01 PM
@Simon,
Sounds like a cool idea. I like it a lot. I recently got into CFPop for pulling down emails and I've been told you have to do the opposite on render of the email body (taking all the CIDs and swapping them back into img SRC attributes to point to files on the server or something). Nothing to do with what you are saying, but at the other end of the same spectrum.
Posted by Ben Nadel on Nov 28, 2007 at 2:37 PM
Could this be adapted to embed css into the email as well? So far I've had no luck.
Posted by John H. on Apr 3, 2008 at 4:51 PM
@John,
I don't think so. When you use CSS in a web page, you are not referring to the file itself, but rather to formatting rules that are loaded in the file. I don't think this loads files, but rather provides inline references to them.
Posted by Ben Nadel on Apr 4, 2008 at 7:49 AM
Yeah. I settled on including the css in a style tag in the header. I was experimenting with styling emails that will be viewed in Lotus Notes.
They'll get just the plain markup while the rest will be able to view the styled email.
I did discover that Lotus Notes will not display .png files. A simple conversion to .gif and presto!
Posted by John H. on Apr 4, 2008 at 10:48 AM
I attempted to use this tag to embed an image, it only sends as an attachment and I am using CF7 .. Strange.
<cffunction name="send" access="public" returntype="void" output="false">
<cfargument name="emailNotobj" required="true" type="bmg.model.beans.emailnotification" />
<cfset var r = variables.reactor.createRecord("distroList").load(distroId=arguments.emailNotObj.getdList()) />
<cfset var eList = "" />
<cfset var emails = r.getDistroEmailIterator().getQuery() />
<cfset var b = getPreview(arguments.emailNotObj) />
<cfloop query="emails">
<cfset eList = eList & emails.email & ',' />
</cfloop>
<cfmail to="#eList#" from="BMGBase@awsmail.att.com" type="html" subject="#arguments.emailNotObj.getSubject()#">
<div>
<cfmailparam file="#ExpandPath('views/images/mailheader.jpg')#" contentid="emailHeader" disposition="inline" />
</div>
#b#
</cfmail>
</cffunction>
Posted by Thomas Kreutzer on May 7, 2008 at 7:31 AM
@Thomas,
To embed it, I am pretty sure you need to give it a contentid value and then reference via an IMG tag within the email where the src is "cid:YOUR_VALUE".
Posted by Ben Nadel on May 7, 2008 at 7:37 AM
two seconds after i post my question i realize there is an image tag with cid:idname in it. I guess I assumed the param tag was going to just fit it in automagically for me. :(
Posted by Thomas Kreutzer on May 7, 2008 at 7:38 AM
@Thomas,
No problem :)
Posted by Ben Nadel on May 7, 2008 at 7:39 AM