Class: Mailit::Mailer

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

Overview

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::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.

The default Mailer backend is Net::SMTP, you can change the default by including another module into Mailit::mailer

Examples:

Usage


mail = Mailit::Mail.new
mail.to = '[email protected]'
mail.from = '[email protected]'
mail.subject 'Here are some files for you!'
mail.text = 'This is what you see with a plaintext mail reader'
mail.attach('/home/manveru/.vimrc')

# Send and wait until sending finished
Mailit::Mailer.send(mail)

# Send in background thread and continue doing other things
Mailit::Mailer.defer_send(mail)

Using Mailt::Mailer::EventMachine by inclusion


class Mailit::Mailer
  include Mailit::Mailer::EventMachine
end

Defined Under Namespace

Modules: EventMachine

Constant Summary collapse

OPTIONS =
{
  :server    => 'smtp.localhost',
  :port      => 25,
  :domain    => 'localhost',
  :username  => '',
  :password  => 'foo',
  :noop      => false,
  :auth_type => :login, # :plain, :login, :cram_md5
  :starttls  => false,  # only useful for EventMachine::SmtpClient
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mailer

Returns a new instance of Mailer.


58
59
60
# File 'lib/mailit/mailer.rb', line 58

def initialize(options = {})
  @options = OPTIONS.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


56
57
58
# File 'lib/mailit/mailer.rb', line 56

def options
  @options
end

Class Method Details

.defer_send(mail, options = {}) ⇒ Object


52
53
54
# File 'lib/mailit/mailer.rb', line 52

def self.defer_send(mail, options = {})
  new.defer_send(mail, options)
end

.send(mail, options = {}) ⇒ Object


48
49
50
# File 'lib/mailit/mailer.rb', line 48

def self.send(mail, options = {})
  new.send(mail, options)
end

Instance Method Details

#defer_send(mail, override = {}) ⇒ Object


76
77
78
# File 'lib/mailit/mailer.rb', line 76

def defer_send(mail, override = {})
  Thread.new{ send(mail, override) }
end

#send(mail, override = {}) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mailit/mailer.rb', line 62

def send(mail, override = {})
  require 'net/smtp'
  mailer = override[:mailer] || ::Net::SMTP

  server, port, domain, username, password, auth_type, noop =
    settings(override, :server, :port, :domain, :username, :password, :auth_type, :noop)
  username = mail.from.to_s if !username.nil? && username.empty?

  mailer.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