Track Sent Emails in a Rails Application Using Open Source
Wouldn't it be great to have the functionality of a regular mailbox within your Rails application? If this thought has ever crossed your mind, we have good news. Our Ruby developer, Anton Bogdanov, created an email tracker called Emailbutler for Rails applications, which logs all emails sent through Action Mailer. The tracker comes with a user-friendly interface that allows you to sort and search through emails.
The main feature of Emailbutler is the ability to log all outgoing emails. If an email fails to send due to an error, it won't appear in the list. The UI allows you to easily work with the email list, filter, and search by recipients and actions.
Another useful feature is the collection of data from SMTP providers. The interface shows whether the email has been sent to the provider, delivered to the recipient, or opened. Spam markings are also displayed in the interface. At the time of publication, Emailbutler supports tracking the delivery status of emails sent through Sendgrid, SMTP2GO, Resend, and Mailjet.
Additionally, with Emailbutler, you can resend an email without manually re-entering the parameters. The interface includes a "Resend" button, which sends the email with the exact same parameters as the first time.
Installation
Add the following line to your application's Gemfile:
gem 'emailbutler'
gem 'pagy'
Then run:
$ bundle install
$ rails g emailbutler:active_record
$ rails db:migrate
Configuration
Initializer
Add the following configuration line to config/initializers/emailbutler.rb:
For Active Record:
require 'emailbutler/adapters/active_record'
Emailbutler.configure do |config|
config.adapter = Emailbutler::Adapters::ActiveRecord.new # required
config.providers = %w[sendgrid smtp2go resend mailjet] # optional
config.ui_username = 'username' # optional
config.ui_password = 'password' # optional
config.ui_secured_environments = ['production'] # optional
end
Routes
Add this line to config/routes.rb:
mount Emailbutler::Engine => '/emailbutler'
UI Styles
To add styles for the UI, add the following line to assets/config/manifest.js:
//= link emailbutler.css
In some cases, you can specify this in assets/javascript/application.js:
//= require emailbutler_manifest
Mailers
Update the application's mailer:
class ApplicationMailer < ActionMailer::Base
include Emailbutler::Mailers::Helpers
end
Webhook Settings for Providers
Sendgrid
- Go to mail settings,
- Enable Event Webhook,
- In the HTTP POST URL field, insert the webhook controller URL of your application (
host/emailbutler/webhooks/sendgrid
), - Select all delivery data,
- Save settings.
A similar procedure applies to other supported providers.
Usage
Each email-sending event will create a new record with the message parameters in the database. Each webhook event will update the message status in the database.
Future Plans
All the core features of Emailbutler are already implemented. We plan to clean up the logs and add new services for mail delivery. Currently, the tracker works with four providers: Sendgrid, SMTP2GO, Resend, and Mailjet.