在流星电子邮件中使用动态HTML模板 [英] Using dynamic HTML templates in Meteor emails
问题描述
是否可以将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(请参阅docs.meteor.com上的 Email.send ,您将需要一些适当的配置,例如添加电子邮件智能软件包和设置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.
2014年6月9日修改:已更新,以使用自流星0.9.1起的最新Blaze
API
EDIT 06/09/2014 : updated to use the latest Blaze
API as of Meteor 0.9.1
这篇关于在流星电子邮件中使用动态HTML模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!