简体   繁体   中英

Rails Runner or Rake task to send one-off email to registered users

I'd like to send a one off email to users and, being new-ish to ActionMailer , not sure how to approach a one-off like this? Rails Runner , a Rake task or maybe even the rails console on the production machine?!?

Figure I'd add an action for it to user_mailer.rb . And create an accompanying view in app/views/user_mailer/ . Just not sure how to trigger it. I'd like to grab a few users and send to them in batches. Happy to do this manually...as we stagger sending the messages over the next few days/weeks.

Appreciate any suggestions or advice.

If you want to do this as automatedly as possible, I'd take a look at the whenever gem . It lets you automate rake (or runner) tasks as cron jobs. You can add a script somewhere in lib that uses ActionMailer to generate the emails, and schedule when they're sent with whenever/cron.

As far as sending in batches, I would do something like this:

Users.find_each(:batch_size => n) do |m|
  mail = UserMailer.new(m.email_address, message)
  mail.deliver
end

Obviously, that's near-pseudocode, but hopefully it puts you on the right track?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM