Creating an environment safe wrapper for cfmail
The one thing that always makes me nervous when testing other people's code, is who is this going to email? Well many years back I created a cf_email custom tag that wrapped cfmail with some special if logic before it to determine if it was in production or different environment. The logic for is easy
comparenoCase(server.serverLevel,"PRODUCTION") EQ 0
and there was another script that got included in the application.cfm file "server.cfm" that loaded the server variables.
But the issue I ran into lately, was I needed to use one of the newer features within cfmail that wasn't hard coded into my wrapper. So knowing that coldfusion added the support to attributeCollection=attributes and seeing the Ray Camden had a nice blog post on CFASSOCIATE. I asked one of my co-workers to work on it :-D But anyways, we came up with the below code.
The below code allows the same features within coldfusion to be used for within the customTag, but now we can add additional logic to verify environment and only send the program's email in production, and tweak the TO,CC,BCC fields if not within production.
Test Code
cf_emailParam
comparenoCase(server.serverLevel,"PRODUCTION") EQ 0
and there was another script that got included in the application.cfm file "server.cfm" that loaded the server variables.
But the issue I ran into lately, was I needed to use one of the newer features within cfmail that wasn't hard coded into my wrapper. So knowing that coldfusion added the support to attributeCollection=attributes and seeing the Ray Camden had a nice blog post on CFASSOCIATE. I asked one of my co-workers to work on it :-D But anyways, we came up with the below code.
The below code allows the same features within coldfusion to be used for within the customTag, but now we can add additional logic to verify environment and only send the program's email in production, and tweak the TO,CC,BCC fields if not within production.
Test Code
<cf_email to="endUser@cfhero.com" from="help@cfhero.com" subject="This is only a test">
<cf_emailparam file="#expandPath("./web.jpg")#" disposition="inline" contentID="image1">
There should be an image here <img src="cid:image1" />
</cf_email>
cf_email<cfif CompareNoCase(thisTag.hasEndtag,"NO") EQ 0>
<cfthrow message="cf_email requires end tag" />
</cfif>
<cfparam name="attributes.from">
<cfparam name="attributes.to">
<cfparam name="attributes.subject">
<cfif CompareNoCase(thisTag.executionMode,"END") EQ 0 >
<!--- add customization here for non-production use --->
<!--- blank the generated content out and move it into a variable --->
<cfset variables.GeneratedContent = thisTag.GeneratedContent>
<cfset thisTag.generatedContent = "">
<cfmail attributecollection="#attributes#" from="#attributes.from#" subject="#attributes.subject#" to="#attributes.to#">
<cfif isDefined("thisTag.emailParam")>
<cfloop array="#thisTag.emailParam#" index="attrCol">
<cfmailparam attributecollection="#attrCol#" >
</cfloop>
</cfif>
#variables.GeneratedContent#
</cfmail>
</cfif>
cf_emailParam
<cfif CompareNoCase(thisTag.hasEndtag,"YES") EQ 0 AND CompareNoCase(thisTag.executionMode,"END") EQ 0 > <cfexit method="exittag" /> </cfif> <cfassociate basetag="cf_email" datacollection="emailParam">
Comments
Post a Comment