August 10, 2018

How to Receive Emails in Your Rails Application

There are times when a Rails app needs to receive emails to process the content, or grab the file from the attachment and process data within the file. In this case griddler gem would be the best bet to parse the incoming emails with its built-in controller action, but it seems tricky to follow through the documentations and I have to adapt an apparent syntax change which caused an issue if I follow the instructions blindly. Here is my version of the guide, hope you find it helpful.

The first step is that you’ll need to find an email deliver service provider to help receive and send your emails. Mailgun and SendGrid would be a good choice as they are free to use for small personal projects. After that, follow through the instructions as given by the service so that they could properly receive and send emails.

After that, you’ll need to setup the service to receive emails and “forward” it to your app. To test this out, you’ll need to expose your local development web server to the public through services such as localtunnel or ngrok. By default griddler gem handles the incoming emails in /email_processor route, so your setup should include a setting to route your emails through HTTP request to your local web server, such as

Now finally it’s time to dive in the code. The code snippets below are based on Mailgun service that I configured personally, feel free to substitute with other adapters available to griddler.

Add these gems to your Gemfile:

gem 'griddler'
gem 'griddler-mailgun'

And configure your griddler to use the adapter of your choice:

Griddler.configure do |config|
  config.email_service = :mailgun

Add mount_griddler for the simplest configuration provided by Griddler gem by default. This handles /email_processor route that is handled by Griddler::EmailsController controller provided by the gem.

Rails.application.routes.draw do
  # ...

Add new file named app/models/email_processor.rb with the following code as a template. Note that this section is different from other guides as there seems to be a new syntax being introduced in the gem, as now an instance of EmailProcessor class will be created before process instance method is called (used to be a class method).

class EmailProcessor
  def initialize(email)
    @email = email

  def process
    puts "received email from #{@email.from}:"
    p @email.body

Now you can try send an email to your configured domain and observe your local webserver to make sure everything works fine. Advanced configurations (such as handling the route manually or customize your email processor class) is available at the gem README.

Hope this clarifies out the way to receive emails from your Rails app.

External Readings:

  1. Receiving Email in Your Rails App With Griddler
  2. Griddler is Better Than Ever!
  3. griddler gem
© 2022, Built with Gatsby