带有mailgun和rails的电子邮件未发送给所有用户

时间:2015-01-19 09:42:28

标签: ruby-on-rails email devise mailgun

我试图在每次发布博客帖子时向所有用户发送电子邮件。 Mailgun似乎只是向一个用户发送电子邮件。我正在制作这个。

我在邮件中的代码就是这个

 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

1 个答案:

答案 0 :(得分:2)

我认为这可以解决您的问题。

<强> 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

您可以尝试以下。

<强> 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