ActionMailer - thuy-econsys/rails_app GitHub Wiki

letter_opener gem

For previewing mailers locally in the browser and check if you have mostly wired everything up correctly.

Add gem to your development group in your Gemfile and then run bundle install. Then go into config/environments/development.rb and configure the development settings:

config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true

ActionMailer::Preview

Create a class inside the spec/mailers/previews/ directory that inherits from ActionMailer::Preview and define a method to invoke mailer's mailer action. In this case, it's ExampleMailer and welcome_email, respectively:

class ExampleMailerPreview < ActionMailer::Preview
  def welcome_email_preview
    ExampleMailer.with( user: User.first ).welcome_email
  end
end

The default directory of ActionMailer::Preview is test/mailers/previews/. But if you're not using the MiniTest suite, it's possible to redirect to spec/mailers/previews/ in the config/application.rb:

config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"

Now you should be able to go to http://localhost:3000/rails/mailers/example_mailer/welcome_email_preview to see a preview of your email and change it as needed.

Customizing

app/mailers/
├── admin_mailer.rb
└── application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: '[email protected]' 
  layout 'mailer'
end

In the Users::RegistrationsController#create method, the AdminMailer passes a key value pair (user: @user) to .with, which creates a params[:user] for the mailer action admin_approval_email to use, similar to how controllers have access to params.

class AdminMailer < ApplicationMailer 
  def admin_approval_email
    mail(
      to: params[:user].email,
      subject: 'Welcome to my Rails App')
  end
end

admin_approval_email returns an ActionMailer::MessageDelivery object which is a wrapper around a Mail::Message object. ActionMailer::MessageDelivery has a message method for inspecting or altering the Mail::Message object.

Views

app/views/admin_mailer/
├── admin_approval_email.html.erb
└── admin_approval_email.text.erb
<p>Thank you for signing up to our website, <%= params[:user].full_name %>.</p>

<p>The sign up was successful. However, we could not sign you in because your account is not yet activated.</p>

<p>
Respectfully,</br>
The Administrators</br>
</p>

It's possible to also use params[:user] here to access the user object.

Running rails generate devise:views generates a lot of views with views subdivided in their own directories, under app/views/devise/, including a devise/mailer subdirectory. Note that the custom mailer is located in app/views/ which is where the views are located if you run rails generate mailer CustomMailer per ActionMailer documentation.

NOTE: Possibly need to keep track of this and ensure that Devise does not override other mailer views.


You will get a translation missing: en.devise.failure.user.not_approved alert if you don't add a proper alert to your config/locales/devise.en.yml file:

en:
  devise:
...  
    failure:
...
      user:
        not_approved: "Your account has not been approved by your administrator yet."
⚠️ **GitHub.com Fallback** ⚠️