Class: EventMachine::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/em-mailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mailer

Returns a new instance of Mailer.



10
11
12
13
14
15
16
17
18
# File 'lib/em-mailer.rb', line 10

def initialize(options)
  @options = options
  if @options[:username] and @options[:password]
    @options[:auth] = {}
    @options[:auth][:username] = @options.delete(:username)
    @options[:auth][:password] = @options.delete(:password)
    @options[:auth][:type] = :plain
  end
end

Instance Method Details

#callback(proc = nil, &blk) ⇒ Object



20
21
22
# File 'lib/em-mailer.rb', line 20

def callback(proc=nil, &blk)
  @callback = proc || blk
end

#errback(proc = nil, &blk) ⇒ Object



24
25
26
# File 'lib/em-mailer.rb', line 24

def errback(proc=nil, &blk)
  @errback = proc || blk
end

#mail(opts) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/em-mailer.rb', line 44

def mail(opts)
  msg = TMail::Mail.new
  msg.to = opts[:to]
  msg.from = opts[:from]
  msg.subject = opts[:subject]
  msg.body = opts[:body]
  msg.set_content_type("text", "html") if opts[:html]
  self.send(msg)
end

#send(msg) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/em-mailer.rb', line 28

def send(msg)
  opts = {
    # Can email have multiple 'from's? I duno, but this client doesn't like it.
    :from => msg.from.is_a?(Array) ? msg.from.first : msg.from,
    :to => msg.respond_to?(:destinations) ? msg.destinations : msg.to,
    :content => "#{msg.to_s}\r\n.\r\n",
    :verbose => false,
    :port => 25,
    :starttls => false
  }.merge(@options)

  email = EM::Protocols::SmtpClient.send(opts)
  email.callback &@callback if @callback
  email.errback &@errback if @errback
end