Hi All,
A client wanted a CFC to handle emails. One of the primary objectives of their current development is flexibility. So, they wanted to be able to specify (or not) as many attributes as possible.
This is not a problem until you get to the point of sending the email. You can't put conditionals inside a tag that say "if this variable has a value, use the tag with this attribute."
There are 20 optional parameters to cfmail in MX61, which means a whole lot of different variations of the tag. Writing all those options are not practical, so how do you approach it?
This is what I did:
First any optional attribute that could be given a default, was:
ReplyTo and FailTo were set to be identical to the "from" value; type was set to HTML; Port was set to 25 timeout was set to 30 MailerID was set to ColdFusion MX Application Server CharSet was set to UTF-8 SpoolEnable was set to no
I left out the query related attributes, since the cfc was only being designed to send a single email.
That left 7 remaining optional attributes. I made the assumption that username would never be used without password (and vice versa), which left 6 options:
cc, bcc, wraptext mimeattach username / password server
The next step was to write a function. To allow for all the optional parameters, I first created a string (Email Attributes) like this:
<Cfif instance.server is not "">
<Cfset EmailAttributes = EmailAttributes & "1">
</Cfif>
<Cfif instance.username is not "">
<Cfset EmailAttributes = EmailAttributes & "2">
</Cfif>
etc.. etc..Then I entered into a switch statement on the value of EmailAttributes.
If it was equal to 1 (Just Server), the cfmail tag looked like this:
<cfmail to="#instance.Personto#" from="#instance.PersonFrom#" subject="#Instance.Subject#" replyto="#instance.replyto#" type="#instance.type#"
port="#instance.port#" timeout="#instance.timeout#" mailerID ="#instance.mailerID#" charset = "#instance.charset#"
spoolenable="#Instance.spoolenable#" failto="#instance.failto#" server="#instance.server#">
</cfmail>
If it was 12 it looked like this (server, username, and password):
<cfmail to="#instance.Personto#" from="#instance.PersonFrom#" subject="#Instance.Subject#" replyto="#instance.replyto#" type="#instance.type#"
port="#instance.port#" timeout="#instance.timeout#" mailerID ="#instance.mailerID#" charset = "#instance.charset#"
spoolenable="#Instance.spoolenable#" failto="#instance.failto#" server="#instance.server#" username="#instance.username#" password="#instance.password#">
</cfmail>
etc.. etc..
I ended up with 46 different variations, although it is "hidden" from the user of the CFC. For efficiency sake, I also provided a simple email function which sends an email with default parameters.
Am I missing something obvious or is there an easier way to approach this?
--
Jeffry Houser, Web Developer <mailto:[EMAIL PROTECTED]>
Aaron Skye, Guitarist / Songwriter <mailto:[EMAIL PROTECTED]>
--
AIM: Reboog711 | Phone: 1-203-379-0773
--
My Books: <http://www.instantcoldfusion.com>
Recording Music: <http://www.fcfstudios.com>
Original Energetic Acoustic Rock: <http://www.farcryfly.com>
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]
