使用Mandrill对Meteor进行完整的电子邮件验证

时间:2015-10-27 21:44:28

标签: mandrill meteor-accounts

我已经阅读了几篇关于使用Mandrill和Meteor.js进行电子邮件验证的SO帖子,但我发现了其他人似乎没有遇到的问题。

最终,我希望在点击电子邮件验证网址后将用户的verified属性设置为true。我使用Mandrill发送包含verification_url的自定义电子邮件模板。我添加了accounts-passwordaccounts-ui个包。我的代码如下所示:

if (Meteor.isServer) {
  Meteor.startup(function () {
    Mandrill.config({
      username: process.env.MANDRILL_API_USER,
      key: process.env.MANDRILL_API_KEY
      // port: 587,  // defaults to 465 for SMTP over TLS
      // host: 'smtp.mandrillapp.com',  // the SMTP host
      // baseUrl: 'https://mandrillapp.com/api/1.0/'  // update this in case Mandrill changes its API endpoint URL or version
    });

    Accounts.config({
      sendVerificationEmail: true
    });

    Accounts.emailTemplates.verifyEmail.html = function (user, url) {
      var referralCode = Random.id();
      var result;
      try {
        result = Mandrill.templates.render({
          template_name: 'email-verification',
          template_content: [],
          merge_vars: [
            {
              name: 'SUBJECT',
              content: 'my fancy subject'
            },
            { name: 'EMAIL',
              content: 'my fancy email'
            },
            {
              name: 'VERIFICATION_URL',
              content: 'http://localhost:3000/?ref=' + referralCode
            }
          ]
        });
      } catch (error) {
        console.error('Error while rendering Mandrill template', error);
      }
      return result.data.html;
    };
});

当我创建用户时,验证电子邮件已正确发送,但是当我单击电子邮件中的验证链接时,服务器上没有任何操作,即我查看我的应用程序的MongoDB并查看用户文档仍然具有该属性verified: false。我曾尝试使用onEmailVerificationLink(http://docs.meteor.com/#/full/Accounts-onEmailVerificationLink),但我在控制台中收到错误,说已经调用了onEmailVerificationLink,这是因为accounts-ui显然是在为我调用它。如何使用Mandrill在Meteor.js中进行正确的电子邮件验证?

1 个答案:

答案 0 :(得分:1)

终于明白了。而不是行

content: 'http://localhost:3000/?ref=' + referralCode

我应该用

替换它
content: url

因为Meteor已经为我创建了验证网址,并通过" url"函数的参数。显然我不需要referralCode