ApplicationMailer - thuy-econsys/rails_app GitHub Wiki

app/controllers/users/registrations_controller.rb

...

# POST /resource
  def create
    super
    if @user.errors.empty?
      AdminMailer.with(user: @user).admin_approval_email.deliver
    end
  end

...

end

app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: '[email protected]' 
  layout 'mailer'
end

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

app/mailers/admin_mailer.rb

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.

app/views/admin_mailer/admin_approval_email.html.erb

<h1><%= params[:user].email %> is awaiting admin approval</h1>

<p>An administrator can approve this registration by visiting the website and editing the user</p>

Rails Mailer Tutorial

⚠️ **GitHub.com Fallback** ⚠️