未分类 · 2016年4月7日 0

Rails Preview Emails

Previewing emails

You can preview your email templates visually by adding a mailer preview file to the

ActionMailer::Base.preview_path. Since most emails do something interesting with database data, you’ll need to write some scenarios to load messages with fake data:

#RailsApp/lib/mailer_previews/notifiler_preview.rb
class NotifierPreview < ActionMailer::Preview
  def welcome
    Notifier.welcome(User.first)
  end
end

Methods must return a Mail::Message object which can be generated by calling the mailer method without the additional deliver. The location of the mailer previews directory can be configured using the preview_path option which has a default of test/mailers/previews:

config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"

An overview of all previews is accessible at http://localhost:3000/rails/mailers on a running development server instance.

Previews can also be intercepted in a similar manner as deliveries can be by registering a preview interceptor that has a previewing_email method:

class CssInlineStyler
  def self.previewing_email(message)
    # inline CSS styles
  end
end

config.action_mailer.preview_interceptors :css_inline_styler

Note that interceptors need to be registered both with register_interceptor and register_preview_interceptor if they should operate on both sending and previewing emails.