We help IT Professionals succeed at work.

Insert image from SQL database into email using CFMAIL

Hello experts!
Please help.
I  need to send an automatic email from the cf page and this email is populated from database.
Seems easy, however i have trouble to insert image into the  body of the email because it is stored as image type in the SQL 2005 database.
Here is a code.
<cfquery name=qName datasource=#dsn#>
SELECT     EMAIL_id,  SUBJECT, BODY, TO_WHOM, CC_WHOM, BCC_WHOM,  IS_FROM
FROM         EMAILCUSTOM
WHERE  EMAIL_ID=#url.bid#
</cfquery>
<cfmail
subject="#qName.subject#"
 from="#qName.is_from#"
 to="#qName.to_whom#"  >

<!---here is an image #qName.body#--->

This is an automatic message. Thank you.
</cfmail>
How  can i put an image into email and not on the page? (i tried <cfcontent type="image/gif" variable="#qName.body#"> and image displayed on the page)

thank you


Comment
Watch Question

Galina BesselyanovaSenior Software Developer/Engineer

Author

Commented:
What is a path for my image if it stored in db?
hmm .. I never store images in a db, (only links to images)  and my first thought was you could write it out to a temp file

however I found this http://www.forta.com/blog/index.cfm?mode=entry&entry=A61BC976-3048-80A9-EF7AE4D7D8F602B9

which makes it easier...and should eliminate the need for cfcontent

just create a getImage.cfm using whatever query structure you need and then

add

<img src="/getImage.cfm?imageid=123">

 to your mail body

not tested (no images in a db ;) but should work

Galina BesselyanovaSenior Software Developer/Engineer

Author

Commented:
thanks
So there is no way i can do this on one page?
Most Valuable Expert 2015

Commented:
If you're running 8.01+, try using the "content" attribute. You should be able to use it for both attachments and inline images.  

This entry is about pdf's, but the same concept should apply to any type of attachment/inline file
http://www.coldfusionjedi.com/index.cfm/2009/1/22/Ask-a-Jedi-Sending-a-dynamic-PDF-via-email


<cfmail ...>
     <cfmailparam file="someGIFImage.GIF"
         contentID="someUniqueStringIdentifyingImage1"
         content="#qName.body#"
         type="inline">

     <img src="cid:someUniqueStringIdentifyingImage1" />
      This is an automatic message. Thank you.
</cfmail>
Most Valuable Expert 2015

Commented:
Correction: That should be disposition, not type.  

<!--- must use type="HTML" for inline images --->
<cfmail type="html" ....>
     <cfmailparam file="someGIFImage.GIF"
         contentID="someUniqueStringIdentifyingImage1"
         content="#qName.body#"
         disposition="inline" >

     <img src="cid:someUniqueStringIdentifyingImage1" />
      This is an automatic message. Thank you.
</cfmail>
Galina BesselyanovaSenior Software Developer/Engineer

Author

Commented:
sorry,
but i don't have physical file. I have image type data in the table in SQL database.
 So, what is a someGIFImage.gif in your code?
Most Valuable Expert 2015
Commented:
It's doesn't represent a physical file.  It's just a header or name given to the email client so it can identify attachments and inline content for the user.  It's just like if you were using <cfheader> and <cfcontent> to display your image. Even though there's no physical file, you can add a header to tell the browser what file name/ext to display to the user.

ie  (not tested)
<cfheader name="Content-Disposition" value="inline; filename=whateverNameYouWant.gif">
<cfcontent type="image/gif" variable="#qName.body#">