Keep your CFML in a database, then CFMAIL it!

<!---
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#">
 



All ColdFusion Tutorials By Author: Derrick Anderson
  • Boost your applications performance with cached CFCs
    When using the cfinvoke tag or the CreateObject function on your pages, it adds CPU time to process the requests to load all of those objects into memory. Under load this can make quite a difference in your page response times. One way to get around it is to put those objects into a shared scope so they only get instantiated once. This will save memory and CPU time on your server.
    Author: Derrick Anderson
    Views: 8,072
    Posted Date: Thursday, August 31, 2006
  • Keep your CFML in a database, then CFMAIL it!
    I came up with this solution after building an email marketing application with content management built in. I found that if I had CF code in my html pieces that were stored in the database, it would not get processed by the server after the contents of the field holding the HTML and CF were used as the body of an email.
    Author: Derrick Anderson
    Views: 8,141
    Posted Date: Tuesday, March 30, 2004