2012-12-11 42 views
2
var SITE_URL = Meteor.absoluteUrl(); 

function sendEmailNotification(type, sourceUser, recipients, notificationObject) { 
    var emailFrom = '[email protected]'; 
    var emailSubject = 'MyApp: '; 
    var emailBody = ''; 
    $.each(recipients, function (index, recipient) { 
     recipients[index] = recipient + '@example.com'; 
    }); 

    switch(type) { 
     case 'item_assigned': 
      emailSubject += notificationObject.item_title; 
      emailBody += '<div style="padding:10px;">'; 
      emailBody += sourceUser; 
      emailBody += ' has assigned you an action item'; 
      emailBody += '</div>' 
      break; 
     case 'list_shared': 
      emailSubject += notificationObject.list_title; 
      emailBody += '<div style="padding:10px;">'; 
      emailBody += sourceUser; 
      emailBody += ' has shared a list with you: '; 
      emailBody += '<a href="' + SITE_URL + '#' + notificationObject.list_id + '">' + notificationObject.list_title + '</a>'; 
      emailBody += '</div>' 
      break; 
    } 
    if (Meteor.isServer) { 
     // This function only runs on server 
     Email.send({ 
      from: emailFrom, 
      bcc: recipients, 
      subject: emailSubject, 
      html: emailBody 
     }); 
    } 
} 

上述函數位於根文件中(因此它的代碼對客戶端和服務器都可用)。但是當我在我的客戶端代碼中調用它時,什麼也沒有發生。我的應用程序中包含了email軟件包。在我的本地機器上(Windows 7),我沒有MAIL_URL變量集。所以調用Email.send()函數理想情況下應該在命令提示符下產生輸出,但實際上沒有輸出。meteorjs:我正確使用Email.send()嗎?

在我們的生產服務器上,SMTP已正確設置,其他應用程序可以使用相同的設置發送電子郵件。我在那裏正確配置了MAIL_URL環境變量,但仍然沒有發送郵件。

有人可以告訴我,如果我的代碼有問題嗎?有什麼我不正確的做法?

P.S .:我甚至嘗試直接調用Email.send(),如下面的代碼,但仍然沒有發生任何事情。

if (Meteor.isServer) { 
    Email.send({ 
     from: '[email protected]', 
     to: '[email protected]', 
     subject: 'This is a test email', 
     html: '<b>Congrats, it works!</b>' 
    }); 
} 



    } 
}); 
+0

您也可以發表您的解決方案作爲一個答案,你自己的問題,這都將獲得你的聲譽以及幫助未來的遊客! – Rahul

回答

3

差不多的Meteor's Email is undefined

重複見this pull request示例代碼。

只是爲了澄清:Meteor不會像這樣依次執行客戶端和服務器代碼。您必須更清楚地瞭解客戶端與服務器上運行的內容。不要考慮沿着JavaScript頁面的線性執行方式,而應該認爲每條Meteor代碼都是作爲事件的結果運行的。如果某段代碼沒有運行,那是因爲沒有事件觸發它。

+1

謝謝大衛。我通過使用Meteor.methods在我的服務器端代碼中創建一個新方法,然後在客戶端代碼中使用Method.call來調用該服務器方法。 但是我強烈地感覺到電子郵件的一部分對流星的官方API文檔有點混淆。他們應該爲他們的文檔中的相同內容提供更明確的示例。 – iamgame

0

我通過使用Meteor.methods創建服務器端方法並將上面的所有代碼放入其中來解決了這個問題。

var SITE_URL = Meteor.absoluteUrl(); 

Meteor.methods({ 
    sendEmailNotification: function (type, sourceUser, recipients, notificationObject) { 
     if (recipients.length > 0) { 
      var emailFrom = '[email protected]'; 
      var emailSubject = 'MyApp: '; 
      var emailBody = ''; 
      for (var i = 0; i < recipients.length; i++) { 
       recipients[i] = recipients[i] + '@example.com'; 
      } 

      switch (type) { 
       case 'item_assigned': 
        emailSubject += notificationObject.item_title; 
        emailBody += '<div style="padding:10px;">'; 
        emailBody += sourceUser; 
        emailBody += ' has assigned you an action item'; 
        emailBody += '</div>' 
        break; 
       case 'list_shared': 
        emailSubject += notificationObject.list_title; 
        emailBody += '<div style="padding:10px;">'; 
        emailBody += sourceUser; 
        emailBody += ' has shared a list with you: '; 
        emailBody += '<a href="' + SITE_URL + '#' + notificationObject.list_id + '">' + notificationObject.list_title + '</a>'; 
        emailBody += '</div>' 
        break; 
      } 
      Email.send({ 
       from: emailFrom, 
       bcc: recipients, 
       subject: emailSubject, 
       html: emailBody 
      }); 
     } 
    } 
}); 

要調用上述功能在客戶端代碼,使用:

Meteor.call('sendEmailNotification', 'list_shared', Meteor.user().username, [sharedUserName], listDetails);