Confirmation Emails & Testing with MailCatcher
Master Email Testing in Rails Development
Common Email Testing Challenges
Server Configuration
Setting up mail servers or finding reliable relays is complex and time-consuming. Improper configuration can lead to blacklisting.
Delivery Issues
Test emails frequently end up in spam folders or get filtered out by email providers before reaching their destination.
Limited Testing Options
Developers are typically restricted to sending emails to their own addresses or designated test accounts only.
Setting Up the Project Environment
Navigate to Class Files
Open Finder and locate the yourname-Rails Class folder, then open Terminal
Clone Repository
Remove existing nutty folder and clone fresh from Git repository
Install Dependencies
Run bundle and yarn install to set up all necessary gems and JavaScript dependencies
Install MailCatcher as a system-level gem by typing:
gem install mailcatcher
NOTE: Since MailCatcher operates independently of your Rails application, we install it globally rather than adding it to your Gemfile. This approach prevents development tools from polluting your application dependencies while ensuring MailCatcher remains available across all your Rails projects. The installation may take a few moments to complete.
Once installation completes, launch the MailCatcher service:
mailcatcherMailCatcher will display two important addresses upon startup. Note the web interface URL:
http://127.0.0.1:1080
This web interface provides access to all intercepted emails, while the SMTP server listens on port 1025 for outgoing mail from your applications.
Open a browser and navigate to either: http://127.0.0.1:1080 or localhost:1080
Explore MailCatcher's email management interface. Notice the clean, webmail-style layout that will display all intercepted emails with full headers, content preview, and source viewing capabilities. Now we need to configure Rails to route development emails through MailCatcher rather than attempting external SMTP delivery.
Keep MailCatcher open in your browser—we'll return to verify our email delivery shortly.
In your code editor, open nutty > config > environments > development.rb
NOTE: Rails provides three distinct runtime environments by default: development, test, and production. Each environment maintains completely separate configuration settings, allowing you to customize behavior for different deployment scenarios. During local development with rails server, we operate in development mode. This environment-specific approach lets us use MailCatcher locally while configuring real SMTP servers for production deployment. The test environment employs its own email testing strategy, storing messages in memory for automated verification.
In development.rb, locate the existing mailer configuration (around line 35) and add the highlighted code:
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
NOTE: This configuration directive instructs Rails to use SMTP (Simple Mail Transfer Protocol) for email delivery instead of the default sendmail approach. SMTP provides more reliable and configurable email transmission, especially when working with external mail services or local testing tools like MailCatcher.
Immediately below that line (around line 36), add the SMTP server configuration:
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }
NOTE: These settings point Rails to MailCatcher's SMTP listener. When MailCatcher started, it displayed smtp://127.0.0.1:1025, indicating the SMTP server endpoint. While production SMTP typically uses port 25 (similar to how web servers use port 80), MailCatcher uses port 1025 to avoid conflicts with system mail services and eliminate the need for administrative privileges.
Save and close the file. Your Rails application is now configured to route development emails through MailCatcher.
MailCatcher Installation Process
Install MailCatcher Gem
Run 'gem install mailcatcher' in Terminal - installation may take a minute
Start MailCatcher Service
Type 'mailcatcher' to start the service at http://127.0.0.1:1080
Configure Rails Environment
Update development.rb to use SMTP delivery method with localhost:1025
Rails Environment Configuration
| Feature | Development | Production |
|---|---|---|
| Mail Server | MailCatcher (localhost:1025) | Real SMTP server |
| Purpose | Local testing | Live email delivery |
| Configuration File | development.rb | production.rb |
A Rails mailer functions similarly to a controller - it prepares messages, sets up instance variables, and renders views containing the message content.
Building the Order Mailer
Generate Mailer Files
Use 'rails g mailer order_mailer' to create the necessary mailer structure
Configure Default Settings
Set the default from email address in application_mailer.rb
Create Email Views
Add both HTML and plain text versions of the confirmation email template
Always send both HTML and plain text versions of emails. Recipients' mail applications can choose which format to render based on user preferences.
Email Integration Checklist
Place OrderMailer call after order completion in the complete action
Ensures server recognizes the new mailer configuration
Add product to cart, checkout, and verify email appears in MailCatcher
Check that both HTML and plain text versions are delivered correctly
Key Takeaways