1

I have and admin_mailer:

class AdminMailer < ActionMailer::Base
 def send_message_to_all_users(user, subject, body, locale)
    @user = user
    @body = body
    @locale = locale
    mail(:to => user.email, :subject => subject)
  end
end

Inside my action on controller user:

def send_email_to_all_users

   users = User.all
   subject = params[:subject]
   body = params[:body]
   locale = params[:locale]
   for user in users
    User.delay(queue: "Email", priority: 20).send_message_to_all_users(user, subject, body, locale)
   end

   respond_to do |format|
    .........
   end
end

In my model User

class User
 include Mongoid::Document
 include Mongoid::Timestamps::Created
.
.
.

#delayed jobs methods
  def self.send_message_to_all_users(user, subject, body, locale)
     AdminMailer.send_message_to_all_users(user, subject, body, locale).deliver
  end

 def deliver
  sleep 15
 end

end

Mailing list of users is very long over 20.000 emails account.

My question is if is correct this way to send newsletter with a big mailing list, or I can do this task of better way.

Thank you!

hyperrjas
  • 10,666
  • 25
  • 99
  • 198
  • If you are locked into using delayed_job, take a look at this [railscast](http://railscasts.com/episodes/171-delayed-job). The cast specifically addresses delayed mailing. – eabraham Nov 28 '12 at 17:30

2 Answers2

0

Look at resque_mailer, and this question

Community
  • 1
  • 1
just so
  • 1,088
  • 2
  • 11
  • 23
  • Thank you but I am using `delayed_job` and add other background-job gem is not possible. Thank you very much! – hyperrjas Nov 27 '12 at 13:31
0

I think you should look into the find_in_batches method here: http://guides.rubyonrails.org/active_record_querying.html

In your case, you could first, find all users in batches and from there, assign each batch to your send_email_to_all_users method call (replacing the User.all with each batch.

Or, even better, you can move the entire newsletter process into its own class and encapsulate it a little (thus, still calling the method from the controller but running all of the logic in the class.)

Anyway, I think find_in_batches is what you need.

Angelo Chrysoulakis
  • 846
  • 2
  • 7
  • 21