L
L
lumino42014-04-01 04:21:21
Ruby on Rails
lumino4, 2014-04-01 04:21:21

Bulk Send Emails Rails. Why is the email sent only to the last user from User.all?

def alarm_email() # TODO в будущем обрабатывать через пакеты (User.find_each)
  delivery_options = {
      address: "smtp.mail.ru",
      port: 465,
      domain: "mail.ru",
      authentication: :login,
      tls: true,
      user_name: "[email protected]",
      password: "password",
      enable_starttls_auto: true
  }
  for user in User.all
    #puts user.email
    mail(to: user.email,
         subject: "Subject",
         delivery_method_options: delivery_options
    )
    #sleep (Random.new.rand(3.0..8.0))
  end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Platonov, 2014-04-01
@lumino4

# controller
User.all.pluck(:email).each do |email|
  NoticeMailer.alarm_email(email).deliver
end

# NoticeMailer
def alarm_email(email)
  # Эти настройки в конфиг нужно вынести
  delivery_options = {
      address: "smtp.mail.ru",
      port: 465,
      domain: "mail.ru",
      authentication: :login,
      tls: true,
      user_name: "[email protected]",
      password: "password",
      enable_starttls_auto: true
  }

  mail to: email, subject: "Subject", delivery_method_options: delivery_options
end

And I highly recommend https://github.com/collectiveidea/delayed_job or equivalents for such tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question