Sending emails based on intervals using Ruby on Rails

前端 未结 5 687
北恋
北恋 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 use DelayedJob for this ( assuming you are not sending large number of emails emails a day, i.e. 100's of thousands per day etc.)

    class Email < ActiveRecord::Base
      belongs_to :campaign
      after_create :schedule_email_dispatch
    
      def schedule_email_dispatch
        send_at(campaign.created_at + self.days.days, :send_email)
      end
    
      def send_email
      end
    end
    

    Run the workers using the rake task:

    rake jobs:work
    

    Every time a new Email object is created a delayed job item is added to the queue. At the correct interval the email will be sent by the worker.

    @campaign = Compaign.new(...)
    @campaign.emails.build(:days => 1)
    @campaign.emails.build(:days => 2)
    @campaign.save # now the delay
    

    In the example above, two delayed job entries will be created after saving the campaign. They are executed 1 and 2 days after the creation date of the campaign.

    This solution ensures emails are sent approximately around the expected schedule times. In a cron job based solution, disptaching happens at the cron intervals. There can be several hours delay between the intended dispatch time and the actual dispatch time.

    If you want to use the cron approach do the following:

    class Email < ActiveRecord::Base
      def self.dispatch_emails
        # find the emails due for dispatch
        Email.all(:conditions => ["created_at <= DATE_SUB(?, INTERVAL days DAY)", 
                 Time.now]).each do |email|
          email.send_email
        end
      end
    end
    

    In this solution, most of the processing is done by the DB.

    Add email.rake file in lib/tasks directory:

    task :dispatch_emails => :environment do
      Email.dispatch_emails
    end
    

    Configure the cron to execute rake dispatch_emails at regular intervals( in your case < 24 hours)

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-12 23:24

    I use ar_mailer gem

    http://seattlerb.rubyforge.org/ar_mailer/
    http://github.com/adzap/ar_mailer
    http://blog.segment7.net/articles/2006/08/15/ar_mailer

    0 讨论(0)
  • 2020-12-12 23:26

    I think it would be much easier and more secure (you don't have to worry on authentication and so on) to create a rake task to send the emails. Also you don't have to worry about a possibly very long running request. Just create a file RAILS_ROOT/lib/tasks/email.rake

    namespace :email do 
      desc "Sends scheduled emails"
      task :send_scheduled => :enviroment do 
        Email.send_scheduled_emails
      end
    end
    

    and in RAILS_ROOT/app/email.rb

    class Email < ActiveRecord::Base      
       # ...
    
       def self.send_scheduled_emails 
         #send your emails ...
       end
    end
    

    Then create a cron job

    0 0 * * * user cd /your/rails/app/ && RAILS_ENV=production rake emais:send_scheduled
    

    to send the emails every night at 12:00.

    0 讨论(0)
  • 2020-12-12 23:47

    I am using rufus-scheduler for scheduled email and twitter updates. You should check it.

    0 讨论(0)
提交回复
热议问题