Mail Service plugin issues in a module

I am having a couple issues using the mail service plugin within in a module. I have a security service (/modules/solitary/model/security/SecurityService) and I have a method that looks like this.

public void function sendNotification(required User user){
rc.emailView = ‘notification.forgotPasswordNotification’;

// Create a new mail object
local.email = MailService.newMail().config(
from="danvega@gmail.com",
to=arguments.user.getEmail(),
subject=“Forgot Password Notification”
);

/**

  • Set tokens within the mail object. The tokens will be evaluated
  • by the ColdBox Mail Service. Any matching @key@ within the view
  • will be replaced with the token’s value
    */
    local.email.setBodyTokens({
    firstName = arguments.user.getFirstName(),
    lastName = arguments.user.getLastName(),
    url = “http://localhost/hellocoldbox/security/resetPassword/” & user.getEmailPasswordHash()
    });

// Add HTML email
local.email.addMailPart(charset=‘utf-8’,type=‘text/html’,body=local.Renderer.renderLayout(“layout.email.html”));

// Send the email. MailResult is a boolean.
local.mailResult = mailService.send(local.Email);

}

Question is 2 part.

1.) instead of hard coding the setting for from email I would like to set this up in the module config. If I do I don’t have any idea how to retrieve that. All of the docs say to call getSetting or getModule(‘module’).getSetting but I think thats from the parent app, how do I do it here.
2.) I copied most of this example for sending email from > http://wiki.coldbox.org/wiki/Plugins:MailService.cfm When I run this example I get the following error

Error Messages: Element RENDERER is undefined in a Java object of type class [Ljava.lang.String;.

Am I missing something here? How can render the view after the tokens have been replaced in an email. I tried setting the body=local.email.getBody() but that didn’t work either. I got the email but the body was empty.

You can inject in your service the entire configuration using this:

property name=“configBean” inject=“coldbox:configBean”;

Then you can access your modules like this:

moduleSettings = configBean.getkey(“modules”);

This will have ALL your loaded module settings by name, so if you have a module called solitary

solitarySettings = configBean.getKey(“modules”).solitary;

That gives you all the possible settings for modules including, location, instantiation path, interceptors, etc. Dump it and see.

You mention local.Renderer, but where is that coming from?

I will give that a try…

as far as the renderer I have no clue, I just copied the code from the wiki. How can I just use the view that I have setup in this method? This will have the body with the replaced tokens that i need.

Ahh the dreaded copy/paste issue!!

You must inject the renderer to get rendering capabilities in your model.

property name=“renderer” inject=“coldbox:plugin:Renderer”;

Then you can render layouts, views, layouts of views, views of layouts, views of views, and more :slight_smile:

Dan,

Once you get your code working,feel free to take a look at my PostBox project, which is a alternate mail service which sends mail through the PostMarkApp.com API rather than over SMTP.

They give you some incredible visibility and reporting tools on your emails. We use then on projects here and can’t say enough good things.

Http://PostBox.riaforge.com

Would love to get your opinions on it if you get chance to have a play.

Robert

On a side note can I ask why you are sending the hashed emailPassword in the email? Wouldn’t the userId be better or email itself?

Regards,

Andrew Scott

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

@Andy no reason, just another attempt to prevent url hacking :wink:

@Rawlings - will do, I am only sending some notifications out in this but I will def look at it for my other project.

@Luis - Yes but if you don’t know Coldbox that well (I am still a newb) you don’t know that renderer is a plugin so thats why I was confused.

I personally think that would be a security risk in changing a password too easily.

Regards,

Andrew Scott

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

and passing a plain id isnt?

ok I feel like an idiot now… In the example below there is a view set at the top, I would think this is the view where your tokens are and then setBodyTokens defines what replaces the tokens… but where the heck do you render that actual view into the body.

http://wiki.coldbox.org/wiki/Plugins:MailService.cfm

The renderer plugin is rendering the layout but how on earth does the mail service know what view it should replace the tokens in? I am lost ;(

Yeah I just see it as strange, and was thinking that you were using that to pull up the password and they just enter their new password.

Regards,

Andrew Scott

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

ohhh no no no…I never store plain text passwords so the user gets an email with a link to reset their password.

instead of /security/passwordReset.cfm?id=1234

you get this

/security/passwordReset.cfm/238u7892u3jkasjfsjf2327ru239u80u

No I wasn’t referring to the storing being plain, I mean using the passed password to pull up the details and then not asking for the current password.

Regards,

Andrew Scott

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

If you forgot your password how could enter your current password?

I just find it strange to see a password passed, yes I knew it was hashed.

Regards,

Andrew Scott

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