Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 997
John Avatar asked Feb 15 '12 21:02

John


People also ask

What is ActionMailer?

Action Mailer allows you to send emails from your application using mailer classes and views.

How do I send an email to ROR?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.


2 Answers

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

like image 195
Daniel Cukier Avatar answered Sep 17 '22 11:09

Daniel Cukier


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

like image 25
Tom L Avatar answered Sep 21 '22 11:09

Tom L