lib/mailit/mailer.rb in manveru-mailit-2009.06.08 vs lib/mailit/mailer.rb in manveru-mailit-2009.08

- old
+ new

@@ -1,10 +1,10 @@ module Mailit # The Mailer is an abstraction layer for different SMTP clients, it provides # #send and #defer_send methods # - # At the time of writing, Net::SMTP and EventMachine::SmtpClient are + # At the time of writing, Net::SMTP and EventMachine::Protocols::SmtpClient are # supported, it should be trivial to add support for any other client. # # The difference between #send and #defer_send depends on the backend, but # usually #send will block until the mail was sent while #defer_send does it # in the background and allows you to continue execution immediately. @@ -22,26 +22,19 @@ # Mailit::Mailer.send(mail) # # # Send in background thread and continue doing other things # Mailit::Mailer.defer_send(mail) # - # The default Mailer backend is Mailit::Mailer::NetSmtp, you can change the + # The default Mailer backend is Net::SMTP, you can change the # default by including another module into Mailit::mailer # # @example Using Mailt::Mailer::EventMachine by inclusion # # class Mailit::Mailer # include Mailit::Mailer::EventMachine # end # - # Another way is to extend an instance of Mailer with the backend you want to - # use, which will not affect other instances. - # - # @example Using Mailit::mailer::EventMachine by extension - # - # mailer = Mailit::Mailer.new - # mailer.extend(Mailit::Mailer::EventMachine) class Mailer OPTIONS = { :server => 'smtp.localhost', :port => 25, :domain => 'localhost', @@ -58,72 +51,81 @@ def self.defer_send(mail, options = {}) new.defer_send(mail, options) end - undef :send - attr_reader :options def initialize(options = {}) @options = OPTIONS.merge(options) - extend NetSmtp unless respond_to?(:send) end - private + def send(mail, override = {}) + require 'net/smtp' - def settings(override, *keys) - options.merge(override).values_at(*keys) + server, port, domain, username, password, auth_type, noop = + settings(override, :server, :port, :domain, :username, :password, :auth_type, :noop) + + ::Net::SMTP.start(server, port, domain, username, password, auth_type) do |smtp| + return if noop + smtp.send_message(mail.to_s, mail.from, mail.to) + end end - module NetSmtp - def send(mail, override = {}) - require 'net/smtp' + def defer_send(mail, override = {}) + Thread.new{ send(mail, override) } + end - server, port, domain, username, password, auth_type = - settings(override, :server, :port, :domain, :username, :password, :auth_type) + private - ::Net::SMTP.start(server, port, domain, username, password, auth_type) do |smtp| - return if noop - smtp.send_message(mail.to_s, mail.from, mail.to) - end - end - - def defer_send(mail, override = {}) - Thread.new{ send(mail, override) } - end + def settings(override, *keys) + options.merge(override).values_at(*keys) end - # Allows you to comfortably use the EventMachine::SmtpClient. - # In order to use it, you have to first include this module into - # Mailit::Mailer or extend the Mailit::Mailer instance with it. + # Allows you to comfortably use the EventMachine::Protocols::SmtpClient. + # In order to use it, you have to first include this module into Mailit::Mailer module EventMachine - # This assumes that EventMachine was required and we are inside the # EventMachine::run block. # # Since EM implements some parts of the mail building we'll have to # deconstruct our mail a bit. # On the upside, it seems like it supports STARTTLS (without certificate # options though) - def send(mail, options = {}) - server, port, domain, username, password, auth_type = - settings(override, :server, :port, :domain, :username, :password, :auth_type) + def self.included(base) + require 'em/protocols/smtpclient' + base.module_eval do + def send(mail, override = {}) + server, port, domain, username, password, auth_type = + settings(override, :server, :port, :domain, :username, :password, :auth_type) - mail.construct # prepare headers and body + mail.construct # prepare headers and body - em_options = { :port => port, :host => host, :domain => domain, - :from => mail.from, :to => mail.to, :header => mail.header_string, - :body => mail.body_string } + em_options = { :port => port, :host => server, :domain => domain, + :from => mail.from, :to => mail.to, :header => mail.header_string, + :body => mail.body_string } - if auth_type - em_options[:auth] = { - :type => auth_type, :username => username, :password => password } - end + if auth_type + em_options[:auth] = { + :type => auth_type, :username => username, :password => password } + end - ::EventMachine::SmtpClient.send(em_options) - end + email = EM::Protocols::SmtpClient.send(em_options) + email.callback &@callback if @callback + email.errback &@errback if @errback + end - alias defer_send send - end - end -end + def callback(proc=nil, &blk) + @callback = proc || blk + end + + def errback(proc=nil, &blk) + @errback = proc || blk + end + + alias defer_send send + end + + end + end # module EventMachine + end # class Mailer +end # module Mailit