Module: PonyExpress

Extended by:
PonyExpress
Included in:
PonyExpress, Mail
Defined in:
lib/pony-express.rb,
lib/pony-express/test.rb

Defined Under Namespace

Modules: Test Classes: Mail

Constant Summary collapse

TRANSPORTS =
[ :smtp, :sendmail ]
DEFAULT_SMTP_OPTIONS =
{ host: 'localhost', port: '25', domain: 'localhost.localdomain' }
@@sendmail_binary =
'/usr/sbin/sendmail'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transport_via_sendmail(content, *rest) ⇒ Object



12
13
14
# File 'lib/pony-express/test.rb', line 12

def self.transport_via_sendmail content, *rest
  PonyExpress::Test.mailbox << content
end

.transport_via_smtp(content, *rest) ⇒ Object



16
17
18
# File 'lib/pony-express/test.rb', line 16

def self.transport_via_smtp content, *rest
  PonyExpress::Test.mailbox << content
end

Instance Method Details

#build(options) ⇒ Object



12
13
14
15
# File 'lib/pony-express.rb', line 12

def build options
  # TODO validation.
  Mimetic.build(options)
end

#mail(options) ⇒ TrueClass or FalseClass

Build and dispatch email in one go.

Examples:

mail.

require "pony-express"
PonyExpress.mail to: "[email protected]", from: "[email protected]", via: "sendmail",
                 subject: "Hello Mr.Burns", text: "Can I have more donuts ?",
                 html: "<strong>More Dooooonuuuuts!</strong>",
                 attachments: [ "/home/homer/donuts.png" ],
                 headers: [{name: "X-FooBar", value: "test"}]

Parameters:

  • to (String)

    Email address.

  • from (String)

    Email address.

  • subject (String)

    Subject (will be converted to quoted printable if needed).

  • text (String)

    Plain text content.

  • cc (String)

    Email address (optional).

  • bcc (String)

    Email address (optional).

  • replyto (String)

    Email address (optional).

  • html (String)

    HTML content (optional).

  • attachments (Array)

    List of email attachments (optional).

  • headers (Array)

    List of email headers (optional).

Returns:

  • (TrueClass or FalseClass)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pony-express.rb', line 43

def mail options
  via         = options.delete(:via)         || :smtp
  via_options = options.delete(:via_options) || {}
  via         = via.to_sym

  envelope_from = options[:from]

  if headers = options[:headers]
    if return_path = headers.find {|h| h[:name] == 'Return-Path'}
      envelope_from = return_path[:value]
    elsif sender = headers.find {|h| h[:name] == 'Sender'}
      envelope_from = sender[:value]
    end
  end

  if TRANSPORTS.include? via
    case via
      when :sendmail then transport_via_sendmail build(options), envelope_from, options[:to], via_options
      when :smtp     then transport_via_smtp build(options), options[:from], options[:to], via_options
    end
  else
    raise ArgumentError, ":via can be one of #{TRANSPORTS}"
  end
end

#parse(content) ⇒ Object

NOTE: parse is very much WIP, YMMV



18
19
20
# File 'lib/pony-express.rb', line 18

def parse content
  Mimetic.parse(content)
end

#sendmail_binaryObject



72
73
74
# File 'lib/pony-express.rb', line 72

def sendmail_binary
  @@sendmail_binary
end

#sendmail_binary=(binary) ⇒ Object



68
69
70
# File 'lib/pony-express.rb', line 68

def sendmail_binary= binary
  @@sendmail_binary = binary
end

#transport_via_sendmail(content, from, to, options = {}) ⇒ Object



76
77
78
# File 'lib/pony-express.rb', line 76

def transport_via_sendmail content, from, to, options = {}
  IO.popen([sendmail_binary, '-t', '-i', "-f #{Shellwords.escape(from)}"], 'w') {|io| io.write(content)}
end

#transport_via_smtp(content, from, to, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pony-express.rb', line 80

def transport_via_smtp content, from, to, options = {}
  o = DEFAULT_SMTP_OPTIONS.merge(options)
  smtp = Net::SMTP.new(o[:host], o[:port])
  if o[:tls]
    unless smtp.respond_to?(:enable_starttls)
      begin
        require 'smtp_tls'
      rescue LoadError
        raise "You may need: gem install smtp_tls"
      end
    end
    smtp.enable_starttls
  end
  if o.include?(:auth)
    smtp.start(o[:domain], o[:user], o[:password], o[:auth])
  else
    smtp.start(o[:domain])
  end
  smtp.send_message content, from, to
  smtp.finish
end