Ruby-on-rails 如何在 Rails 中预览电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7165064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I preview emails in Rails?
提问by Peter Nixey
This might be a dumb question but when I'm putting together an HTML email in Rails, is there a particularly easy built-in way to preview the template it in the browser or do I need to write some sort of custom controller that pulls it in as its view?
这可能是一个愚蠢的问题,但是当我在 Rails 中整理 HTML 电子邮件时,是否有一种特别简单的内置方法可以在浏览器中预览模板,或者我是否需要编写某种自定义控制器来提取它在它看来?
回答by benjaminjosephw
Action Mailer now has a built in way of previewing emails in Rails 4.1. For example, check this out:
Action Mailer 现在有一种在Rails 4.1中预览电子邮件的内置方式。例如,看看这个:
# located in test/mailers/previews/notifier_mailer_preview.rb
class NotifierPreview < ActionMailer::Preview
# Accessible from http://localhost:3000/rails/mailers/notifier/welcome
def welcome
Notifier.welcome(User.first)
end
end
回答by cailinanne
Daniel's answeris a good start, but if your email templates contain any dynamic data, it won't work. E.g. suppose your email is an order receipt and within it you print out @order.total_price- using the previous method the @ordervariable will be nil.
Daniel 的回答是一个好的开始,但如果您的电子邮件模板包含任何动态数据,它将无法工作。例如,假设您的电子邮件是订单收据,并在其中打印出来@order.total_price- 使用前面的方法,@order变量将为 nil。
Here's a little recipe I use:
这是我使用的一个小食谱:
First, since this email preview functionality is definitely for internal use only, I set up some generic routes in the admin namespace:
首先,由于此电子邮件预览功能绝对仅供内部使用,因此我在 admin 命名空间中设置了一些通用路由:
#routes.rb
MySite::Application.routes.draw do
namespace :admin do
match 'mailer(/:action(/:id(.:format)))' => 'mailer#:action'
end
end
Next, I create the controller. In this controller, I create one method per email template. Since most emails contain dynamic data, we need to populate whatever member variables the template expects.
接下来,我创建控制器。在这个控制器中,我为每个电子邮件模板创建一个方法。由于大多数电子邮件包含动态数据,我们需要填充模板期望的任何成员变量。
This could be done with fixtures, but I typically prefer to just grab some pseudo-random real data. Remember - this is NOT a unit test - this is purely a development aid. It doesn't need to produce the same result every single time - in fact - it's probably better if it doesn't!
这可以通过装置来完成,但我通常更喜欢只获取一些伪随机的真实数据。请记住 - 这不是单元测试 - 这纯粹是一种开发帮助。它不需要每次都产生相同的结果 - 事实上 - 如果不这样做可能会更好!
#app/controllers/admin/mailer_controller.rb
class Admin::MailerController < Admin::ApplicationController
def preview_welcome()
@user = User.last
render :file => 'mailer/welcome.html.erb', :layout => 'mailer'
end
end
Note that when we render the template, we use layout=>:mailer. This embeds the body of your email inside the HTML email layout that you've created instead of inside your typical web application layout (e.g. application.html.erb).
请注意,当我们渲染模板时,我们使用layout=>:mailer. 这会将您的电子邮件正文嵌入到您创建的 HTML 电子邮件布局中,而不是嵌入到典型的 Web 应用程序布局(例如application.html.erb)中。
And that's pretty much it. Now I can visit http://example.com/admin/mailer/preview_welcometo preview change to my welcome email template.
仅此而已。现在我可以访问http://example.com/admin/mailer/preview_welcome预览我的欢迎电子邮件模板的更改。
回答by Marc
回答by Galen
The easiest setup I've seen is MailCatcher. Setup took 2 minutes, and it works for new mailers out of the box.
我见过的最简单的设置是MailCatcher。设置需要 2 分钟,它适用于开箱即用的新邮件。
回答by pisaruk
I use email_preview. Give it a try.
我使用email_preview。试一试。
回答by markets
I recently wrote a gem named Mailyto preview, edit (template file) and deliver the application emails via a browser. It also provides a friendly way to hook data, a flexible authorization system and a minimalist UI.
我最近编写了一个名为Maily的 gem ,用于通过浏览器预览、编辑(模板文件)和发送应用程序电子邮件。它还提供了一种友好的数据挂钩方式、灵活的授权系统和极简的 UI。


I have planned to add new features in the near future, like:
我计划在不久的将来添加新功能,例如:
- Multiple hooks per email
- Parametrize emails via UI (arguments of mailer method)
- Play with translations keys (list, highlight, ...)
- 每封电子邮件有多个钩子
- 通过 UI 参数化电子邮件(邮件程序方法的参数)
- 使用翻译键(列表、突出显示、...)
I hope it can help you.
我希望它能帮助你。
回答by glebm
You can use Rails Email Preview
您可以使用Rails 电子邮件预览


REPis a rails engine to preview and test send emails, with I18n support, easy premailer integration, and optional CMS editing with comfortable_mexican_sofa.
REP是Rails引擎预览和测试发送电子邮件,以国际化支持,容易premailer整合,以及可选的CMS与编辑comfortable_mexican_sofa。
回答by Carma Tec
Rails Email Preview helps us to quickly view the email in web browser in development mode.
Rails 电子邮件预览帮助我们在开发模式下快速查看 Web 浏览器中的电子邮件。
1) Add “gem ‘rails_email_preview', ‘~> 0.2.29' “to gem file and bundle install.
1) 添加“gem ‘rails_email_preview', ‘~> 0.2.29' “到 gem 文件并捆绑安装。
2) Run “rails g rails_email_preview:install”this creates initializer in config folder and add routes.
2)运行“rails g rails_email_preview:install”它在配置文件夹中创建初始化程序并添加路由。
3) Run “rails g rails_email_preview:update_previews”this crates mailer_previews folder in app directory.
3)“rails g rails_email_preview:update_previews”在 app 目录中运行这个 crates mailer_previews 文件夹。
Generator will add a stub to each of your emails, then u populate the stub with mock data.
Generator 将为您的每封电子邮件添加一个存根,然后您用模拟数据填充存根。
Ex:
前任:
class UserMailerPreview
def invitation
UserMailer.invitation mock_user(‘Alice'), mock_user(‘Bob')
end
def welcome
UserMailer.welcome mock_user
end
private
def mock_user(name = ‘Bill Gates')
fake_id User.new(name: name, email: “user#{rand 100}@test.com”)
end
def fake_id(obj)
obj.define_singleton_method(:id) { 123 + rand(100) }
obj
end
end
4) Parameters in search query will be available as an instance variable to preview class. Ex: if we have a URL like
“/emails/user_mailer_preview-welcome?user_id=1”@user_idis defined in welcome method of UserMailerPreviewit helps us to send mail to specific user.
4) 搜索查询中的参数将可用作预览类的实例变量。例如:如果我们“/emails/user_mailer_preview-welcome?user_id=1”@user_id在welcome 方法中定义了一个URL
,UserMailerPreview它可以帮助我们向特定用户发送邮件。
class UserMailerPreview
def welcome
user = @user_id ? User.find(@user_id) : mock_user
UserMailer.welcome(user)
end
end
5) To access REP url's like this
5) 像这样访问 REP url
rails_email_preview.rep_root_url
rails_email_preview.rep_emails_url
rails_email_preview.rep_email_url(‘user_mailer-welcome')
6) We can send emails via REP, this will use environment mailer settings. Uncomment this line in the initializer to disable sending mail in test environment.
6) 我们可以通过 REP 发送电子邮件,这将使用环境邮件程序设置。在初始化程序中取消注释此行以禁用在测试环境中发送邮件。
config.enable_send_email = false
Source : RailsCarma Blog : Previewing Emails in Rails Applications With the Mail_View Gem
回答by KurtPreston
I'm surprised no one's mentioned letter_opener. It's a gem that will render and open emails as a browser page whenever an email is delivered in dev.
我很惊讶没有人提到letter_opener。这是一个 gem,每当电子邮件在 dev 中发送时,它都会将电子邮件作为浏览器页面呈现和打开。
回答by Swaps
railsgenerates a mail preview if you use rails g mailer CustomMailer.
You will get a file CustomMailerPreviewinside spec/mailers/previewsfolder.
rails如果您使用rails g mailer CustomMailer. 你会得到一个文件CustomMailerPreview里spec/mailers/previews的文件夹。
Here you can write your method that will call the mailer and it'll generate a preview.
在这里你可以编写你的方法来调用邮件程序,它会生成一个预览。
For ex -
对于前 -
class CustomMailerPreview < ActionMailer::Preview
def contact_us_mail_preview
CustomMailer.my_mail(user: User.first)
end
end
Preview all emails at http://localhost:3000/rails/mailers/custom_mailer

