使用模板发送电子邮件 - grails [英] send an email using a template - grails

查看:19
本文介绍了使用模板发送电子邮件 - grails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用模板发送电子邮件.我想要一个 GSP 文件,我可以在其中设置样式并发送电子邮件.目前发送邮件功能如下:

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'
    }
}

在我的 config.groovy 文件中

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')
}

更新

我尝试在 EMAILS/ 下添加一个 mailTemplate.gsp,但它没有用.

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

错误我找不到名称 [/emails/mailTemplate] 和路径 [/emails/_mailTemplate.gsp] 的模板

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

推荐答案

您可以使用 groovyPageRenderer.render() 来解析您的电子邮件.下面是一个例子:

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)
        }
    }
}

在这种情况下,模板在这里:/views/mails/MyTemplateFile.gsp

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

希望这会有所帮助.

并且渲染可以与模型一起使用.示例:

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

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

编辑 2:我第一次回复时忘记添加mailService了

I forgot to add the mailService in my first reply

这篇关于使用模板发送电子邮件 - grails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆