/ meteor

Email subscription in Meteor with Mailgun

Email is still the most reliable way of messaging online. It is universal, it is not owned by anyone, it's got the most users out of any messaging system in the world, it's easy to use. This is why I prefer email newsletters. This is why I subscribe by email and why I let my visitors have this option.

Mailgun is still my favorite emailing service. No, I haven't tried them all. I haven't tried any. I have no reason to.

Here is how you can also get returning visitors with email subscriptions.

Mailgun

I'm skipping the "how to set up Mailgun" tutorial, as I've written about this here.

Everything set up? All you need to do now is create a mailing list. I'm sure you're smart enough but I'll add images of the three buttons you need to press anyway, makes this blog look more fun and attractive. from a distance.

Meteor

I'm assuming you know how to deal with forms. If not - google it! Or ask me to write a tutorial. I'll do it if you promise me heavy traffic!

Modules

First things first, we need to send a post request so we need to add the http module first

meteor add http

Code

So we have code in four files:

server/
    secrets.js     // separate file for api-key
	mailgun.js     // sends data to Mailgun
client/
	dostuff.js     // call mailgun.js when form is submitted
.gitignore         // keeps our secrets.js secret

server/secrets.js

Mailgun gives you an api-key, that you can access from mailgun.com/cp. We need to attach this to our "sign this guy up for the newsletter" message to authenticate our app, but we also need to keep it a secret from the nasty hackers. Actually, hackers are cool and they may just save the world someday. But anyway we'll try keep them from screwing with our mailing list.

process.env['MAILGUN_API_KEY'] = "key-keepingitasecretlikeapro";

server/mailgun.js

This is what does the actualy work - sends data to Mailgun through a post request.

Meteor.startup(function () {
	Meteor.methods({
		
		subscribe: function(email) {

			// Make multiple requests faster
			this.unblock();

			// Define useful variables
			var mailinglist = 'news@krister.ee'
			var apiURL = 'https://api.mailgun.net/v2/lists/' + mailinglist + '/members' 
			var options = {
				auth:"api:" + process.env.MAILGUN_API_KEY,
				params: {
					subscribed: true,
					address: email,
				}
			}

			// What happens after the request has gotten reply
			var onResult = function(error, result) {
				if(error){ 
					console.log("Error: " + error)
				} else {
					console.log("Subscription successful")
				}
			}

			// Make the actual request happen
			Meteor.http.post(apiURL, options, onResult)
		},
	});
});

client/dostuff.js

Extracted the email from the form? Good. Now give it to the subscribe method on the server and let it do it's thing.

Meteor.call('subscribe', data.email)

.gitignore

Ban git from knowing our harmless little secrets.

server/secrets.js

Magic sauce

Getting people to subscribe

People recognize good content and they do want to subscribe to your stuff. Sometimes you just need to remind them to do it. A great, fun article about human behaviour: http://blog.codinghorror.com/the-just-in-time-theory/


Oh, and don't forget to subscribe!