For some time now, I have found testing email related functionality to be a bit of a pain. This has become especially true since I started using well-encapsulated CFCs.
For example, I will often need send email to users of the site. Sometimes, I want to test this using actual data, but I don't want to risk the messages to actually send during testing.
Historically, I have changed the code during testing to get around this. This approach has proved to be labor intensive and is a bit risky as well.
I realized recently that since all of my code uses Mailer.cfc to send email now, I should be able to take advantage of this centralization of functionality.
Taking a page out of my DataMgr playbook, I now have a Mailer_Sim.cfc as part of Mailer 1.5 (now in beta). This allows me to have code in my CFC that sends mail using Mailer.cfc. Depending on the instantiation of Mailer.cfc, it may send the email or it may merely log the email to the database instead.
Here is an example in which request.isProduction is used to indicate if this is a production site. This example assumes that I have DataMgr stored in Application.DataMgr (DataMgr is only needed when using the logging feature).
<cfset Application.Mailer = CreateObject("com.sebtools.Mailer").init("mail.example.com","firstname.lastname@example.org")>
<cfset Application.Mailer = CreateObject("com.sebtools.Mailer_Sim").init(MailServer="mail.example.com",From="email@example.com",DataMgr=Application.DataMgr)>
I then pass Mailer.cfc into my components with no concern for whether the site is production or development. On the production site, the email messages sent out via Mailer.cfc will go out normally. In development, however, they will be written to the "mailerLogs" table in your database.
I could also log email that I do send by using the startLogging() method of the main Mailer component, which takes DataMgr as an argument.
Here is some example code to send email:
<cfinvoke component="#Application.Mailer#" method="send">
<cfinvokeargument name="To" value="firstname.lastname@example.org">
<cfinvokeargument name="Subject" value="My Subject">
<cfinvokeargument name="Contents" value="Message Text">
This would send an email to email@example.com unless it was using Mailer_Sim, in which case it would write that email to the database instead.
Mailer.cfc is open source and free for any use. The component docs for 1.5 are also available.