send an email using a template - grails

后端 未结 3 792
无人共我
无人共我 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:14

    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

    0 讨论(0)
  • 2020-12-20 20:20

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题