Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
681 views
in Technique[技术] by (71.8m points)

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]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...