shekar73 / meteor-email

Send verification, notification and reminder emails from any Meteor app.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Meteor Email (Verification)

Send verification, notification and reminder emails from any Meteor app.

Background

As part of registering new users for your fantastic Meteor app you will need to verify their email addresses to ensure that people are not signing up with fake emails (or worse using someone else's email!)

This tutorial shows you how to do this kind of verification.

Example

meteor verify email

Try it: http://meteor-email.meteor.com/

Implementation

Meteor has a method for verifying that an email addres is valid:
Accounts.sendVerificationEmail(userId, [email]) but you need to configure email before you can send.

Setup Meteor to Send Email

Enable the Email Module

In your terminal/console run:

meteor add email

Send Email Through Gmail SMTP

In your server/server.js file add the following MAIL_URL config line:

process.env.MAIL_URL="smtp://xxxxx%40gmail.com:yyyyy@smtp.gmail.com:465/";

Where xxxxx is your gmail username and yyyyy is your gmail password.

Send Your First Email Through Gmail

In your server/server.js file add this email directive

Email.send({
  from: "meteor.email.2014@gmail.com",
  to: "your-personal-email-here@gmail.com",
  subject: "Meteor Can Send Emails via Gmail",
  text: "Its pretty easy to send emails via gmail."
});

You should receive an email within a few seconds:

Meteor Gmail

As soon as you've confirmed that's working, comment out the Email.send code so you don't continuously send yourself test emails each time you update your project.

Enable Meteor Accounts

Enable the simplest type of user account (email and password)

meteor add accounts-base
meteor add accounts-password

Now you can use the Accounts.createUser and Accounts.sendVerificationEmail methods.

That will send an email in the form:

Meteor verification email

The standard Meteor virification link looks like: http://yoursite.com/#/verify-email/gDSfHxYWuzwRiqmmN

Add Iron Router

Add the Iron Router package (requires meteorite)

mrt add iron-router

Iron router routes.js with controller function for handling verification.

Router.map(function () {
    this.route('/', {
        path: '/',
        template: 'verifyemail',
    });

    this.route('verifyEmail', {
        controller: 'AccountController',
        path: '/verify-email/:token',
        action: 'verifyEmail'
    });

    this.route('verified', {
        path: '/verified',
        template: 'verified'
    });

    this.route('checkemail', {
        path: '/checkemail',
        template: 'checkemail'
    });
});

// More info: https://github.com/EventedMind/iron-router/issues/3
AccountController = RouteController.extend({
    verifyEmail: function () {
        Accounts.verifyEmail(this.params.token, function () {
            Router.go('/verified');
        });
    }
});

Now the verification email will be in the form:

Iron router verification email

Try it!

This is what you can see:

Form prompting for email

Verification Email Sent

Verification link in email

Verified

Useful Links:

Disposable Gmail Account (used for testing):

Notes

  1. We have used this successfully in our Meteor Apps but have not written automated tests because sending email is part of Meteor's Core Functionality. If anyone else wants to write tests and make this into an Atmosphere package, we are happy to point this tutorial to your package so you get the click throughs!

  2. We are not using the latest Meteor so have not tried this code with meteor 1.0< if you have issues, please add them here on GitHub so others can learn. Thanks!

About

Send verification, notification and reminder emails from any Meteor app.


Languages

Language:JavaScript 76.5%Language:HTML 19.4%Language:CSS 4.1%