I have three CFCs:
1. MailAccountManager.cfc - EmailServer Bean, manages email settings:
server info, username/password, etc
2. MailManager.cfc - this is the Email Bean, manages the actual email,
has setters for the email's FROM, TO, SUBJECT, BODY, TYPE (html or
text), etc, and has the sendMail() method which does the actual
<CFMAIL>. This bean is dependent on MailAccountManager.cfc since I
need the email settings to do the <CFMAIL>
3. MailService.cfc - this is the service cfc, and is dependent on
MailManager.cfc. I have two init functions. One does init that
basically does nothing. The second init is "initMail()" and this
initializes the mail parameters in MailManager.cfc. I have the first
init() since CS chokes if I use initMail() as the init(). I don't
know if I did that correctly. I have two methods in MailService.cfc
that sends email: mail() and mailTo(). Mail() will just send email
with whatever previous settings, but the email body is different which
is why it's required. MailTo() needs a to, from, body and subject
each and everytime. Here is the code:
<cfcomponent output="false">
<cffunction name="init" output="false">
<cfargument name="from" required="false" />
<cfargument name="to" required="false" />
<cfargument name="cc" required="false" />
<cfargument name="bcc" required="false" />
<cfargument name="type" required="false" />
<cfargument name="subject" required="false" />
<cfset VARIABLES.instance = {} />
<cfset VARIABLES.instance = ARGUMENTS />
<cfreturn this />
</cffunction>
<cffunction name="initMail" output="false">
<cfargument name="from" required="false" default="" />
<cfargument name="to" required="false" default="" />
<cfargument name="cc" required="false" default="" />
<cfargument name="bcc" required="false" default="" />
<cfargument name="type" required="false" default="" />
<cfargument name="subject" required="false" default="" />
<cfset VARIABLES.instance.from = ARGUMENTS.from />
<cfset
VARIABLES.instance.to = ARGUMENTS.to />
<cfset
VARIABLES.instance.cc = ARGUMENTS.cc />
<cfset VARIABLES.instance.bcc = ARGUMENTS.bcc />
<cfset VARIABLES.instance.type = ARGUMENTS.type />
<cfset VARIABLES.instance.subject = ARGUMENTS.subject />
<cfset getMailManager().setTo(ARGUMENTS.To) />
<cfset getMailManager().setFrom(ARGUMENTS.From) />
<cfset getMailManager().setCC(ARGUMENTS.CC) />
<cfset getMailManager().setBCC(ARGUMENTS.BCC) />
<cfset getMailManager().setType(ARGUMENTS.Type) />
<cfset getMailManager().setSubject(ARGUMENTS.Subject) />
</cffunction>
<cffunction name="mailTo" output="false">
<cfargument name="from" required="true" />
<cfargument name="to" required="true" />
<cfargument name="cc" default="" />
<cfargument name="bcc" default="" />
<cfargument name="type" default="html"/>
<cfargument name="subject" required="true" />
<cfargument name="body" required="true" />
<cfset getMailManager().mailTo( from="#ARGUMENTS.from#",
to="#ARGUMENTS.to#",
cc="#ARGUMENTS.cc#",
bcc="#ARGUMENTS.bcc#",
type="#ARGUMENTS.type#",
subject="#ARGUMENTS.subject#",
body="#ARGUMENTS.body#") />
</cffunction>
<cffunction name="mail" output="false">
<cfargument name="body" required="true" />
<cfset getMailManager().mail(body="#ARGUMENTS.body#") />
</cffunction>
<cffunction name="setMailManager" output="false">
<cfset VARIABLES.instance.MailManager = ARGUMENTS[1] />
</cffunction>
<cffunction name="getMailManager" output="false">
<cfreturn VARIABLES.instance.MailManager />
</cffunction>
<cffunction name="getVariables" output="false">
<cfreturn VARIABLES.instance />
</cffunction>
</cfcomponent>
1st question: For MailService.cfc, is that optimal, having two inits?
What would you recommend I would have done?
2nd question: I have all three CFCs as singletons in the application
scope. If I need to change the mail settings in
MailAccountManager.cfc, i.e. if I am sending from a different email
account, I do a:
MailService.getMailManager().getMailAccountManager().setUsername
("new_username")
MailService.getMailManager().getMailAccountManager().setMailServer
("new_mailserver")
and so on with whatever settings to be able to send email from that
server. Is this ok? It works, but I was wondering how else this
would have been architected/executed properly?
3rd question: I was thinking that since MailAccountManager.cfc has
the settings I have, then whenever I need to change email settings, I
can just do a createObject() for it, then set it in
MailServer.getMailManager().setMailAccountManager() with this new
object. This would make my two singletons MailServer and MailManager
dependent on a transient object. How would I set that up in my
coldspring.xml? Here is my current coldspring.xml:
<bean id="MailAccountManager"
class="com.prank.mail.MailAccountManager">
<constructor-arg name="server">
<value>${server}</value>
</constructor-arg>
<constructor-arg name="port">
<value>${port}</value>
</constructor-arg>
<constructor-arg name="username">
<value>${username}</value>
</constructor-arg>
<constructor-arg name="password">
<value>${password}</value>
</constructor-arg>
<constructor-arg name="useSSL">
<value>${useSSL}</value>
</constructor-arg>
<constructor-arg name="useTLS">
<value>${useTLS}</value>
</constructor-arg>
</bean>
<bean id="MailManager" class="com.prank.mail.MailManager">
<property name="MailAccountManager">
<ref bean="MailAccountManager" />
</property>
</bean>
<bean id="MailService" class="com.prank.mail.MailService">
<property name="MailManager">
<ref bean="MailManager" />
</property>
</bean>