home

Using PostMark To Send Mail

Jul 21, 2010

I think a lot of developers first reaction to having to send mail from an app would be to setup an stmp server and fire emails off (ok, well that might be the second reaction - the first reaction might be ughhh). That's certainly the approach I initially took to handle registration confirmation for our new gaming website.

But a couple people, including my co-developer Aaron Pepper, insisted that making use of PostMark to handle sending our email would be a better solution. Now, let me tell you that my first reaction was absolutely not. I mean, why would you outsource and pay for something as basic as sending out email, right?

Well it turns out that I've since seen the light and I think you should too. What we were doing, and what a lot of places do, is send out a confirmation email with an activation link. Its a pretty well-understood paradigm - even for non computer savvy users, but its still somewhat convoluted and unfriendly. Worse, for a young site like ours, desperate for new content, the disconnect is likely to turn some users off.

This is where PostMark comes in. With their simple API we're able to send off email - so far so good. But their bounce API is where things get really interested. We chose to use their callback (or hook) capability. Essentially, whenever PostMark fails to deliver a message, they hit our server (address is specified by us in their web interface and uses basic authentication) and pass the relevant information. Here's what our rails code looks like to handle this callback:

def bounce
   user = User.find_by_field(:email, params[:Email])
   user.invalid_email unless user == nil
   render :nothing => true
end

Pretty simple, right? The real shift though isn't with the code, its with how we handle user registration. Lets be honest, for a game tip website, having valid email isn't the most crucial thing. So we let users register and start using the site right away. If we get a bounce, we update the user record. Exactly what you do at this point is up to you - you might politely ask them to update their information, or be more draconian and block them from doing anything until they provide a valid email.

The end result is that our website flow is friendlier. Users can start using the site right away and our welcome email is just a welcome email - no special activation instructions, no huge deal if it get marked as spam. I'd say that for most websites, its the right model to use. PostMark costs $1.50 per 1000 emails (with bulk pricing available), has libraries for most major frameworks, and provides a rich API (you can query a webservice for bounces rather than have them callback).

My only complaint is that they don't have a Rails 3 library yet. While Rails 3 might be in beta, it'd still be nice to have. So I wasn't able to hook into the beautiful rails mailing api. Instead I had to use PostMark's ruby (not rails) library directly.

For the curious, since we are using nginx as our webserver, here's what our location confirguration looks like for this specific URL (to enable basic authentication):

location /users/bounce {
   auth_basic "auth";
   auth_basic_user_file "htpasswd";
   root   /www/noobgaming/current/public;
   passenger_enabled on;
}

The htpasswd file is the typical apache format - you can use this htpasswd tool with the crypt algorithm