簡體   English   中英

使用meteor js成功注冊用戶后發送電子郵件

[英]Send email after successful user registration using meteor js

我想將電子郵件功能添加到我的應用程序中。 我已經添加了電子郵件包,並根據提供的文檔執行了步驟

我想當用戶注冊自己時,成功注冊后應該發送一封電子郵件。

這是我嘗試過的:

服務器/ smtp.js:

Meteor.startup(function () {
  smtp = {
    username: 'abc@gmail.com',   // eg: server@gentlenode.com
    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;
});

這是我的server / 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',
                        'alice@example.com',
                        'abc@example.com',
                        'Hello from Meteor!',
                        'This is a test of Email.send.');
                        Router.go("home");
                    }
                }
            });
        }
    });
});

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

我可以建議一個替代解決方案嗎? 將鈎子添加到服務器端的Accounts.onCreateUser()以發送電子郵件:

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

您實際上在使用gmail嗎? 我相信Google的SMTP端口是465,而不是25。請使用該配置確認您的電子郵件發送在流星之外正常工作,然后在流星中嘗試發送。 我也相信Google每天限制通過SMTP發送的電子郵件數量為99。 如果要驗證用戶的電子郵件地址,請使用accounts包中的內置電子郵件驗證系統。

暫無
暫無

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

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