在 Meteor 电子邮件中使用动态 HTML 模板 [英] Using dynamic HTML templates in Meteor emails

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

问题描述

有没有办法将 Meteor 模板呈现为电子邮件的 HTML 正文?

Is there a way to render a Meteor template as the HTML body of an email?

例如,如果我想在该电子邮件中显示集合数据或生成动态链接.

For example if I want to show collection data or generate dynamic links inside that email.

推荐答案

是的,这是可能的,这里我提供了一个客户端解决方案来解决这个常见问题.

Yes this is possible, here I provide a client-side solution to this common problem.

首先,您应该定义一个简单的模板,作为您的电子邮件 html 正文:

First you should define a simple template that will serve as your email html body :

<template name="shareEmailContent">
  <p>{{message}}</p>
  <a href="{{url}}">{{title}}</a>
</template>

然后您可以使用 Email.send(参见 Email.send at docs.meteor.com,您需要一些适当的配置,例如添加电子邮件智能包并设置 MAIL_URL) 以通过电子邮件发送模板渲染的结果.Email.send 仅适用于服务器,因此您必须定义一个可从客户端调用的服务器方法.

Then you can use Email.send (see Email.send at docs.meteor.com, you'll need some proper configuration such as adding the email Smart Package and setting MAIL_URL) to email the result of the template rendering. Email.send only works on the server, so you must define a server method callable from the client.

服务器端:

Meteor.methods({
  sendShareEmail:function(options){
    // you should probably validate options using check before actually
    // sending email
    check(options,{
      from:String,
      // etc...
    });
    Email.send(options);
  }
});

客户端:

var dataContext={
  message:"You must see this, it's amazing !",
  url:"http://myapp.com/content/amazingstuff",
  title:"Amazing stuff, click me !"
};
var html=Blaze.toHTMLWithData(Template.shareEmailContent,dataContext);
var options={
  from:"sender@domain.com",
  to:"receiver@domain.com",
  subject:"I want to share this with you !",
  html:html
  })
};
Meteor.call("sendShareEmail",options);

如评论中所述,您还可以决定在服务器上呈现电子邮件模板.尚不支持服务器端渲染,但您仍然可以使用第三方模板包来完成它.

As mentioned in the comments, you can also decide to render email templates on the server. Server-side rendering is not yet supported but you can still accomplish it using a third party templating package.

EDIT 06/09/2014 :更新为使用最新的 Blaze API 作为 Meteor 0.9.1

EDIT 06/09/2014 : updated to use the latest Blaze API as of Meteor 0.9.1

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

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