Module: Mailit::Mailer::EventMachine

Defined in:
lib/mailit/mailer.rb

Overview

Allows you to comfortably use the EventMachine::Protocols::SmtpClient. In order to use it, you have to first include this module into Mailit::Mailer

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

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)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mailit/mailer.rb', line 96

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

      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

      email = EM::Protocols::SmtpClient.send(em_options)
      email.callback &@callback if @callback
      email.errback &@errback if @errback
    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