Skip to content

Commit

Permalink
Now it can handle SMTP options
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyhann committed Nov 23, 2008
1 parent ef20ff4 commit 5cfd6a2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
11 changes: 11 additions & 0 deletions README.rdoc
Expand Up @@ -18,6 +18,17 @@ This can be over-ridden if you specify a via option

Pony.mail(:to => 'you@example.com', :via => :sendmail) # sends via sendmail

You can also specify options for SMTP:

Pony.mail(:to => 'you@example.com', :via => :smtp, :smtp => {
:host => 'smtp.yourserver.com',
:port => '25',
:user => 'user',
:pass => 'pass',
:auth => :plain # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
}

== Meta

Written by Adam Wiggins
Expand Down
Binary file added lib/.pony.rb.swp
Binary file not shown.
30 changes: 18 additions & 12 deletions lib/pony.rb
Expand Up @@ -6,15 +6,15 @@ module Pony
def self.mail(options)
raise(ArgumentError, ":to is required") unless options[:to]

unless(via = options[:via].to_s).empty?
if via == 'smtp' || via == 'sendmail'
send("transport_via_#{via}", build_tmail(options))
else
raise(ArgumentError, ":via must be either smtp or sendmail")
end
else
transport build_tmail(options)
end
unless(via = options[:via].to_s).empty?
if via == 'smtp' || via == 'sendmail'
send("transport_via_#{via}", build_tmail(options), options)
else
raise(ArgumentError, ":via must be either smtp or sendmail")
end
else
transport build_tmail(options)
end
end

def self.build_tmail(options)
Expand All @@ -38,7 +38,7 @@ def self.transport(tmail)
end
end

def self.transport_via_sendmail(tmail)
def self.transport_via_sendmail(tmail, options = {})
IO.popen('-') do |pipe|
if pipe
pipe.write(tmail.to_s)
Expand All @@ -48,8 +48,14 @@ def self.transport_via_sendmail(tmail)
end
end

def self.transport_via_smtp(tmail)
Net::SMTP.start('localhost') do |smtp|
def self.transport_via_smtp(tmail, options = {:smtp => {}})
options = options[:smtp]
# Credits for Sinatra::Mailer and Rhyhann
options_array = options.empty? ?
['localhost' ] :
[options[:host], options[:port].to_i, options[:domain],
options[:user], options[:pass], options[:auth] ]
Net::SMTP.start(*options_array) do |smtp|
smtp.sendmail(tmail.to_s, tmail.from, tmail.to)
end
end
Expand Down

0 comments on commit 5cfd6a2

Please sign in to comment.