Skip to content

Instantly share code, notes, and snippets.

@brentkirby
Created June 28, 2011 15:07
Show Gist options
  • Save brentkirby/1051339 to your computer and use it in GitHub Desktop.
Save brentkirby/1051339 to your computer and use it in GitHub Desktop.
Tiny resque mail worker setup that removes Rail's stupid dependencies so we can delay messages without the whole rails env. (used w mongo fork)
require 'zlib'
class AnyMailer < ActionMailer::Base
def enqueue(method, *args)
message = self.send(method, *args)
Resque.enqueue(MailWorker, message: compressed_msg(message))
end
# Could easily be changed to not use BSON
def compressed_msg(msg)
BSON::Binary.new(Zlib::Deflate.deflate(msg.to_yaml))
end
end
require 'zlib'
class MailWorker
class_attribute :smtp_config
def self.configure( options )
options.merge!({
:address => "smtp.sendgrid.net",
:enable_starttls_auto => true,
:port => 587,
:tls => true,
:authentication => 'plain'
})
self.smtp_config = options
end
@queue = :mail
@delayed_jobs = true
def self.perform(options)
Mail.defaults do
delivery_method :smtp, MailWorker.smtp_config
end
comp = options['message']
message = Mail::Message.from_yaml(Zlib::Inflate.inflate(comp.to_s))
message.deliver!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment