LetterOpenerWeb::Redis
Redis storage backend for letter_opener_web.
This gem allows you to store email letters and attachments in Redis instead of the local filesystem, enabling multi-instance deployments and shared email viewing across application servers.
Installation
Add these lines to your application's Gemfile:
gem 'letter_opener_web'
gem 'letter_opener_web-redis', require: 'letter_opener_web_redis'Configuration
1. Configure Redis Connection
Set up your Redis connection in your Rails configuration file (e.g., config/environments/development.rb):
# Require the gem first
require 'letter_opener_web_redis'
LetterOpenerWeb.configure do |config|
# Use the Redis storage backend
config.letter_class = LetterOpenerWeb::RedisLetter
# Redis Configuration
config.redis_url = ENV.fetch('REDIS_URL', 'redis://localhost:6379/0')
config.redis_namespace = 'letter_opener' # optional, defaults to 'letter_opener'
end2. Configure ActionMailer
Update your ActionMailer configuration to use the Redis delivery method:
config.action_mailer.delivery_method = :letter_opener_web_redisStorage Structure
Emails are stored in Redis with the following key structure:
{namespace}:letters:ids # Set of all letter IDs
{namespace}:letter:{id}:metadata # JSON metadata (id, sent_at, attachments)
{namespace}:letter:{id}:plain # Plain text HTML
{namespace}:letter:{id}:rich # Rich HTML
{namespace}:letter:{id}:attachment:{filename} # Base64-encoded attachment content
Where {namespace} defaults to letter_opener but can be customized via configuration.
Memory Considerations
Since Redis stores all data in memory, be mindful of:
- Email Size: Large attachments will consume significant memory
- Retention: Consider implementing TTL (time-to-live) for old emails
- Limits: Monitor Redis memory usage and set appropriate limits
License
The gem is available as open source under the terms of the MIT License.