|
Hey,
I have a method in one of my controllers which sends out an email using the standard Mail plugin. This all works fine, but I'm using exactly the same code in another controller so I thought I may as well create a utility class "Emailer" as I'm going to be doing this a few times. In my controller I don't have to reference or import anything special to send out emails, but for some reason sendMail() is not available from within my utility class? Am I overlooking something obvious? |
|
Make sure both controllers and your util are in the same package (or, explicitly import the utility)?
Just a guess. On Wed, Nov 16, 2011 at 8:23 AM, webmediauk <[hidden email]> wrote: Hey, |
|
Thanks Michael,
My controllers are in their own package (same one) and my utility class "Emailer" is in /utils. I can call my utility class method Emailer.sendDownloadEmail() from within my controller and my utility class method works up to the point where it goes to sendMail(). I'm presuming sendMail() is part of the Mail plugin? It's this reference to sendMail() that fails, as if the plugin is not available from my utility class? |
|
In reply to this post by webmediauk
On 16/11/11 13:23, webmediauk wrote:
> Hey, > I have a method in one of my controllers which sends out an email using the > standard Mail plugin. This all works fine, but I'm using exactly the same > code in another controller so I thought I may as well create a utility class > "Emailer" as I'm going to be doing this a few times. > In my controller I don't have to reference or import anything special to > send out emails, but for some reason sendMail() is not available from within > my utility class? > Am I overlooking something obvious? > > > What I'd do is create a mail service class, put your mailing code there and use it from both controllers. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by webmediauk
I think the subtle difference here is that sendMail is injected to
controllers by the plugin. If you are using it in a service or utility class, you have to use mailService.sendMail{ } On Wed, Nov 16, 2011 at 1:23 PM, webmediauk <[hidden email]> wrote: > Hey, > I have a method in one of my controllers which sends out an email using the > standard Mail plugin. This all works fine, but I'm using exactly the same > code in another controller so I thought I may as well create a utility class > "Emailer" as I'm going to be doing this a few times. > In my controller I don't have to reference or import anything special to > send out emails, but for some reason sendMail() is not available from within > my utility class? > Am I overlooking something obvious? > > -- > View this message in context: http://grails.1312388.n4.nabble.com/Grails-Mail-in-utility-classes-tp4076282p4076282.html > Sent from the Grails - user mailing list archive at Nabble.com. > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by John Moore
Thanks John,
Are you saying that you wouldn't make it a utility class but a domain class? |
|
In reply to this post by tomas lin
On 16/11/11 14:08, Tomas Lin wrote:
> I think the subtle difference here is that sendMail is injected to > controllers by the plugin. > > If you are using it in a service or utility class, you have to use > > mailService.sendMail{ > > } > > Sure, but is this a Bad Thing? I tend to use non-transactional service classes as a place to put logic which may be required from a number of different controllers, but is this not a Grails-y way to do things? Of course, another way of sharing logic is to have your controllers extend an abstract BaseController, which may work equally well for the OP. I tend to have a number of utility methods in the BaseController class (such as methods to build a csv string from a list, etc). --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by tomas lin
Thanks Tomas,
I have tried this as per the docs and it doesn't seem to work either? |
|
In reply to this post by webmediauk
On 16/11/2011 14:30, webmediauk wrote:
> Thanks John, > Are you saying that you wouldn't make it a utility class but a domain class? No, the standard Grails-y way to do this would be to make it a service, into which you can autowire the mailService (provided by the mail plugin): class MyCustomMailerService { static transactional = false // autowire the mailService def mailService void sendSomeMessage(...) { mailService.sendMail { //... } } } You can then autowire this service into your controllers etc. class SomeController { def myCustomMailerService def exampleAction = { myCustomMailerService.sendSomeMessage() } } Ian -- Ian Roberts | Department of Computer Science [hidden email] | University of Sheffield, UK --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by webmediauk
On 16/11/11 14:30, webmediauk wrote:
> Thanks John, > Are you saying that you wouldn't make it a utility class but a domain class? > > > I'd make it a service class. Do something like 'grails create-service com.xxx.Mail', create your mail method in that, and then in any controller you want to send mail from you inject the service class with 'def mailService', and call 'mailService.sendMail(whatever)' where you need to send mail. John --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Ian Roberts
On 16/11/11 14:44, Ian Roberts wrote:
> On 16/11/2011 14:30, webmediauk wrote: >> Thanks John, >> Are you saying that you wouldn't make it a utility class but a domain >> class? > > No, the standard Grails-y way to do this would be to make it a > service, into which you can autowire the mailService (provided by the > mail plugin): > > class MyCustomMailerService { > static transactional = false > > // autowire the mailService > def mailService > > void sendSomeMessage(...) { > mailService.sendMail { > //... > } > } > } > > You can then autowire this service into your controllers etc. > > class SomeController { > def myCustomMailerService > > def exampleAction = { > myCustomMailerService.sendSomeMessage() > } > } > > Got that in moments before I did. I had forgotten about the existence of the mailService provided by the plugin, though. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Thanks guys,
I set it up as a service and all is good! |
| Powered by Nabble | Edit this page |
