0

i have a rails model registrations that has the following fields

attr_accessible :address, :company, :name, :phone, :email

i have successfully been able to send a mail to the user via the email fielded in by the user and that works succesfully using action mailer

def create
    @registration = Registration.new(params[:registration])

    respond_to do |format|
      if @registration.save
        UserMailer.registration_confirmation(@registration).deliver

        format.html { redirect_to root_path, notice:"  Thanks! #{@registration.name}, Your registration have been
        confirmed & your seat reserved" }
        format.json { render :show, status: :created, location: @registration }
      else
        format.html { render action: "new" }
        format.json { render json: @registration.errors, status: :unprocessable_entity }
      end
    end
  end  

and the registration mailer is as thus

def registration_confirmation(registration)

    @registration = registration
      @greeting = "Hi"
    mail(to: @registration.email, subject: 'Welcome')
  end 

which works very well...

All i want to achieve is to be able to send a mail to another email address e.g (admin@gmail.com) stating that a user as registered and also showing the registration details ... thanks

Kadismile
  • 210
  • 3
  • 19
  • 1
    Take a look this answer http://stackoverflow.com/a/7437303/1297435, you can put emails to variable (array : `emails = [@regustraion.email, "admin@gmail.com"]`) and then call them on mail to `mail(to: emails, subject: 'Welcome')` – rails_id Feb 22 '15 at 20:55
  • thanks @anonymousxxx, what if i wanted the mail to the admin@gmail.com to have something different in it like the details of whom registerd another thing different in the mail of who registerd i.e registration.email.. any work around ? – Kadismile Feb 22 '15 at 21:47

1 Answers1

1

I would generate a new mailer specifically for notifications that should be sent to the administrator.

rails g mailer AdminMailer registration_notice

You could then edit the AdminMailer registration_notice to be similar to your UserMailer, but with a different recipient:

  def registration_notice(registration)
    @registration = registration
    mail(to: 'admin@gmail.com', subject: 'A new user has registered')
  end

Put whatever registration details you would like to include into the views for registration_notice.html.erb (or text).

Then just add the call to the mailer in the create action of the controller, right after the call to the UserMailer:

  def create
    @registration = Registration.new(params[:registration])

    respond_to do |format|
      if @registration.save
        UserMailer.registration_confirmation(@registration).deliver
        AdminMailer.registration_notice(@registration).deliver
        # etc
      end
    end
  end

You'd probably also want to consider sending the mails in the background instead of making the user wait for the create request to finish, but that's beyond the scope of this question.

keyzee
  • 441
  • 3
  • 7