首页 > 解决方案 > 困惑如何自定义流星验证邮件

问题描述

如何将验证链接从默认 Meteor 方法获取到我拥有的使用 sendgrid stmp 的自定义电子邮件方法中。

这是流星验证方法,它本身运行良好,并且有我想要的链接:

sendVerificationLink() {
 let userId = Meteor.userId();
   if ( userId ) {
     return Accounts.sendVerificationEmail( userId );
    }
  },

这是我使用 sendgrid 的自定义方法,除了我无法弄清楚如何使用自定义令牌获取链接之外,一切正常:

'signupEmail' (submission) {
   this.unblock();
   const link = ''
   const message = `welcome ${submission.firstname} `
   const text = `welcome ${submission.firstname}. Please verify your 
   account ${link}`
   Email.send({
     from: "hi@test.com",
     to: submission.email,
     subject: message,
     text: text,
      html: text,
    });
} 

标签: meteor

解决方案


以防万一将来有人在寻找这个,我在 Meteor 论坛上找到了答案: https ://forums.meteor.com/t/how-to-get-verification-link-for-custom-sent-verification-email /22932/2

基本上我添加了一个令牌记录并将其保存在数据库中。然后将令牌与方法一起使用:Accounts.urls.verifyEmail 创建了要插入电子邮件的链接。

这是我的最终方法:

'signupEmail' (submission) {
    this.unblock();
    let userId = Meteor.userId();
    var tokenRecord = {
      token: Random.secret(),
      address: submission.email,
      when: new Date()};

      Meteor.users.update(
        {_id: userId},
        {$push: {'services.email.verificationTokens': tokenRecord}}
      );
      const verifyEmailUrl = Accounts.urls.verifyEmail(tokenRecord.token);

      const message = `welcome ${submission.firstname} `
      const text = `welcome ${submission.firstname}. Please verify your account ${verifyEmailUrl}`
      Email.send({
        from: "hi@test.com",
        to: submission.email,
        subject: message,
        text: text,
        html: text,
      });
  },

推荐阅读