coldfusioncfmail

Coldfusion CFMAIL & CfMailPart Custom Tags


I am writing a custom tag for CFMAIL. I need to handle certain emails differently then the standard CFMAIL. I am using the following post as a staring point - http://www.cfhero.com/2011/11/creating-environment-safe-wrapper-for.html

Before

<cfmail to="no@example.com" from="no@test.com" subject="This is only a test">
<cfmailparam name="Importance" value="high">
hello world</cfmail>

After Custom Tags

<cf_email to="no@example.com" from="no@test.com" subject="This is only a test">
<cf_emailparam name="Importance" value="high">
hello world</cf_email>

Everything working correctly for the custom CFMAIL and CFMAILPARAM but my question is how to handle "CFMAILPART". When using the existing CFMAILPART within the new tags, CF throws an error.

<cf_email to="no@example.com" from="no@test.com" subject="This is only a test">
<cf_emailparam name="Importance" value="high">
    <cfmailpart type="text">Text</cfmailpart>
    <cfmailpart type="html">HTML</cfmailpart></cf_email>

Detail: The tag must be nested inside a cfmail tag.

Message: Context validation error for tag cfmailpart.

I am striking out with my ideas and I am failing finding any help docs. Any help or starting points would most appreciated.

Thanks in advance.


Solution

  • EDIT: While this will get it to work using custom tags, John Whish's solution using a cfc is more flexible.

    You'll probably need to create a cf_emailpart custom tag for the same reason you created a cf_emailparam tag. cfemailparam must be contained in a cfmail tag

    <cf_email to="no@example.com" from="no@test.com" subject="This is only a test">
        <cf_emailparam name="Importance" value="high">
        <cf_emailpart type="text">Text</cf_emailpart>
        <cf_emailpart type="html">HTML</cf_emailpart>
    </cf_email>
    

    cf_emailpart

    Almost the Same as cf_emailparam. The last part of this associates the tag with cf_email

    <cfif CompareNoCase(thisTag.hasEndtag,"YES") EQ 0 AND CompareNoCase(thisTag.executionMode,"END") EQ 0 >
        <cfexit method="exittag" />
    </cfif>
    
    <cfassociate basetag="cf_email" datacollection="emailPart">
    

    Then you'll need to add this inside your cf_email custom tag file, after the section where you output the cfmailparam tags, and inside the cfmail tag.

    <cfif isDefined("thisTag.emailPart")>
        <cfloop array="#thisTag.emailPart#" index="attrCol">
            <cfmailpart attributecollection="#attrCol#" >
        </cfloop> 
     </cfif>
    

    I haven't tested this code, but I think this is the direction you'll have to go.