Skip to content

Commit

Permalink
merge in attachment changes from nmeans, add tests and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
benprew committed Nov 18, 2009
2 parents d859066 + f6c54c0 commit aaeb1b7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.swp
.DS_Store
16 changes: 14 additions & 2 deletions README.rdoc
Expand Up @@ -6,11 +6,11 @@ Ruby no longer has to be jealous of PHP's mail() function, which can send an ema

Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Hello there.')
Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => '<h1>Hello there!</h1>', :content_type => 'text/html')
Pony.mail(:to => 'you@example.com', :cc => 'him@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Howdy')

Any option key may be omitted except for :to.



== Transport

Pony uses /usr/sbin/sendmail to send mail if it is available, otherwise it uses SMTP to localhost.
Expand Down Expand Up @@ -52,16 +52,28 @@ You can attach a file or two with the :attachments option:

Pony.mail(..., :attachments => {"foo.zip" => File.read("path/to/foo.zip"), "hello.txt" => "hello!"})

An attachment's mime-type is set based on the filename (as dictated by the ruby gem mime-types). So 'foo.pdf' has a mime-type of 'application/pdf'

== Meta

Maintained by Ben Prew

Written by Adam Wiggins

Patches contributed by: Mathieu Martin, Arun Thampi, Thomas Hurst, Stephen
Celis, Othmane Benkirane, Neil Mock, and Hiroshi Saito
Celis, Othmane Benkirane, Neil Mock, Hiroshi Saito, and Nickolas Means

Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

http://github.com/benprew/pony

== Releases

0.4.1
* Add :cc capability
* fix bug: resolve body not displaying when attachments sent

0.4
* Implemented file attachments option
* use TLS if :tls => true

1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -48,6 +48,7 @@ spec = Gem::Specification.new do |s|
s.files = %w(Rakefile) + Dir.glob("{lib,spec}/**/*")

s.require_path = "lib"
s.add_dependency('mime-types', '>= 1.16')
s.add_dependency( 'tmail', '~> 1.0' )
end

Expand Down
33 changes: 23 additions & 10 deletions lib/pony.rb
@@ -1,5 +1,6 @@
require 'rubygems'
require 'net/smtp'
require 'mime/types'
begin
require 'smtp_tls'
rescue LoadError
Expand Down Expand Up @@ -31,22 +32,34 @@ def self.build_tmail(options)
mail = TMail::Mail.new
mail.content_type = options[:content_type] if options[:content_type]
mail.to = options[:to]
mail.cc = options[:cc] || ''
mail.from = options[:from] || 'pony@unknown'
mail.subject = options[:subject]
mail.body = options[:body] || ""
(options[:attachments] || []).each do |name, body|
attachment = TMail::Mail.new
attachment.transfer_encoding = "base64"
attachment.body = Base64.encode64(body)
# attachment.set_content_type # TODO: if necessary
attachment.set_content_disposition "attachment", "filename" => name
mail.parts.push attachment
if options[:attachments]
# If message has attachment, then body must be sent as a message part
# or it will not be interpreted correctly by client.
body = TMail::Mail.new
body.body = options[:body] || ""
body.content_type = options[:content_type] || "text/plain"
mail.parts.push body
(options[:attachments] || []).each do |name, body|
attachment = TMail::Mail.new
attachment.transfer_encoding = "base64"
attachment.body = Base64.encode64(body)
content_type = MIME::Types.type_for(name).to_s
attachment.content_type = content_type unless content_type == ""
attachment.set_content_disposition "attachment", "filename" => name
mail.parts.push attachment
end
else
mail.content_type = options[:content_type] || "text/plain"
mail.body = options[:body] || ""
end
mail
end

def self.sendmail_binary
@sendmail_binary ||= `which sendmail`.chomp
@sendmail_binary ||= `which sendmail`.chomp + " -t"
end

def self.transport(tmail)
Expand All @@ -66,7 +79,7 @@ def self.transport_via_sendmail(tmail, options={})
if pipe
pipe.write(tmail.to_s)
else
exec(sendmail_binary, *tmail.to)
exec(sendmail_binary)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions pony.gemspec
Expand Up @@ -16,4 +16,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.1}
s.summary = s.description
s.add_dependency('tmail', '>= 1.2.3')
s.add_dependency('mime-types', '>= 1.16')
end
23 changes: 21 additions & 2 deletions spec/pony_spec.rb
Expand Up @@ -28,6 +28,10 @@
Pony.build_tmail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
end

it "cc" do
Pony.build_tmail(:cc => 'joe@example.com').cc.should == [ 'joe@example.com' ]
end

it "from" do
Pony.build_tmail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
end
Expand All @@ -50,14 +54,29 @@

it "attachments" do
tmail = Pony.build_tmail(:attachments => {"foo.txt" => "content of foo.txt"})
tmail.should have(1).parts
tmail.parts.first.to_s.should == <<-PART
tmail.should have(2).parts
tmail.parts.first.to_s.should == "Content-Type: text/plain\n\n"
tmail.parts.last.to_s.should == <<-PART
Content-Type: text/plain
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=foo.txt
Y29udGVudCBvZiBmb28udHh0
PART
end

it "suggests mime-type" do
tmail = Pony.build_tmail(:attachments => {"foo.pdf" => "content of foo.pdf"})
tmail.should have(2).parts
tmail.parts.first.to_s.should == "Content-Type: text/plain\n\n"
tmail.parts.last.to_s.should == <<-PART
Content-Type: application/pdf
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=foo.pdf
Y29udGVudCBvZiBmb28ucGRm
PART
end
end

describe "transport" do
Expand Down

0 comments on commit aaeb1b7

Please sign in to comment.