6

I want to send an email using a template. I want to have a GSP file where i could style it, and send the email. Currently the send mail function is as follows:

def sendEmail(){

    mailService.sendMail {
        to "email","**email**"
        from "email"
        subject "Hi"
        body 'Hi'
    }
}

in my config.groovy file

grails {
    mail {
      host = "smtp.gmail.com"
      port = 465
      username = "email"
      password = "pwd"
      props = ["mail.smtp.auth":"true",
               "mail.smtp.socketFactory.port":"465",
               "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
               "mail.smtp.socketFactory.fallback":"false"]
    }
 }

I went through another Stack Overflow post on this: Where should i add the mail templates ? is it in the views folder ?

sendMail{
    multipart true
    to "[hidden email]"
    subject "Subject goes here"
    html  g.render( template: '/emails/mailTemplate')
    inline 'springsourceInlineImage', 'image/jpg', new File('./web-app/images/springsource.png')
}

UPDATE

I TREID ADDING A mailTemplate.gsp UNDER EMAILS/ BUT IT DIDNT WORK.

ERROR I GOT Template not found for name [/emails/mailTemplate] and path [/emails/_mailTemplate.gsp]

Community
  • 1
  • 1
Illep
  • 16,375
  • 46
  • 171
  • 302

3 Answers3

11

You can use groovyPageRenderer.render() to parse your email. Below, an example:

class MailingService {

    def groovyPageRenderer
    def mailService

    def yourFunction(User user) {

        def content = groovyPageRenderer.render(view: '/mails/myTemplate')
        mailService.sendMail {
            to user.email
            from "email@test.com"
            subject "MySubject"
            html(content)
        }
    }
}

In this case, the template is here: /views/mails/MyTemplateFile.gsp

Hope this helps.

Edit: And the render could be used with a model. Example:

groovyPageRenderer.render(view:'/mails/myTemplate',model:[user:user])

Edit2: I forgot to add the mailService in my first reply

Abincepto
  • 1,016
  • 8
  • 13
  • 1
    i get this error "Cannot invoke method render() on null object. Stacktrace follows: Message: Cannot invoke method render() on null object" – Illep Jun 28 '14 at 13:15
  • Did you add the second line of my answer "def groovyPageRenderer" ? – Abincepto Jun 28 '14 at 14:56
  • 1
    I have included that line. But still i get the error `Cannot invoke method render() on null object` – Illep Jun 28 '14 at 16:31
  • May be you need to add this import: "import grails.gsp.PageRenderer" and define the renderer as "PageRenderer groovyPageRenderer". I tried the code using Grails 2.3.7 and it works. I hope your code will work with these modifications. – Abincepto Jun 28 '14 at 16:52
  • Please Note. I have added the code in the `Services` section. – Illep Jun 28 '14 at 17:13
  • My code also lives in a service. GroovyPageRenderer is in the documentation and the example is about mail rendering (http://grails.org/doc/2.2.4/guide/single.html#webFeatures). I will try later using 2.2.4 – Abincepto Jun 28 '14 at 17:23
6

well, you can try this code...

mailService.sendMail {
            to user.email
            from "email@test.com"
            subject "MySubject"
            body(view:'/emails/mailTemplate', model: [a:A])
        }

here mailTemplate.gsp is in view/emails. In body of mail service you can use render syntax. then add '<%@ page contentType="text/html" %>' in top of mailTemplate.gsp

An Ish A
  • 711
  • 5
  • 18
  • Throws exception `Exception occurred in job: Grails Job org.quartz.JobExecutionException: java.lang.IllegalArgumentException: Mail views cannot be loaded from relative view paths where there is no current HTTP request [See nested exception: java.lang.IllegalArgumentException: Mail views cannot be loaded from relative view paths where there is no current HTTP request]` – biniam Dec 16 '15 at 14:51
1

Well looking at your code, everything looks good enough.

html g.render(template : '/path/to/template')

should render your template and it will become the body of your mail message.

Have you made sure that you made your template as _template. Since all the gsp's that start with (_) are only considered as a template.

You should also make all the styling(css) inline so that it gets rendered without errors in all mail providers.

Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23