How to view email views in a browser using rails

I am working on emails for my rails application. Right now, the only way to find out the email is to send it to yourself. How to get "daily_summary.html.haml", which is located in the "notifications" folder in the "views" folder, for rendering in the browser? I thought I should just add a route:

match 'notifications' => 'notifications/daily_summary' 

But then I do not know how to handle the controller / action side.

+6
source share
4 answers

Since Rails 4.1, email preview is native. All you have 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 available 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 

See the ActionMailer API documentation for more information .

+10
source

There's a gem called the Letter Openener, which sounds like it will do exactly what you are looking for. It views email messages in a browser, rather than sending them. I did not use it myself. If this works, I would love to hear about it though!

https://github.com/ryanb/letter_opener

There is another called Mail Viewer, but it has not been actively developed for quite some time. Probably best avoided:

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

+8
source

I would look at actionmailer_extensions . It forces ActionMailer to write outgoing messages to disk as .eml files. This may be enough for your purposes (just configure the script to look at the output directory for new files and open them in your preferred mail client), or you can break the gem and change it directly (its source is simple) to write .html files and open them in your browser.

Hope this helps!

+1
source

For Rails 3, there is now the mail_view , which was included in Rails 4.1. Here is the setup link. This is pretty 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(' name@example.com ', 'Jill Smith') mail = UserMailer.forgot_password(user) end end 

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

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

+1
source

Source: https://habr.com/ru/post/908556/


All Articles