Aliaspooryorik
ColdFusion ORM Book

ColdBox 3 MailService

I was working on a ColdBox 3 site and noticed that the wiki page for the Mailservice is quite sparse, so I thought I'd blog a simple example of it in use


/**
* I am a simple ColdBox handler
**/

component
{
// get ColdBox to inject the MailService plugin for us
property name="MailService" inject="coldbox:plugin:MailService";

void function send( required event )
{
var rc = arguments.event.getCollection();

var Email = variables.MailService.newMail();

Email.setTo( "some@email.fake" );
Email.setFrom( "My Client<another@email.fake>" );
Email.setCC( "copycat@email.fake" );
Email.setType( "html" );
Email.setSubject( "Hello!" );
Email.setBody( "<p>Test email from ColdBox MailService</p>" );

variables.MailService.send( Email );

// set thanks message
rc.message = "Thank you for your comment";

// set the view to show
arguments.event.setView( "thanks" );

}
}

Nice and neat, but what if I wanted to have different email addresses for development and production? Well, just set them up in the ColdBox.cfc config.


/**
* I am the configuration for this ColdBox application
**/

component
{
// Configure ColdBox Application
void function configure()
{
// coldbox directives
coldbox = {
...normal stuff here...
};

environments = {
// The dev environment is used whenever the cgi.http_host is localhost
dev = "^localhost$,^127.0.0.1$"
};

// custom settings
settings = {
// email
email = {
to = "some@email.fake",
from = "My Client<another@email.fake>",
cc = "copycat@email.fake",
type = "html"
}
}
}

// settings if running from "localhost"
void function dev()
{
// override email settings
settings.email.to = "me@testing.foo";
settings.email.cc = "me@testing.foo";
}
}

I need to update my handler to use these settings:


/**
* I am a simple ColdBox handler
**/

component
{
// get ColdBox to inject the MailService plugin for us
property name="MailService" inject="coldbox:plugin:MailService";

void function send( required event )
{
var rc = arguments.event.getCollection();

var Email = variables.MailService.newMail();

// use settings from Coldbox.cfc
Email.config( argumentCollection=getSetting( "email" ) );
Email.setSubject( "Hello!" );
Email.setBody( "<p>Test email from ColdBox MailService</p>" );

variables.MailService.send( Email );

// set thanks message
rc.message = "Thank you for your comment";

// set the view to show
arguments.event.setView( "thanks" );

}
}

Neat. Now we don't have to worry about spamming our client when testing. However, we are getting the email set twice when doing any dev work as we have a cc and a to address set. Now that the ColdBox config is created using cfml (instead of XML) we can fix this really easily by changing the dev environment settings in Coldbox.cfc to:


void function dev()
{
// override email settings
settings.email.to = "me@testing.foo";
// don't want a cc copy when testing
StructDelete( settings.email, "cc" );
}

 


8 comments

  1. This is cool stuff John.

    I've been writing almost identical services for MG apps that we run here and it works really well.

    I'm currently reworking the service to move away from standard SMTP and start sending emails through a PostmarkApp account as it makes tracing mail traffic and the bounces quite nice.

    postmarkapp.com/

    They work on a RESTful interface that you hit instead of sending over SMTP.

    Once I've had chance to work things through properly I may be interested in building an extension for the ColdBox mailer service which allows you to utilize this send method.

    Rob

    Comment by Robert – September 08, 2010
  2. Thanks, John.

    I've linked to this from within the docs. Appreciate it!

    Comment by Aaron Greenlee – September 08, 2010
  3. @Robert, PostmarkApp looks interesting, it wouldn't be hard to build your own ColdBox plugin to do this which extends the coldbox.system.core.mail.MailService.

    @Aaron, cool - love what you guys are doing with ColdBox 3 :)

    Comment by John Whish – September 09, 2010
  4. One other very cool feature about the mailService plugin is the use of tokens:

    <cfset tokens.password ='#myUser.getpassword()#'>
    <cfset emailObject.setBodyTokens(tokens)>

    Then you can just use @password@ in your mailbody.

    Comment by Erik-Jan – September 09, 2010
  5. @Erik, yeah, that is cool, maybe I should do a follow up post on some of the other things you can do (like attachments and tokens etc)

    Comment by John Whish – September 10, 2010
  6. FYI, there's already a Postmark CFC, here:
    github.com/wildbit/postmark-coldfusion

    Comment by Dave Merrill – October 15, 2010
  7. @Dave, thanks for the link, I didn't know about that.

    Comment by John Whish – October 18, 2010
  8. HI John,

    I've finally gotten round to writing a mail service which integrates with the PostMark service. Take a look if it's of any interest to you.

    postbox.riaforge.org/ />
    Thanks,

    Robert

    Comment by
    Robert – February 02, 2011

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts