Mailgun integration ParseServer

In this blog, we learn how to integrate Mailgun with ParseServer and deploying on Heroku.

We also enable E-mail verification for Parse Users.

Once you have your app created and integrated with parse-server and deployed on Heroku, enable Mailgun add-on on Heroku.

mailgun1

By default, you will have a Sandbox domain created for you.

mailgun2

If you are using Mailgun for testing or educational purpose, you need to add emails authorized for use with Mailgun. Click on domain name.

mailgun3

Once you have configured Mailgun add-on with Heroku, you will have these environment variables added.

mailgun4

Now, we need to go to your github project integrated with your app on Heroku.

In that project, look for index.js file.

Add this configuration to the index.js file :

var server = ParseServer({
    ...otherOptions,
    // Enable email verification
    verifyUserEmails: true,
    // The public URL of your app.
    // This will appear in the link that is used to verify email addresses and reset passwords.
    // Set the mount path as it is in serverURL
    publicServerURL: 'https://contacts1.herokuapp.com/parse',
    // Your apps name. This will appear in the subject and body of the emails that are sent.
    appName: 'contacts1',
    // The email adapter
    emailAdapter: {
        module: 'parse-server-simple-mailgun-adapter',
        options: {
            // The address that your emails come from
            fromAddress: 'parse@example.com',
            // Your domain from mailgun.com
            domain: 'xxxxxxxxxxxxx.mailgun.org',
            // Your API key from mailgun.com
            apiKey: 'key-xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        }
    }
});

After you update and commit the file index.js, redeploy app on Heroku.

Make sure there are no errors for your app by clicking on ‘Open App’.

With this infrastructure, when you create ParseUser, a new property “emailVerified” gets added to the user object in MongoDb.

Updated parse-server configuration :  using environment variables

var api = new ParseServer({
    databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
    cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
    appId: process.env.APP_ID || 'myAppId',
    masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
    serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed
    verifyUserEmails: process.env.PARSE_SERVER_VERIFY_USER_EMAILS || false,
    publicServerURL: process.env.PARSE_PUBLIC_SERVER_URL || 'http://localhost:1337/parse',
    appName: process.env.PARSE_SERVER_APP_NAME || 'myAppName',
    emailAdapter: {
        module: 'parse-server-simple-mailgun-adapter',
        options: {
            fromAddress: process.env.DEFAULT_FROM_ADDRESS,
            domain: process.env.MAILGUN_DOMAIN,
            apiKey: process.env.MAILGUN_API_KEY,
        }
    },
    liveQuery: {
        classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
    }
});