Use ar_mailer for future mail delivery

This little howto describes, how to use ar_mailer to schedule emails for future delivery.

Sorry for the bad formatting. Might change it someday…
1) Migrate emails table and add needed fields

  add_column :emails, :various, :string #holds classname and id of any object you want to
  add_column :emails, :type, :string #type of email
  add_column :emails, :date_to_send, :date #date to send that mail

2) Override “def find” in email.rb

  def self.find(*args)
    with_scope(:find=>{ :conditions=>["(date_to_send IS NULL OR date_to_send <= ?)",] }) do

3) Create DelayedEmail class, inherting Email

  class DelayedEmail < Email
    before_save :set_due_date
    before_save :set_various_field
    @@days_before_sending = 21 #default is 3 weeks after creation of various object
    @@various_class = nil

    def set_due_date
      self.date_to_send = + @@days_before_sending

    def set_various_field
      name = @@various_class.class.to_s + "_" rescue "NOCLASS_"
      id = rescue "0"
      self.various = name + id

    #tell email to which object it belongs. might be important for future deletion of unsent mails
    def self.set_various_class(c)
      @@various_class = c

    #tell email how many days shall pass by before sending email
    def self.set_days(days)
      @@days_before_sending = days

4) Modify ar_mailer standard emailer class in controller before delivery and set it back afterwards

  #Let any mailer send an email through this class
  #eg. Mailer.deliver_mymail(...)

2 Replies to “Use ar_mailer for future mail delivery”

  1. Hi Eric,
    sorry for the delay. There’s no change in ARMailer actually.
    It’s all about 2 things:
    1) adding a due date to the emails table
    2) overriding the email find method to collect only due emails.
    This works without changing anything in ARMailer.
    Anyway, let me know if you need further information.

    Cheers Matt

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.