Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send an email using a template - grails

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]

like image 744
Illep Avatar asked Jun 28 '14 05:06

Illep


3 Answers

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 protected]"
            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

like image 68
Abincepto Avatar answered Nov 19 '22 16:11

Abincepto


well, you can try this code...

mailService.sendMail {
            to user.email
            from "[email protected]"
            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

like image 6
An Ish A Avatar answered Nov 19 '22 16:11

An Ish A


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.

like image 1
Ekansh Rastogi Avatar answered Nov 19 '22 14:11

Ekansh Rastogi