Sending emails based on intervals using Ruby on Rails

前端 未结 5 686
北恋
北恋 2020-12-12 23:20

I would like to be able to send a string of emails at a determined interval to different recipients.

I assign to each Contact this series of Emails called a Campaign,

5条回答
  •  温柔的废话
    2020-12-12 23:24

    I would create a rake task in RAILS_ROOT/lib/tasks/email.rake

    namespace :email do
      desc "send emails to contacts"
      task :send do
        Email.all.each do |email|
          # if start_date is a datetime or timestamp column
          contacts = Contact.all(:conditions => ["DATE(start_date) = ?", email.days.days.ago.to_date])
          # if start_date is a date column
          contacts = Contact.all(:conditions => { :start_date => email.days.days.ago.to_date })
          contacts.each do |contact|
            #code to send the email
          end
        end
      end
    end
    

    Then I would use a cronjob to call this rake task every day at 3 a.m.:

    0 3 * * * app_user cd RAILS_APP_FOLDER && RAILS_ENV=production rake email:send
    

提交回复
热议问题