8

When emails are rendered from templates, the templates are looked up in "grails_app/views":

mailService.sendMail {
    from sender
    to recepient.email
    subject "Don't forget"
    body  (view: "/emails/reminder",
        model:[recepient: recepient, document: document])
}

How can I put the mail templates outside of the application (war file) into the file system?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
deamon
  • 89,107
  • 111
  • 320
  • 448

1 Answers1

8

There are two options:

  1. Store your templates as text in the database, and generate the default crud code to update them. You would then modify the email code to pull the data out of the database using domain objects.
  2. Store the templates as strings in a Groovy config file. Groovy can use external configuration files that can be edited separately from the packaged application. See this blog postfor details.

Once you have your string, you can use the GSP engine from a Grails controller with any arbitrary string to create a view. See this blog post for more details.

In short, you would store your template as a string using one of the listed options and then use the Grails GSP engine to create the view once you retrieved the template string.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Jared
  • 39,513
  • 29
  • 110
  • 145
  • 1
    But how do I render a GSP string (within a service)? – deamon Jan 19 '11 at 15:58
  • @deamon: I think the answer of Jared is well enough. But here's the idea that should be made clear: the input for "body" in sendMail function is a simple string. You only need to pass the string (maybe with HTML decoration) into "body". Notice that if you want to send HTML file, you must turn on other relevant options. – Hoàng Long Jan 20 '11 at 02:19