Creating a Global Email Object - Wanting to pull the environment in the object.

Creating a Global Email Object - Wanting to pull the environment in the object.

My boss requested that I make a global email object that can be extended by emailPasswordChanged(userEmail=“useremail @ something.com”).Send();

  1. Is it wise to access the getSetting(‘environment’) inside of the object?
  2. Can you access it in the object?
  3. Do you have any suggestions on this?
    He wants to make sure if a developer instantiates the object they cannot email the customer in the development environment but it would email a default email address and wrap the message inside of the message sent so we would know it would work in production.

I have been working on a few ideas.

Email.cfc

/***************************************************************************

  • Author : Nathan Stanford
  • Description : Email Object

*************************************************************************/
/

  • email
  • @accessors true
    /
    component email output=“false” hint=“This is email object.” {
    /
    properties */
    property name=“toEmailAddress” type=“string”;
    property name=“ccEmailAddress” type=“string”;
    property name=“bccEmailAddress” type=“string”;
    property name=“fromEmailAddress” type=“string”;
    property name=“subject” type=“string”;
    property name=“body” type=“string”;
    property name=“attachments” type=“array”;

instance = {};

instance.isDevMode = true;
instance.devModeToEmailAddress = “nathan.stanford @ abc.com”;
instance.devModeFromEmailAddress = “nathan.stanford @ abc.com”;

/*
property name=“isDevMode” type=“boolean”;
property name=“devModeToEmailAddress” type=“string”;
property name=“devModeFromEmailAddress” type=“string”;
private isDevMode
private devModeToEmailAddress
private devModeFromEmailAddress
*/

/*********************************

  • Initialize email object
    *********************************/
    function init(required pDevMode, pDevModeToEmailAddress=instance.devModeToEmailAddress, pDevModeFromEmailAddress=‘foo @ foo.com’)
    {
    if(pDevMode){
    setToEmailAddress(pDevModeToEmailAddress);
    setFromEmailAddress(pDevModeFromEmailAddress);
    }

return this;
}

function doSend()
{

params = {};
params.to = “nathan.stanford @ abc.com”;
params.from = “me @ gmail.com”;

params.subject = “Report”;
params.body = “Here is the report you were after”;

new mail(argumentCollection=params).send();
return this;

}

function get()
{

return variables;
}

}

You can certainly get access to your Coldbox settings in your object by injecting them. The disadvantage to this is that the object is then dependent upon the settings you inject. Personally, I like to keep emails settings per environment for testing, etc.

Here is an example of injecting a setting into a model object

property name=“emailSetting” inject=“coldbox:setting:emailSetting”;

And if you want to know your current environment you can just inject that setting.

property name=" environment" inject="coldbox:setting:environment ";

Hope that gets you started in the right direction.

Curt Gratz

Computer Know How

What I have done is created a setting in ColdBox.cfc that is something like this.

settings = {
productionMode = false;
}

Then I do an environment detection, so the name of the production server would be something like this.

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

I then have a plugin that extends the internal system, and in the sendMail I have condition that checks to see if this mode is false and then use another setting to get the developers email.

The beauty about this is that in the environment detection each developer can setup their own machine detection function, and set their emails if needed or have a global or whatever tickles your fancy.

Plugins also don’t need any injection, as they can read the settings from the controller like this.

getSetting(“productionMode”);

Hope that helps.

Btw if it helps I have put together a blog post on this, let me know if anything is not clear and needs better explanation.

http://www.andyscott.id.au/2011/10/3/ColdBox-30-and-sending-emails-whether-in-production-or-development

I have developed something similar to where within the coldbox.cfc
(configuration) you setup your development environment to include an
interceptor that overrides the default emailService

The details are on:

my website:
      http://extra.cfhero.com/coldbox-add-ons/coldbox-environmentsafeemail/coldbox-environmentsafeemail_1_0_1

and coldbox forgebox:
      http://coldbox.org/forgebox/view/ColdBox-Interceptor---Environment-Safe-Mail-Service

but the concept is using the core of coldBox... therefore...

**configure you local development in the configuration**

         function configure() {
          ....
                    environments = {
                    development = "URL HERE"
         };
          .....
         }

**and then include an interceptor to override the functionality of the
mailService**

  function development() {
    var local = StructNew();

    //Register interceptors as an array, we need order
    local.newInterceptors = {class="EnvironmentSafeMailService",
       properties={EmailTrace=true, EmailSend=false,
EmailOverrideAddress="newEmail@address.com"}
      };
    ArrayAppend(interceptors ,local.newInterceptors);

  }

I hope this helps... feel free to take the code and adjust it for your
needs.

Craig Benner

I also have a blog post on this, which I link to how i built it.

thanks,
Craig