[英]How can I send emails after a method call with Meteor?
我正在嘗試使用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
,則它不會傳遞到第二部分。
有任何想法嗎?
我認為您對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;
}
});
}
兩個注意事項:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.