`
wuhuizhong
  • 浏览: 681177 次
  • 性别: Icon_minigender_1
  • 来自: 中山
社区版块
存档分类
最新评论

Sending delayed email from devise

    博客分类:
  • ROR
 
阅读更多

Alternatively, instead of using the Delayed::Mailer gem, you can quite easily implement and use your own ActionMailer "delivery method", one that...

  1. intercepts mail delivery from ActionMailer
  2. stores the email in a table (optional)
  3. creates a Delayed::Job that references the stored email
  4. delivers the stored email when the delayed job is executed

Do something along the lines of:

# in config/application.rb
ActionMailer::Base.add_delivery_method :queued, Mail::QueuedDelivery

# in config/environment.rb (or one of the config/environments/*.rb files)
config.action_mailer.delivery_method = :queued # ie. Mail::QueuedDelivery

# in lib/mail/queued_delivery.rb
module Mail
  class QueuedDelivery

    def initialize(values = {})
      # (optional)
    end

    def deliver!(mail)
      email = Email.create!(:delivery_handler => mail.delivery_handler.name, :message => mail.to_s)
      Delayed::Job.enqueue Jobs::Email::DeliverEmail.new(email.id)
    end

  end # class QueueDelivery
end # module Mail
 

The Delayed::Job you need to implement would then...

  1. retrieve the stored email from the database -- email = ::Email.find_by_id(email_id)
  2. deliver the email via the mail gem -- Mail::Message.new(email.message).deliver

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics