简体   繁体   English

"如何使用 Rails 在浏览器中查看电子邮件视图"

[英]How to see e-mail views in browser using rails

I am working on e-mails for my rails app.我正在为我的 rails 应用程序处理电子邮件。 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?如何让“views”文件夹下的“notifications”文件夹中的“daily_summary.html.haml”在浏览器中呈现? 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. 从Rails 4.1开始,电子邮件预览就是原生的。 All you need to do is create a class in this directory: 您需要做的就是在此目录中创建一个类:

test/mailers/previews/

The class must extend ActionMailer::Preview 该类必须扩展ActionMailer::Preview

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

Write methods that return Mail::Message objects. 编写返回Mail::Message对象的方法。 They are accessible in the development environment using this url: 可以使用此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 可以在ActionMailer API文档中找到更多信息

There's a gem called Letter Opener that sounds like it'll do exactly what you're looking for. 有一个名为Letter Opener的宝石,听起来就像你正在寻找的那样。 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 https://github.com/ryanb/letter_opener

There's another one called Mail Viewer but it hasn't been actively developed in quite some time. 还有一个名为Mail Viewer但在相当长的一段时间内没有积极开发。 Probably better to steer clear: 可能更好地避开:

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

For Rails 3 there is now a gem, mail_view , which was include in Rails 4.1. 对于Rails 3,现在有一个gem, mail_view ,它包含在Rails 4.1中。 Here is a link to set-up . 这是设置的链接。 It is quite easy. 这很容易。

1.) Add to Gemfile: 1.)添加到Gemfile:

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

2.) in routes.rb: 2.)在routes.rb中:

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

3.) Create a MailPreview model: 3.)创建MailPreview模型:

 # 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. 在这个模型中,您可以根据需要为方法命名,但它们与UserMailer方法相对应是有意义的。

4.) To view go to /mail_view for a list of all methods in MailPreview. 4.)要查看转至/mail_view以获取/mail_view中所有方法的列表。 Click on one to see the HTML preview right there in the browser. 单击其中一个以在浏览器中查看HTML预览。

I'd take a look at actionmailer_extensions . 我来看看actionmailer_extensions It makes ActionMailer write outgoing emails to the disk as .eml files. 它使ActionMailer将外发电子邮件作为.eml文件写入磁盘。 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. 这可能足以满足您的需要(只需设置一个脚本来查看新文件的输出目录并在您首选的电子邮件客户端中打开它们),或者您可以分叉gem并直接修改它(它的源代码很简单)来编写.html文件并在浏览器中打开它们。

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:这已经包含在@Daniel Cukier 的答案中,但超级快速的答案就是记住这条路线( \/rails\/mailers<\/code> ),即:

http://localhost:3000/rails/mailers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM