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.
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
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:
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('name@example.com', '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.
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!
This is already contained in @Daniel Cukier's answer, but the super quick answer is just remember this one route ( \/rails\/mailers<\/code> ), ie:
http://localhost:3000/rails/mailers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.