将电子邮件模板作为Scala模板在Play中使用? [英] Email templates as scala templates in Play?

查看:134
本文介绍了将电子邮件模板作为Scala模板在Play中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Play 1.2.4中.您可以使用标准模板机制和语法(链接),虽然我没用过,但看起来确实很酷. Play2.0的邮件程序插件能够做到这一点吗?

In Play 1.2.4. you could send complex, dynamic e-mail using the standard templates mechanism and syntax (link), this looks realy cooll although I haven't used it. Is the mailer plugin of Play2.0 capable of such things?

推荐答案

如果通过复杂的动态电子邮件"表示基于模板的HTML电子邮件正文,则可以在Play 2.0中执行相同的操作.

If by "complex, dynamic e-mail" you mean HTML email body based on template, you can do the same with Play 2.0.

您只需要基于模板创建一个新视图,例如mailBody.scala.html:

You just have to create a new view based on a template, for instance mailBody.scala.html:

@(user:User)

<h3>Welcome @user.name</h3>
<br/>
....

然后,在发送电子邮件的方法中,您只需调用视图的render()方法:

Then, in your method which sends an email, you just have to call the render() method of your view:

public static void sendMail(User user) {

   MailerAPI mail = play.Play.application().plugin(MailerPlugin.class).email();
   mail.setSubject(...);
   mail.addRecipient(user.email);
   mail.addFrom(...);

   String body = views.html.mailBody.render(user).body();
   mail.sendHtml(body);

}

这篇关于将电子邮件模板作为Scala模板在Play中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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