Email templates as scala templates in Play?

后端 未结 1 1188
时光说笑
时光说笑 2021-02-04 08:51

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

1条回答
  •  执念已碎
    2021-02-04 09:33

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

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

    @(user:User)
    
    

    Welcome @user.name


    ....

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

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