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" );
}
- Posted in:
- ColdFusion
- Coldbox


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
I've linked to this from within the docs. Appreciate it!
Comment by Aaron Greenlee – September 08, 2010
@Aaron, cool - love what you guys are doing with ColdBox 3 :)
Comment by John Whish – September 09, 2010
<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
Comment by John Whish – September 10, 2010
github.com/wildbit/postmark-coldfusion
Comment by Dave Merrill – October 15, 2010
Comment by John Whish – October 18, 2010
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