'How to see e-mail views in browser using rails

I am working on e-mails for my rails app. Right now the only way I know to view the e-mail is to send it to myself. How do I get "daily_summary.html.haml", which is in the "notifications" folder under the "views" folder, to render in the browser? I was thinking I should just add the route:

match 'notifications' => 'notifications/daily_summary'

But then I don't know how to handle the controller/action side of things.



Solution 1:[1]

Since Rails 4.1, e-mail preview is native. All you need to do is create a class in this directory:

test/mailers/previews/

The class must extend ActionMailer::Preview

class WeeklyReportPreview < ActionMailer::Preview
    def weekly_report
        WeeklyReport.weekly_report(User.first)
    end
end

Write methods that return Mail::Message objects. They are accessible in the development environment using this url:

http://localhost:3000/rails/mailers/[preview_class_name]/[method_name]

In my case:

http://localhost:3000/rails/mailers/weekly_report/weekly_report

More info can be found in the ActionMailer API documentation

Solution 2:[2]

There's a gem called Letter Opener that sounds like it'll do exactly what you're looking for. It previews email messages in the browser rather than sending them. I haven't used it myself. If it works I'd love to hear about it, though!

https://github.com/ryanb/letter_opener

There's another one called Mail Viewer but it hasn't been actively developed in quite some time. Probably better to steer clear:

https://github.com/37signals/mail_view

Solution 3:[3]

I'd take a look at actionmailer_extensions. It makes ActionMailer write outgoing emails to the disk as .eml files. This might be enough for your purposes (just set up a script to watch the output directory for new files and open them in your preferred email client), or you could fork the gem and modify it directly (its source is dead simple) to write .html files and open them in your browser instead.

Hope that helps!

Solution 4:[4]

For Rails 3 there is now a gem, mail_view, which was include in Rails 4.1. Here is a link to set-up. It is quite easy.

1.) Add to Gemfile:

gem 'mail_view', :git => https://github.com/basecamp/mail_view.git'
# or
gem "mail_view", "~> 2.0.4"

2.) in routes.rb:

 # config/routes.rb
 if Rails.env.development?
    mount MailPreview => 'mail_view'
 end

3.) Create a MailPreview model:

 # app/mailers/mail_preview.rb or lib/mail_preview.rb
class MailPreview < MailView
...
 def forgot_password
   user = Struct.new(:email, :name).new('[email protected]', 'Jill Smith')
   mail = UserMailer.forgot_password(user)
 end
end

In this model you can name the methods whatever you want, but it makes sense that they correspond to UserMailer methods.

4.) To view go to /mail_view for a list of all methods in MailPreview. Click on one to see the HTML preview right there in the browser.

Solution 5:[5]

This is already contained in @Daniel Cukier's answer, but the super quick answer is just remember this one route (/rails/mailers), i.e.:

http://localhost:3000/rails/mailers

This gives access to an index of all the mailers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Cukier
Solution 2 Tom L
Solution 3 Jordan Running
Solution 4 kburbach
Solution 5 stevec