CB 3.0 MailService Plugin & Tokens

Hi guys,

Today I took my first crack at using the CB 3.0 MailService plugin. First off, this plugin is awesome! I was able to get it up and running without a hitch. (more or less) I’ve implemented a custom interceptor to send an email to a user after an contact entry is logged to the database. announceInterception(‘onContactCreate’) fires successfully and hits the following method:

/**

  • @interceptionPoint
    */
    function onContactCreate(event,interceptData){
    var rc = event.getCollection();
    log.info("--------onContactCreate-----------");

rc.emailView = “emails/contactRequest”;
rc.emailBody = getPlugin(“Renderer”).renderLayout(“Layout.Email”);
// Get new mail object
rc.mail = mailService.newMail().config(from=getSetting(‘email’).from,
replyto=getSetting(‘email’).replyto,
to="#rc.user_email#",
type=“html”,
subject=“Welcome #rc.user_name#!”,
server=getSetting(‘email’).server,
port=getSetting(‘email’).port,
username=getSetting(‘email’).username,
password=getSetting(‘email’).password,
priority=“3”);

// create some tokens
tokens = {contact_name=rc.firstname,user_firstname=rc.user_firstname,name=rc.user_name};
rc.mail.setBodyTokens(tokens);

// Set some body text
rc.mail.setBody(rc.emailBody);

// Send the mail
results = mailService.send(rc.mail);
}

Rather than set the body of the message within the function I store the body of the email in an external view “emails/contactRequest”. This body loads successfully and the email is deployed however the tokens do not populate.

Layout.Email looks like this:

#renderView(view=rc.emailView)#

which renders contactRequest
contactRequest.cfm looks like this:

Dear

@contact_name@

You're friend @user_name@ has sent you an invite!

Okay, that email content is made up :), but it does contain the tokens that i’m trying to populate. When I receive the email the tokens display as is: @contact_name@. I ran a quick test and placed the content for setBody() within the above function as illustrated here: http://wiki.coldbox.org/wiki/WhatsNew:3.0.0.cfm#New_MailService_Plugin and was able to get the tokens to populate. It’s only when the I use an external view as the body of the email that the tokens don’t populate. Has anyone had success in populating tokens using this approach?

Thanks very much.

Nolan

Can I ask you a question here, although interceptors are great I am curious why you would design and use one in a once of solution?

Just curious and looking for feedback and understanding.

Regards,

Andrew Scott

http://www.andyscott.id.au/

Hi Andrew,

I'm open to feedback on best practices or other approaches if this is an incorrect use. I'm new to interceptor design and was simply following the examples that Luis illustrated within his book. I'm sure there are several ways to do this. i.e in my saveContact method of my UserService I could simply call another service method which deploys the email. I see interceptors as elegant way to perform certain functions. In my handler after the new contact is added I can simply call announceInterception('onContactCreate',data); and the interceptor will fire. I could certainly achieve the same result by running this within a service.

Feedback is appreciated if you think i'm going in the wrong direction on this.

Thanks.

Nolan

Well this is why I asked because I am open to other people’s way of doing things as well. And it is not that you are going in the wrong direction, my thinking is interceptors are better suited for something that would get more use rather than a once off.

Maybe someone with a bit more knowledge can blog about creating and using custom interceptors.

But if you feel comfortable with it keep going, as I said I am more curious as to why rather than to say you shouldn’t.

Regards,

Andrew Scott

http://www.andyscott.id.au/

I did a blog post on custom interceptors a while back. I don’t think it’ll tell you guys anything you don’t already know, but I did outline my use case for them. http://www.dougboude.com/blog/1/2008/12/Coldbox-Interceptors-and-Custom-Interception-Points-Demystified.cfm

Fantastic, Thanks Doug.

Just so it doesn’t get lost in the dialogue…does anyone have any thoughts on my issues with the MailService tokens.

Cheers.

Nolan

Actually not really Nolan, because the parser doesn’t care about the body content, it just does @__@ replacement on it, no matter what it is. There might be something else going on.

Can you further investigate

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com