<!---
This example uses a table called EmailTemplates with the following columns
`ID` INT(11)
`sender` VARCHAR(255)
`subject` VARCHAR(255)
`htmlbody` BLOB //The html pages are stored in this column
`textbody` BLOB //The text pages are stored in this column
--->


<!--- Set defaults --->
<cfparam name="mailto" default="[Email Address]">
<cfparam name=
"mailID" default="59">
<cfparam name=
"path_to_file" default="C:\path_to_file\">
<cfparam name=
"DSN" default="MYDSN">

<!--- Create a random filename for the html doc--->
<cfset html_file = #RandRange(1000,100000)#&'.cfm'>

<!--- Create a random filename for the text doc--->
<cfset text_file = #RandRange(1000,100000)#&'.cfm'>

<!--- Query the database for the proper email to send, choose your own criteria --->
<cfquery datasource="#MYDSN#" name="getliterature">
    SELECT * FROM EmailTemplates WHERE ID = #mailID#
</cfquery>

<!--- Write the temporary file with the HTML contents of the email body --->
<cffile action="write" nameconflict="makeunique" file="#path_to_file##Variables.html_file#" output="#getliterature.htmlbody#">

<!--- Write the temporary file with the TEXT contents of the email body --->
<cffile action="write" nameconflict="makeunique" file="#path_to_file##Variables.text_file#" output="#getliterature.textbody#">

<!--- Save the variable with the contents of the saved HTML file --->
<cfsavecontent variable="HTMLBody">
    <cfoutput>
        <cfinclude template=
"#Variables.html_file#">
    </cfoutput>
</cfsavecontent>

<!--- Save the variable with the contents of the saved TEXT file --->
<cfsavecontent variable="TextBody">
    <cfoutput>
        <cfinclude template=
"#Variables.text_file#">
    </cfoutput>
</cfsavecontent>

<!--- Send the mail message--->
<cfmail to="#mailto#" from="#getliterature.sender#" subject="#getliterature.subject#" type="html">
    <cfmailpart type=
"html">
       
#HTMLBody#
    </cfmailpart>
    <cfmailpart type=
"text">
       
#TextBody#
    </cfmailpart>
</cfmail>


<!--- Delete the temporary file created earlier --->
<cffile action="delete" file="#path_to_file##Variables.html_file#">
<cffile action=
"delete" file="#path_to_file##Variables.text_file#">
 

About This Tutorial
Author: Derrick Anderson
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX
Total Views: 78,100
Submission Date: March 30, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 2
Discuss This Tutorial
  • Thanks man, this is just what I needed. Not all of it, but a decent chunk I won't have to reinvent!

  • If you are suggesting that i copied this from you, you are mistaken. CF Code is not very complex, many people have written code just like the code tutorials here on easycfm, you should not assume you've been copied when you see similar code to what you may have written in the past, i see it all the time.

  • Nice to see things that I wrote are on the web now under somebody elses name with cfmail added to it ;)

Advertisement

Sponsored By...
iOpenSoft, LLC is a Houston, Texas Advanced Technology Studio Specializing in Web Design, Web Development, iPhone App Development and Android App Development.