send an email using a template - grails

后端 未结 3 791
无人共我
无人共我 2020-12-20 19:40

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:



        
3条回答
  •  感动是毒
    2020-12-20 20:36

    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

提交回复
热议问题