0

I am trying to send all users an email every time a blog post is posted. Mailgun seems to deliver the email but always just to one user. I am running this in production.

my code in the mailer is this

 def new_record_notification(users)
      @users = User.all
        @users.each do |user| 
            mail to: user.email, subject: "Success! You did it."
        end
      end
    end

pins_controller.rb

 # POST /pins
  # POST /pins.json
  def create
    @pin = current_admin.pins.new(pin_params)

    respond_to do |format|
      if @pin.save
        ModelMailer.new_record_notification(@record).deliver
        format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
        format.json { render action: 'show', status: :created, location: @pin }
      else
        format.html { render action: 'new' }
        format.json { render json: @pin.errors, status: :unprocessable_entity }
      end
    end
  end
Scoala
  • 150
  • 11

1 Answers1

2

I think this will resolve your problem.

mailer.rb

def new_record_notification(user)
  mail to: user.email, subject: "Success! You did it."
end

pins_controller.rb

def create
    @pin = current_admin.pins.new(pin_params)

    respond_to do |format|
      if @pin.save
        @users = User.all
        @users.each do |user| 
          ModelMailer.new_record_notification(user).deliver
        end
        format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
        format.json { render action: 'show', status: :created, location: @pin }
      else
        format.html { render action: 'new' }
        format.json { render json: @pin.errors, status: :unprocessable_entity }
      end
    end
  end

OR

You can try following.

mailer.rb

def new_record_notification(users)
  recipients = User.all.collect{ |user| user.email }
  mail to: recipients, subject: "Success! You did it."
end

pins_controller.rb

def create
  @pin = current_admin.pins.new(pin_params)
  respond_to do |format|
    if @pin.save
      ModelMailer.new_record_notification(@record).deliver
      format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
      format.json { render action: 'show', status: :created, location: @pin }
    else
      format.html { render action: 'new' }
      format.json { render json: @pin.errors, status: :unprocessable_entity }
    end
  end
end
Amit Sharma
  • 3,427
  • 2
  • 15
  • 20
  • Hi I will accept your answer, with the second option in mailgun log I see it recieves all users emails but fails in connecting to yahoo, gmail etc. clients so I submitted a ticket to them. Thanks – Scoala Jan 19 '15 at 11:15
  • how can i check the status of mail is send or not send or bounce mail. – Shailendr singh Jan 22 '15 at 08:22