emailcoldfusioncoldfusion-9model-glue

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


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

<cfmail from="abc@123.com" to="def@456.com" 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("abc@123.com");
mail.setTo("def@456.com");
mail.setSubject("New e-mail!");
mail.setBody( ** SOME CFM FILE ** );
mail.send();

Does anybody have any idea how I can do this?


Solution

  • 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>