LetterOpenerDb
LetterOpenerDb is a Ruby gem that intercepts outgoing emails in your Rails application and stores them in a database for easy viewing, searching, and debugging. Inspired by letter_opener, this gem provides a convenient web interface instead of relying on local file system storage.
Motivation
While the original letter_opener gem saves emails as files, it has several limitations:
- No Search Functionality: Storing emails on the file system makes them difficult to search.
- Server Scalability Issues: If you run multiple servers (e.g., on a staging environment), emails are scattered across different file systems, making it impossible to view all captured emails in one place.
To address these issues, LetterOpenerDb saves the emails in a database, enabling:
- Search by subject, body, or other fields
- Centralized Access to all emails, even in multi-server environments
Features
-
Database Storage
All emails sent by ActionMailer are recorded in the database. -
Web Interface
Easily browse and inspect sent emails through an embedded Rails engine. -
Simple Search
Search by text in the subject or body of emails. -
Rails Engine
Includes models, controllers, and views for quick installation and easy maintenance.
Screenshot
Installation
Add this line to your application's Gemfile, pointing to your Git repository (private or public) or a local path:
gem 'letter_opener_db'Then execute:
bundle install
Setup
- Migrations Generate and run the migrations to create the letter_opener_db_mail_records table:
rails generate letter_opener_db:install
rails db:migrate- Delivery Method In your config/environments/development.rb (and/or config/environments/test.rb), set:
config.action_mailer.delivery_method = :letter_opener_db- Mount the Engine In your main config/routes.rb, add:
mount LetterOpenerDb::Engine, at: '/letter_opener_db'You can choose any path you prefer (e.g., '/mails').
Usage
- Send Emails as you normally would in development or test environments.
- Access the emails at http://localhost:3000/letter_opener_db/mail_records (or your chosen mount path).
- Browse and Search
- See all captured emails, sorted by send time.
- Click on a subject to view details (recipients, subject, body, etc.).
- Use the simple search box to filter emails by subject or body text.
Development
-
Clone or Fork the repository.
-
Edit the gem’s files in the lib and app directories as needed.
-
Test changes by running:
bundle exec rspec -
Optionally, add this gem as a path reference in another Rails app to test integration:
gem 'letter_opener_db', path: '../letter_opener_db'Contributing
Contributions are welcome! To submit a feature or fix:
- Fork this repository.
- Create a new branch (git checkout -b feature/my-feature).
- Commit your changes (git commit -m 'Add my feature').
- Push the branch (git push origin feature/my-feature).
- Open a Pull Request with a clear description of your changes.
License
This project is released under the MIT License.

