How do I send a cfm file as the body of an e-mail using ColdFusion?

862 Views Asked by At

I have a legacy application where an email.cfm file is used with a cfmail tag to send e-mail:

<cfmail from="[email protected]" to="[email protected]" subject="New e-mail!">
    // lots of HTML
</cfmail>

Now I'd like to update it for ColdFusion Model Glue 3. I want to send it using a mail object in the controller, and include in the body a CFM page:

var mail = new mail();
mail.setFrom("[email protected]");
mail.setTo("[email protected]");
mail.setSubject("New e-mail!");
mail.setBody( ** SOME CFM FILE ** );
mail.send();

Does anybody have any idea how I can do this?

4

There are 4 best solutions below

0
Daniel T. On BEST ANSWER

I ended up following Henry's advice in the comments and created a CFML-based CFC:

<cfcomponent>

    <cffunction name="SendMail">
        <cfargument name="from"/>
        <cfargument name="to"/>
        <cfargument name="subject"/>

        <cfmail from="#from#" to="#to#" subject="#subject#">
            <!--- HTML for e-mail body here --->
        </cfmail>
    </cffunction>

</cfcomponent>

Dave Long's suggestion is also good, which is to create components using <cfcomponent>, then wrapping the code in <cfscript> tags. This gives you the ability to fall back to CFML in case the there is no cfscript equivalent or it's easier to do with CFML:

<cfcomponent>
    <cfscript>
        void function GetData()
        {
            RunDbQuery();
        }
    </cfscript>

    <cffunction name="RunDbQuery">
        <cfquery name="data">
            SELECT * FROM ABC;
        </cfquery>
        <cfreturn data>
    </cffunction>

</cfcomponent>
9
Michael C. O'Connor On

You can render the content you want to email in a cfsavecontent block and then use that in the email, like:

<cfsavecontent variable="myemail">
...add some HTML, include another file, whatever...
</cfsavecontent> 
<cfscript>
mail.setBody( myemail );
</cfscript>

See http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d57.html

0
Shaun McCran On

Call the CFC assigning it to a variable, like cfset request.emaiBody = cfc.function(). Then just put it in your setBody tag.

0
KamasamaK On

OP was convinced to use CFML, but to answer the question as it was initially asked:

var mail = new Mail();
mail.setFrom("[email protected]");
mail.setTo("[email protected]");
mail.setSubject("New e-mail!");
mail.setType("html");
savecontent variable="mailBody" {
  include "email.cfm";
}
mail.setBody(mailBody);
mail.send();