Shivam Daryanani's Blog

Receiving Email in a Rails App

Receiving email in a Rails app can be tricky. The Action Mailer library has minimal support for this and it can be quite tricky. This is where SendGrid’s parse API comes in along with the gem released by ThoughtBot called Griddler.

Setup

Start of by adding the griddler gem to your gemfile.

1
gem "griddler"

Followed by a bundle install. If you run rake routes, you will see Griddler has created a route to receive mails on. If you need to edit this route, you can paste it in your routes file.

config/routes.rb
1
post '/email_processor' => 'griddler/emails#create'

By default Griddler will look for an EmailProcessor class. You can create one in the models folder.

app/models/email_processor.rb
1
2
3
4
5
class EmailProcessor
  def self.process(email)
    #create models, process reports etc.
  end
end

These are the following attributes available on the email received.

1
2
3
4
5
6
7
8
9
10
.to 
.from
.subject
.body
.raw_text
.raw_html
.raw_body
.attachments
.headers
.raw_headers

Email server setup

For my app I ended up using SendGrid, which has an add-on for Heroku. Once your account is setup, go the parse webhook settings page.

In order for this to work you need a domain name. Your site doesn’t necessarily have to be deployed on it. Enter the domain you are going to use in the domain field. For the URI enter the route where you want to receive mails. I have used Ngrok as I wanted to test it in development.

The last part needed in order to receive emails is to point your domain’s MX record to, in this case, mx.sendgrid.net. What is a MX record? The full form is Mail Exchanger record. It specifies how email should be routed for a particular domain. It specifies a mail server, in this case sendgrid, for accepting emails for your domain. Go to your domain settings and create a MX record which points to mx.sendgrid.net. An example for Namecheap would look like this. You will also need to setup an email for your domain, by default namecheap creates an info@yourdomain.com.

And that’s it! Start your rails server along with ngrok or a localtunnel. Give it a go send an email to the address you created in the domain settings. In my case this would be info@shivamdaryanani.com and boom you should have received a post message from Sendgrid. You would want to put a debugger inside the method to play around with email object that Griddler gives you. The SendGrid API expects a 200 response from your endpoint. If it doesn’t get it or if your site is down, it tries up to 3 times in set intervals until it reaches your app. Which is great if your site is under maintenance.

Why would I want to receive email?

That’s a good question. A lot of sites create their support tickets in this way. Once you email support@domain.com, it parses the email and creates the ticket. Also here is a list of creative ideas:

Hope this was useful let me know if you come across any roadblocks.

Comments