ColdBox 3.0+ and sending emails whether in production or development.

As this question comes up a lot, I thought I would blog about this some more. ColdBox is brilliant when it comes to doing things by convention, and minimising our work, and emails is no different. However one of the main problems that comes up a lot is how to send emails to developers, rather than to the real recipient of the email while in development.

The first thing we need to do is setup a setting in our ColdBox.cfc that will determine what mode we are in, as a guide I have done the following.

settings = {
   productionMode = false;
}

The other thing that we need to do is to then setup for our detected environment, this is also again very easy to do in ColdBox and we would do it something like this. Which will get the name of the server, so it can be called as a function

string function detectEnvironment() {
   return replace(createObject( 'java', 'java.net.InetAddress' ).getLocalHost().getHostName(),"-","","all" );
}

And the function in ColdBox.cfc will look like this.

void function productionServerName() {
   settings.productionMode = true;
}

Once we have done this the next thing we need to do is create a plugin that extends the mailService plugin, and place this into our applications plugins folder. We will then create the following function/method

<cffunction name="send" access="public" returntype="struct" output="false" hint="Send an email payload. Returns a struct: [error:boolean,errorArray:array]">
   <cfargument name="mail" required="true" type="any" hint="The mail payload to send." colddoc:generic="coldbox.system.core.mail.Mail"/>
   <cfscript>
      // Proxy in the send call and monitor it.
    if(!getSetting("productionMode")) {
         setTo( getSetting("developerEmail") );
    }
      var results = super.send(argumentCollection=arguments);
      return results;
   </cfscript>
</cffunction>

And that is pretty much it.. What this does is that when it goes to send the email, it first gets the current setting and it the productionMode is set to false, then we are running in development mode, and we then get the email address that the email is to then be sent to instead.


ColdBox 3.0+ and sending emails whether in production or development. - http://t.co/NVaXwo7P Oct 3, 2011
ColdBox 3.0+ and sending emails whether in production or development. http://t.co/YLbMhrnX Oct 3, 2011

  • Craig Benner's Gravatar I agree with a lot of what you said, but instead of creating a new mail service cfc, i replaced the current plugin using the interceptor logic.

    Here is a blog post:
    http://www.cfhero.com/2011/10/coldbox-environment-...

    and the interceptor
    http://extra.cfhero.com/coldbox-add-ons/coldbox-en...
    # Posted By Craig Benner | 10/4/11 8:04 AM
  • Andrew Scott's Gravatar I think using the interceptor to replace the plugin is way overkill, but I think using an interceptor to maybe change the to field is more feasible.

    I can think of a few examples where changing the plugin would break your code.
    # Posted By Andrew Scott | 10/4/11 8:26 AM
  • Craig Benner's Gravatar You've intrigued my interests of what example would break the code, when what the interceptor is doing the same thing you are doing except in the original plugin?
    # Posted By Craig Benner | 10/4/11 9:04 AM
  • Daniel's Gravatar why not use the coldbox environment seeting already there ;-)


    environments = {
          development = "^local.",
          stage = "^stage.",
          production ="^www"
          };

    than create the functions to set the setting for the coreesposnding environment

    function development(){
          
          settings.dsn = 'mydb-stagedev'';   
          }
       function stage(){
          settings.dsn = 'mydb-stage';   
       };
       function production(){
    settings.dsn = 'mydb-prod';   
    };
    # Posted By Daniel | 10/25/11 7:16 AM
  • Andrew Scott's Gravatar I am not 100% sure that will work for me, it might for you. But the main reason is that each developer machine could use their own email address, with that method you only ever get to use the one email address.

    So with the detect environment, you can then have different settings for each machine it is running on, hence each developer can configure to their own testing needs.
    # Posted By Andrew Scott | 10/25/11 12:05 PM