Meteor accounts sendVerificationEmail

Hello there,
i’m stil struggling with email verification :confused:
People like Accounts.sendverification email not sending seem to use some code to archive this, but as i understand the docs: http://docs.meteor.com/#/full/accounts_config it should be as simple as this(event just 3. should be enough):

Meteor.startup(function () {
    // 1. Set up stmp
    var username = 'xxx';
    var password = 'xxx';
    var server = 'smtp.mailgun.org';
    var port = '25';

    process.env.MAIL_URL = 'smtp://' +
        encodeURIComponent(username) + ':' +
        encodeURIComponent(password) + '@' +
        encodeURIComponent(server) + ':' + port;

    // 2. Format the email
    //-- Set the from address
    Accounts.emailTemplates.from = 'test@test.com';

    //-- Application name
    Accounts.emailTemplates.siteName = 'My_App';

    //-- Subject line of the email.
    Accounts.emailTemplates.verifyEmail.subject = function(user) {
        return 'Confirm Your Email Address for My_App';
    };

    //-- Email text
    Accounts.emailTemplates.verifyEmail.text = function(user, url) {
        var newUrl = url.replace('/#','');
        return 'Thank you for registering.  Please click on the following link to verify your email address: \r\n' + newUrl;
    };
    //3.
    Accounts.config({
        sendVerificationEmail: true
    });
});

And now whenever someone registers he/she gets a mail?
Apparently nothing like this happens and there is no error log on server or client :frowning:

So what am i missing here?
The mailgun configuration works -> i can sent emails via contactform.

Regards,
Lukas

Hi Lukas,

I use something like the code below. The Mandrill.config() is for API access. The important part is the MAIL_URL. That’s how meteor sends the email.

Make sure you have the right packages installed. Someone else might have a better answer, but I would guess:

  • accounts-password - Probably need this, but not sure
  • email
  • cmather:handlebars-server - for the server side email templates
  • wylio:mandrill - for mandrill, but obviously not required.
// Configure default email MAIL_URL for accounts
Meteor.startup(function () {

  // Configure API access
  Mandrill.config({
    username: Meteor.settings.mandrill.user,
    key: Meteor.settings.mandrill.pass,
    port: Meteor.settings.mandrill.port || 587,
    host: Meteor.settings.mandrill.host || 'smtp.mandrillapp.com',
    baseUrl: 'https://mandrillapp.com/api/1.0/'
  });

  process.env.MAIL_URL = 'smtp://' +
    encodeURIComponent(Meteor.settings.mandrill.user) + ':' +
    encodeURIComponent(Meteor.settings.mandrill.pass) + '@' +
    encodeURIComponent(Meteor.settings.mandrill.host || 'smtp.mandrillapp.com') + ':' +
    (Meteor.settings.mandrill.port || 587);

  Accounts.emailTemplates.siteName = Meteor.settings.siteName;
  Accounts.emailTemplates.from = Meteor.settings.emailFrom;
});

// Welcome and Email Verification

Accounts.emailTemplates.verifyEmail.subject = function(user) {
  return 'Welcome to Funkytown.io :-)';
};
Accounts.emailTemplates.verifyEmail.html = function (user, url) {
  // return html string
  return Handlebars.templates.verifyEmail_html({
    emailAddress: user.email(),
    url: url,
  });
};
Accounts.emailTemplates.verifyEmail.text = function (user, url) {
  // return text string
  return Handlebars.templates.verifyEmail_text({
    emailAddress: user.email(),
    url: url,
  });
};

Hay michael,
this doesn’t really defer from my solution except for using mandrill instead of mailgun and using a serverside templating instead of plain text? I’m not sure, but i can’t believe but this is the reason :confused:

Especially because sending emails with this config works general (even when using the “resent verification link” is anabled, pressing it sends a mail :frowning: )

got it working with this on the server

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;
});

But now i’m once more totally confused?!
I thought:

Accounts.config({
        sendVerificationEmail: true
    });

would do that job? I use the accounts package inside a package - did i miss some export stuff?
Regards,
Lukas

1 Like

You know what’s even weirder, you need that setTimeout for the email be sent. @sakulstra thanks for the tip.