Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4 email preview in production

I am using rails 4.1.1 and ActionMailer::Preview for previewing emails. In development environment everything is working excellent.

But in production environment the preview routes are not accessible. I store the previews in test/mailers/previews/

Is is possible to enable them for production?

like image 848
hpeev Avatar asked Dec 12 '14 23:12

hpeev


People also ask

How do I preview a Rails email?

rails generates a mail preview if you use rails g mailer CustomMailer . You will get a file CustomMailerPreview inside spec/mailers/previews folder. Here you can write your method that will call the mailer and it'll generate a preview. Save this answer.

What is ActionMailer?

Action Mailer is the Rails component that enables applications to send and receive emails. In this chapter, we will see how to send an email using Rails. Let's start creating an emails project using the following command. tp> rails new mailtest. This will create the required framework to proceed.


1 Answers

To do it without opening a big security hole:

production.rb

MyApp::Application.configure do
  config.action_mailer.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/spec/mailer_previews" : nil
  config.autoload_paths += [config.action_mailer.preview_path]

  routes.append do
    get '/rails/mailers'         => "rails/mailers#index"
    get '/rails/mailers/*path'   => "rails/mailers#preview"
  end
end

class ::Rails::MailersController
  before_filter :authenticate_admin!
  def local_request?
    true
  end
  private
  def authenticate_admin!
    ...
  end
end
like image 73
Michael Johnston Avatar answered Oct 05 '22 01:10

Michael Johnston