2016-01-28 53 views
2

我想將電子郵件功能添加到我的應用程序中。我已添加電子郵件包,並按照documentation提供的documentation提供使用meteor js成功註冊用戶後發送電子郵件

我希望當用戶註冊自己時,應該在成功註冊後發送電子郵件。

這裏是我的嘗試:

服務器/ smtp.js:

Meteor.startup(function() { 
    smtp = { 
    username: '[email protected]', // eg: [email protected] 
    password: 'abc123', // eg: 3eeP1gtizk5eziohfervU 
    server: 'smtp.gmail.com', // eg: mail.gandi.net 
    port: 25 
    } 

    process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) + ':' + smtp.port; 
}); 

這裏是我的服務器/ emp_details.js在那裏我有調用的方法。下面的代碼放在裏面Meteor.methods()

sendEmail: function (to, from, subject, text) { 
    check([to, from, subject, text], [String]); 

    // Let other method calls from the same client start running, 
    // without waiting for the email sending to complete. 
    this.unblock(); 

    //actual email sending method 
    Email.send({ 
     to: to, 
     from: from, 
     subject: subject, 
     text: text 
    }); 
    } 

最後,我呼籲在客戶端的方法如下所示:

Template.register.onRendered(function() 
{ 
    var validator = $('.register').validate({ 
     submitHandler: function(event) 
     { 
      var email = $('[name=email]').val(); 
      var password = $('[name=password]').val(); 
      var empProfile = Session.get('profileImage'); 
      console.log(empProfile); 
      Accounts.createUser({ 
       email: email, 
       password: password, 
       profile: { 
        name:'test', 
        image: empProfile 
       }, 
       function(error) 
       { 
        if(error) 
        { 
         if(error.reason == "Email already exists.") 
         { 
          validator.showErrors({ 
           email: "This email already belongs to a registered user" 
          }); 
         } 
        } 
        else 
        { 
         Meteor.call('sendEmail', 
         '[email protected]', 
         '[email protected]', 
         'Hello from Meteor!', 
         'This is a test of Email.send.'); 
         Router.go("home"); 
        } 
       } 
      }); 
     } 
    }); 
}); 

我不知道如何正確地使用這個電子郵件功能。

回答

4

我可以提出一種替代解決方案嗎?加入一個鉤子Accounts.onCreateUser()在服務器端發送電子郵件:

Accounts.onCreateUser(function (options, user) { 
    Meteor.call(
     'sendEmail', 
     '[email protected]', 
     user.profile.email, //use the path to the users email in their profile 
     'Hello from Meteor!', 
     'This is a test of Email.send.' 
    ); 
}); 

你實際使用Gmail嗎?谷歌的SMTP端口是465我相信,而不是25.確認您的電子郵件發送在流星之外使用該配置,然後在Meteor中嘗試。我也相信谷歌將通過SMTP發送的電子郵件數量限制爲每天99個,所以要小心。如果您想驗證用戶電子郵件地址,請使用accounts包中的內置電子郵件驗證系統。

+1

謝謝佈雷特,它的工作,只是一個user.profile.email遊戲 –

+0

請建議一個備用解決方案也。我也想嘗試一下。 –

相關問題