我正在尝试使用Meteor电子邮件包发送电子邮件,但我无法让它工作。我无法从客户端拨打电子邮件,因为在数据库中存在某些属性之前无法发送电子邮件。
这是我目前的代码:
var dataContext = {
numParticipants: numParticipants,
link1: link1,
link2: link2
}
var email = Blaze.toHTMLWithData(Template.paidEmail, dataContext);
if (Meteor.isServer) {
this.unblock();
Email.send({
to: sendTo,
from: 'example@email.com',
subject: 'Your creation has been created!',
html: email
});
}
我不确定如何继续前进。在这种情况下我收到Template is not defined
错误,如果我将开头部分包裹在Meteor.isClient
内,则不会传递给第二部分。
有什么想法吗?
答案 0 :(得分:1)
我认为你对Meteor的异形工作方式有点困惑。虽然您可以在客户端和服务器上使用相同的代码块,但它将在完全不同的实例中,因此您不能使用客户端Blaze库呈现某些HTML并期望它在服务器上可用以下块,只是因为它们在构建应用程序之前位于同一文件中;当您的应用实际运行时,它们将存在于完全不同的上下文中。
您需要将服务器代码包装在Meteor.methods
块中,然后从客户端调用它。类似的东西:
if (Meteor.isClient) {
var dataContext = {
numParticipants: numParticipants,
link1: link1,
link2: link2
}
var email = Blaze.toHTMLWithData(Template.paidEmail, dataContext);
Meteor.call('send-email', sendTo, email);
}
if (Meteor.isServer) {
Meteor.methods({
'send-email': function(sendTo, email) {
this.unblock();
Email.send({
to: sendTo,
from: 'example@email.com',
subject: 'Your creation has been created!',
html: email
});
return true;
}
});
}
两个注释:
Futures
等服务器方法中使用Email.send
进行慢速异步调用非常有用。我没有把它放在上面的例子中,因为它只会在这个阶段使问题变得模糊,但它绝对值得looking in to,特别是对于需要提供反馈的方法。