10

I want to send a verification email when some user is created. I use the accounts-password package, so any Accounts methods are called in my code.

I read in documentation that I need to call:

Accounts.sendVerificationEmail(userId, [email])

but the problem is that I don't know when to call it.

I tried to call in the callback function of Accounts.onCreateUser(func) but the user had not been created yet in the database.

Any ideas?

Diego
  • 462
  • 10
  • 26
nsblenin
  • 237
  • 3
  • 12
  • 1
    I got it. Just call Accounts.config({sendVerificationEmail: true, forbidClientAccountCreation: false}); in the server – nsblenin Jan 31 '13 at 01:12
  • 3
    You can add your comment as an answer and accept your own answer (you'll have access in a couple days). – TimDog Jan 31 '13 at 01:59
  • Is this still an issue nsblenin? – cmather Mar 16 '13 at 01:35
  • Adding this on for anyone stumbling across: Although the Meteor docs say you can add it "Anywhere", it only worked for me after adding the Accounts.config code in the server-only javascript file. – timelincoln Apr 24 '13 at 02:04
  • Seriously, why do people almost *NEVER* add their own answers below when they stumble upon a correct solution? – Seth Malaki May 10 '13 at 15:10

3 Answers3

14

on the serverside:

Accounts.config({sendVerificationEmail: true, forbidClientAccountCreation: false}); 

got the answer from the comments above.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Laurens
  • 141
  • 1
  • 3
3

sendVerificationEmail is only available server-side. What I usually do is to use a setInterval inside onCreateUser to wait for Meteor to create the user before sending an email.

Read More: Verify an Email with Meteor Accounts.

// (server-side)
Accounts.onCreateUser(function(options, user) {  
  user.profile = {};

  // we wait for Meteor to create the user before sending an email
  Meteor.setTimeout(function() {
    Accounts.sendVerificationEmail(user._id);
  }, 2 * 1000);

  return user;
});
Julien Le Coupanec
  • 7,742
  • 9
  • 53
  • 67
  • The setTimeout is not necessary as the user has already and _id property at that time. – Fabian Vilers Jul 23 '15 at 11:32
  • 1
    Forget my previous comment, the sendVerificiationEmail function would complains about a user not found.I think a better approach is to hook on after insert on the Meteor.user collection and send the verification email then. – Fabian Vilers Jul 23 '15 at 11:44
2

You need specify mail in enviroment variables. Then use Accounts.sendVerificationEmail(userId, [email]) in callback of Account.onCreateUser sorry for mistake and delay.

Like this (below is full example js file):

Template.register.events({
'submit #register-form' : function(e, t) {
  e.preventDefault();
  var email = t.find('#account-email').value
    , password = t.find('#account-password').value;

    // Trim and validate the input

  Accounts.onCreateUser({email: email, password : password}, function(err){
      if (err) {
        // Inform the user that account creation failed
      } else {
        // Success. Account has been created and the user
        // has logged in successfully.
       Accounts.sendVerificationEmail(this.userId, email);
      }
    });

  return false;
}  });

if(Meteor.isServer){
   Meteor.startup(function(){
      process.env.MAIL_URL='smtp://your_mail:your_password@host:port'
   }
}

I refered to this pages : http://blog.benmcmahen.com/post/41741539120/building-a-customized-accounts-ui-for-meteor

http://sendgrid.com/blog/send-email-meteor-sendgrid/

How come my Meteor app with accounts package is not sending a verification email?

Community
  • 1
  • 1
despi23
  • 138
  • 4
  • I tried this and receive the following error: Exception in delivering result of invoking 'createUser': TypeError: Object # has no method 'sendVerificationEmail' Do you know what I might be doing wrong? – user1447679 Mar 02 '14 at 06:36
  • In latest patch of meteor they might change sendVericationEmail in documentation its said now its usable by server side. Ill check it myself and edit my post when im done.Here link to documentation http://docs.meteor.com/#accounts_sendverificationemail. – despi23 Mar 07 '14 at 18:25
  • Sorry i for missleading its Acoounts.onCreateUser. – despi23 Jan 01 '15 at 21:18