How to get verification link for custom sent verification email?

We’ve created custom designed email templates that we’re intending to send out via sendgrid upon signup and other important events in our app. I can’t seem to find a way how to simply just get a verification link which we can then send to the user by calling the sendgrid api, passing the url and the template we want to use in sendgrid.

It seems the verification link is tied to the ugly default email and send function of meteor?

2 Likes

hmm, here’s what the reset password email code does: https://github.com/meteor/meteor/blob/48cc1078a40ea18bc131e39391f69bdbac3c76e5/packages/accounts-password/password_server.js#L529

Looks like it does indeed call Email.send at the end. So I guess one option is to monkey-patch Email.send, or submit a PR to make that pluggable somehow.

At first glance seems we can just use

var tokenRecord = {
    token: Random.secret(),
    address: address,
    when: new Date()};
}

Meteor.users.update(
    {_id: userId},
    {$push: {'services.email.verificationTokens': tokenRecord}});

var verifyEmailUrl = Accounts.urls.verifyEmail(tokenRecord.token);

to plug our own verification token into the user profile. Though I’ll have a look if I can do a PR for this. First time looking at meteor code. That would be my first :blush:

2 Likes

Yeah - I think that should work great!

1 Like

Any idea why this is not working?

SendNewUserMail: function (address) {

  var tokenRecord = {
    token: Random.secret(),
    address: address,
    when: new Date()
  };

  Meteor.users.update(
    {_id: this.userId},
    {
      $push: {
        'services.email.verificationTokens': tokenRecord
      }
    },
    function (error, result) {
      if(error) {
        console.log(error);
      }
      else {
        console.log(result);
      }
    }
  );
}

The result always shows that a document gets updated and there’s no error. I have no idea why nothing’s happening. I tried sets, push with update but also upserts set push. Nothing actually pushes a record to the user. Allow/deny shouldn’t matter also since it’s on the server?

I’ve been trying to get this working without result for a while now though. I’m thinking of just changing the sendVerificationEmail function to accept a third optional argument which would be a method to call instead of Email.send();

Does this sound like an acceptable solution to work on and create a PR for? I’ve never tweaked meteor code, can I just edit the code in the local build and it gets rebuilt properly so I can test this? Where exactly do I edit my code?

Though I’ve read several times that PRs take forever to get accepted so meaning I could only do this if I don’t want to update our version for a while since I can expect the PR to sit around for weeks/months before being accepted?

Seems like editing code in local/build/programs/server/packages is not permitted?

The weird thing is even this is not working:

VerifyUser: function() {
  Accounts.sendVerificationEmail( this.userId );      
},

@sashko Could it be there’s a breaking change with Meteor 1.3? I was checking if there’s something in my packages that could be it but I don’t think I have anything user interfering. I notice I have no accounts-base installed but according to the docs that should be imported by accounts-password and it seems like user creation itself is working as expected.

Did you ever resolve this?

@elie What is your use case/issue?

We are using ssr-rendered templates passed to mailgun for the most part but still rely on passing html as params into some default account functions like the send verification email. Not amazing, as the account emails go through a different flow, but it works.

Was wondering if you got it working through sendgrid as opposed to mailgun

Elie

Ah, no sorry I haven’t used sendgrid in a while now.

Does this still work?

I tried to find some more information in the Meteor docs but Accounts.urls.verifyEmail is not mentioned.