Rails 4 - 如果发送给多个收件人,则不会传递邮件(使用Sendgrid)

时间:2016-03-09 05:29:28

标签: ruby-on-rails ruby actionmailer sendgrid

我想向我的用户发送一条消息,每天几次向我的应用程序注册。 这是我用于此的邮件程序方法:

  def notify_users(product)  
    @product
    emails  = Customer.signed_up.pluck(:email_address)
    puts "EMAILS:"
    puts emails.inspect # I see here, say, 5 email addresses
    emails.each do |email|
      unless email.blank?
        puts "Sending to #{email}" # email is displayed here properly
        mail(to: email, subject: "New products", from: 'no-reply@website.com') 
      end                     
    end

我发现如果emails只包含1个电子邮件地址,则电子邮件会在电子邮件地址中发送。但是,当我确实在多个电子邮件地址上发送电子邮件时,通常只会传递最后一条消息(因此如果emails继续["a@gmail.com", "b@gmail.com", "c@gmail.com"]),则电子邮件仅在c@gmail.com上发送。

为什么?试图找出原因,但仍然不成功。

1 个答案:

答案 0 :(得分:0)

试试这样:

def send_notification_to_user(email)
  puts "Sending to #{email}" # email is displayed here properly
  mail(to: email, subject: "New products", from:'no-reply@website.com') 
end

def notify_users(product)  
  @product
  emails  = Customer.signed_up.pluck(:email_address)
  puts "EMAILS:"
  puts emails.inspect # I see here, say, 5 email addresses
  emails.each do |email|
    unless email.blank?
      send_notification_to_user(email).deliver_now 
    end                     
  end
end