簡體   English   中英

用Meteor調用方法后如何發送電子郵件?

[英]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;
        }
    });
}

兩個注意事項:

  1. 一旦達到這種復雜性級別,就強烈建議開始將代碼庫分解為單獨的客戶端和服務器文件(在適當的目錄中 )。 如本示例所示,您可以將它們放在同一個文件中,但是如果您的代碼庫很小,並且迅速變得混亂,則沒有真正的優勢。
  2. 在諸如Email.send類的服務器方法Email.send Futures用於慢速異步調用可能會非常有益。 我沒有將它放在上面的示例中,因為它在這個階段只能解決問題,但是絕對值得研究 ,特別是對於需要提供反饋的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM