{"id":46,"date":"2008-12-10T12:49:01","date_gmt":"2008-12-10T11:49:01","guid":{"rendered":"http:\/\/www.le-matt.de\/blog\/use-ar_mailer-for-future-mail-delivery\/"},"modified":"2009-04-15T08:29:31","modified_gmt":"2009-04-15T07:29:31","slug":"use-ar_mailer-for-future-mail-delivery","status":"publish","type":"post","link":"https:\/\/www.any-where.de\/blog\/use-ar_mailer-for-future-mail-delivery\/","title":{"rendered":"Use ar_mailer for future mail delivery"},"content":{"rendered":"<p>This little howto describes, how to use ar_mailer to schedule emails for future delivery.<\/p>\n<p>Sorry for the bad formatting. Might change it someday&#8230;<br \/>\n1) Migrate emails table and add needed fields<\/p>\n<pre>  add_column :emails, :various, :string #holds classname and id of any object you want to\r\n  add_column :emails, :type, :string #type of email\r\n  add_column :emails, :date_to_send, :date #date to send that mail<\/pre>\n<p>2) Override &#8220;def find&#8221; in email.rb<\/p>\n<pre>  def self.find(*args)\r\n    with_scope(:find=&gt;{ :conditions=&gt;[\"(date_to_send IS NULL OR date_to_send &lt;= ?)\", Date.today] }) do\r\n      super(*args)\r\n    end\r\n  end<\/pre>\n<p>3) Create DelayedEmail class, inherting Email<\/p>\n<pre>  class DelayedEmail &lt; Email\r\n    before_save :set_due_date\r\n    before_save :set_various_field\r\n    @@days_before_sending = 21 #default is 3 weeks after creation of various object\r\n    @@various_class = nil\r\n\r\n    def set_due_date\r\n      self.date_to_send = Date.today + @@days_before_sending\r\n    end\r\n\r\n    def set_various_field\r\n      name = @@various_class.class.to_s + \"_\" rescue \"NOCLASS_\"\r\n      id = @@various_class.id.to_s rescue \"0\"\r\n      self.various = name + id\r\n    end\r\n\r\n    #tell email to which object it belongs. might be important for future deletion of unsent mails\r\n    def self.set_various_class(c)\r\n      @@various_class = c\r\n    end\r\n\r\n    #tell email how many days shall pass by before sending email\r\n    def self.set_days(days)\r\n      @@days_before_sending = days\r\n    end\r\n  end<\/pre>\n<p>4) Modify ar_mailer standard emailer class in controller before delivery and set it back afterwards<\/p>\n<pre>  DelayedEmail.set_days(10)\r\n  DelayedEmail.set_various_class(SOMEOBJECT)\r\n  ActionMailer::ARMailer.email_class=DelayedEmail\r\n  #Let any mailer send an email through this class\r\n  #eg. Mailer.deliver_mymail(...)\r\n  ActionMailer::ARMailer.email_class=Email<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This little howto describes, how to use ar_mailer to schedule emails for future delivery. Sorry for the bad formatting. Might change it someday&#8230; 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, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.any-where.de\/blog\/use-ar_mailer-for-future-mail-delivery\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Use ar_mailer for future mail delivery&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,25],"tags":[26,243],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-programming-the-web","category-ruby-on-rails","tag-ar_mailer","tag-ruby-on-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":2,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}