Search code examples
ruby-on-railsactionmailer

Rails ActionMailer: Send email only if @user has is_admin also


I have this set up for sending an email to a new user after they register. Right now it sends an email to ANY new user.

class UserMailer < ActionMailer::Base
  def welcome_email(user)
  @user = user
  mail(to: @user.email,
       from: "Thinkrtc",
       subject: "Welcome To Thinkrtc"
    )
  end
end

I also have a second way a user can sign up, which has a is_admin boolean. So, when they sign up in the second way, they get the is_admin is true boolean.

I want to set up my UserMailer up there to only send it to the people who creates a new account on the second way and gets the is_admin = "true".


Solution

  • class UserMailer < ActionMailer::Base
      def welcome_email(user)
        @user = user 
        if @user.is_admin
          mail(to: @user.email,
             from: "Thinkrtc",
             subject: "Welcome To Thinkrtc"
          )
        end
      end
    end
    

    This will send an email if the user has is_admin == true.