Use griddler to receive emails as http post events from Mandrill in Rails

Much of this post is based off of SendGrid’s great tutorial that can be found here. However, I found that some of the contents were outdated. Additionally, I wanted to use mandrill instead of SendGrid.

At times, it may be necessary to receive an email in your rails app as an incoming post event. Fortunately, this problem has been solved and the solution is relatively simple to implement.

Set up Griddler #

Add the griddler gem and the mandrill adapter to your gemfile:

gem 'griddler'

gem 'griddler-mandrill'

Now, we need to add just a little bit of code that griddler needs. First, set up the griddler routes. This is very simple; add mount_griddler to the top of your routes.rb

Next we need to set up a few griddler initialize parameters. Create a new file in config/initializers/griddler.rb and put this code in it:

griddler.rb

This code may be somewhat redundant because these are all the default settings griddler would use (with the exception of email_service, however it’s nice to have this here so if you need to change something later you know where to do it.

processor_class simply tells griddler which class to look at for the processor code.

processor_method specifies which method in the processor_class to hit when an email is received.

reply_delimiter is a string that griddler will search for in the body of the inbound email and automatically truncate any included reply text.

email_service is the only important thing here. This tells griddler with adapter you’re using. In our case, we’re using mandrill. It is important to note that you must have this set here as well as the gem installed. If you don’t, you will see some very weird errors that can be hard to trace.

Last, let’s setup our email_processor.rb class. Create a file in app/models/email_processor.rb and put this code in it:
email_processor.rb

This is simple enough. We want to put all of our email processing code inside the process method. Keep in mind from the initialize method we get access to @email which is a special Griddler::Email class from griddler. We have some pretty useful methods, and griddler’s github readme documents them well, so I’ll simply copy their docs here:
griddler methods

Get a public url #

To set up and test the mandrill integration, you’ll need a public url for your app. You can deploy it to Heroku, or use the amazing and recently re-written tool, local tunnel. Once you have it installed, simply start your rails server with rails s and in then run lt --port 3000 (or whatever port your server is running on). It will respond with a url that you can paste into your browser, and like magic your app is available!

Set up Mandrill #

MailChimp makes this as painless as it could possibly be.

First, set up your domain’s mx records to point to mandrill. Assuming you have a mandrill account and are logged in, hit this url: https://mandrillapp.com/inbound
Add a new inbound domain:
mandrill inbound
Your dns settings are invalid. You need to update your domain’s mx records. I can’t really go into detail about how to do that here, it will vary based on your registrar. Click dns settings to get the correct ip’s for your mandrill account though.
dns settings

Once set up correctly, you DNS settings should show up as MX: valid.
Now, click your domain name to set up your hook. Add a new route. Your settings will look similar to this:
route

Now the only thing left to do is send a test email from mandril. Save your settings and go back to your inbound domains. Click the test button, and if everything went well, you should see a message saying the test was successful!

Summary #

Thanks to Thoughtbot and their excellent gem, receiving incoming emails and post events is as simple as it could possibly be.

I didn’t play with any other adapters or email to post services, so I may not have much say on any questions regarding that. Otherwise, feel free to contact me on twitter @davidwdegraw with questions!

 
25
Kudos
 
25
Kudos

Now read this

Hints on Savon and Rails

So I had to use the Savon gem recently, and there’s a couple pitfalls that aren’t super well documented. There’s a couple great resources for learning however. Most notable are the Savon docs themselves and Ryan Bates’ RailsCasts. Keep... Continue →