javascript - 使用meteor js用户注册成功后发送电子邮件

标签 javascript email meteor meteor-accounts

我想将电子邮件功能添加到我的应用程序中。我已添加电子邮件包并按照documentation执行步骤提供

我希望当用户自行注册时,注册成功后应发送一封电子邮件。

这是我尝试过的:

服务器/smtp.js:

Meteor.startup(function () {
  smtp = {
    username: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccadaeaf8caba1ada5a0e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>',   // eg: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92e1f7e0e4f7e0d2f5f7fce6fef7fcfdf6f7bcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>
    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',
                        '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="26474a4f454366435e474b564a430845494b" rel="noreferrer noopener nofollow">[email protected]</a>',
                        '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f1e1d1c3f1a071e120f131a511c1012" rel="noreferrer noopener nofollow">[email protected]</a>',
                        'Hello from Meteor!',
                        'This is a test of Email.send.');
                        Router.go("home");
                    }
                }
            });
        }
    });
});

我不知道如何正确使用此电子邮件功能。

最佳答案

我可以建议一个替代解决方案吗?在服务器端添加一个钩子(Hook)到Accounts.onCreateUser()来发送电子邮件:

Accounts.onCreateUser(function (options, user) {
    Meteor.call(
        'sendEmail', 
        '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8e8b828681af96809a9d9c869b8ac18c8082" rel="noreferrer noopener nofollow">[email protected]</a>',
        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。使用该配置确认您的电子邮件发送在 Meteor 之外可以正常工作,然后在 Meteor 中尝试。我还相信 google 将每天通过 SMTP 发送的电子邮件数量限制为 99 封,所以要小心。如果您想验证用户的电子邮件地址,请使用 accounts 包中的内置电子邮件验证系统。

关于javascript - 使用meteor js用户注册成功后发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062463/

相关文章:

javascript - 查看 GWT 应用程序的所有 javascript 变量

email - 如何在 CF9 中使用 cfscript 发送带有附件的电子邮件?

ios - 在 ios 中发送带有表格 View 和饼图的电子邮件

mongodb - 更新 Mongo 对象字段

mongodb - Meteor 消耗了大量的 Mongo 连接

javascript - 检测当前 Web 组件外部的点击

javascript - JSON.解析: unexpected end of data (javascript)

javascript - Sequelize 关联 : How to update parent model when creating child?

php - Codeigniter $this->email->send() 在 mail() 时不工作

javascript - meteor 用户在客户端始终未定义