【问题标题】:e-mail with mailgun and rails not sent to all users带有 mailgun 和 rails 的电子邮件未发送给所有用户
【发布时间】:2015-03-17 06:46:58
【问题描述】:

每次发布​​博客文章时,我都会尝试向所有用户发送电子邮件。 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

【问题讨论】:

  • 能否请您发布调用此邮件方法的代码
  • 当然,我编辑了问题

标签: ruby-on-rails email devise mailgun


【解决方案1】:

我想这会解决你的问题。

ma​​iler.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

您可以尝试关注。

ma​​iler.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

【讨论】:

  • 您好,我会接受您的回答,使用 mailgun 日志中的第二个选项,我看到它会收到所有用户的电子邮件,但无法连接到 yahoo、gmail 等客户端,所以我向他们提交了一张票。谢谢
  • 我如何检查邮件的状态是发送还是不发送或退回邮件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 2013-12-03
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多