Skip to content
Browse files

Hacked and mutilated the network section, made it easier to extend ou…

…t with other delivery and retriever methods. API changed SLIGHTLY with this. Please check the readme.
  • Loading branch information...
1 parent e7322aa commit e12737ecc4a4fee0e59c6d655cba2302f94f9a11 @mikel committed Nov 13, 2009
View
10 CHANGELOG.rdoc
@@ -1,6 +1,14 @@
+== Fri Nov 13 00:31:04 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
+
+* Hacked and mutilated the network section, made it easier to extend out with other
+ delivery and retriever methods. API changed SLIGHTLY with this. Please check the
+ readme
+* Resolved Issue #8 - Mail::SMTP now delivers to all mail.destinations
+* Version bump to 1.2.5
+
== Thu Nov 12 02:58:01 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
-* Resolved Issue #8 - Message ID not handling multiple periods in left hand side
+* Resolved Issue #5 - Message ID not handling multiple periods in left hand side
* Resolved Issue #6 - Ordering of add_file and body items causes invalid emails
== Tue Nov 10 08:15:14 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
View
6 Manifest.txt
@@ -70,7 +70,13 @@ lib/mail/header.rb
lib/mail/mail.rb
lib/mail/message.rb
lib/mail/network/deliverable.rb
+lib/mail/network/delivery_methods/file_delivery.rb
+lib/mail/network/delivery_methods/sendmail.rb
+lib/mail/network/delivery_methods/smtp.rb
+lib/mail/network/delivery_methods/test_mailer.rb
lib/mail/network/retrievable.rb
+lib/mail/network/retriever_methods/imap.rb
+lib/mail/network/retriever_methods/pop3.rb
lib/mail/parsers/address_lists.rb
lib/mail/parsers/address_lists.treetop
lib/mail/parsers/content_disposition.rb
View
6 lib/mail.rb
@@ -28,7 +28,13 @@ module Mail # :doc:
require File.join(dir_name, 'utilities')
require File.join(dir_name, 'configuration')
require File.join(dir_name, 'network', 'deliverable')
+ require File.join(dir_name, 'network', 'delivery_methods', 'smtp')
+ require File.join(dir_name, 'network', 'delivery_methods', 'file_delivery')
+ require File.join(dir_name, 'network', 'delivery_methods', 'sendmail')
+ require File.join(dir_name, 'network', 'delivery_methods', 'test_mailer')
require File.join(dir_name, 'network', 'retrievable')
+ require File.join(dir_name, 'network', 'retriever_methods', 'pop3')
+ require File.join(dir_name, 'network', 'retriever_methods', 'imap')
require File.join(dir_name, 'message')
require File.join(dir_name, 'part')
View
94 lib/mail/configuration.rb
@@ -7,7 +7,11 @@
module Mail
# The Configuration class is a Singleton used to hold the default
- # configuration for all SMTP, POP3 and IMAP operations handled by Mail.
+ # configuration for all class wide configurations of Mail.
+ #
+ # This includes things like running in Test mode, the POP3, IMAP,
+ # SMTP, Sendmail and File delivery method information et al.
+ #
# See Mail.defaults for more information.
class Configuration
include Singleton
@@ -35,11 +39,19 @@ def defaults(&block)
# Mail.defaults do
# smtp '127.0.0.1', 25
# end
- def smtp(*args)
+ def smtp(*args, &block)
if args.size > 0
- @smtp = [args[0], (args[1].to_i > 0 ? args[1] : 25)]
+ host_array = [args[0], (args[1].to_i > 0 ? args[1] : 25)]
end
- @smtp
+ set_settings(Mail::SMTP, host_array, &block)
+ end
+
+ # Allows you to define the delivery method for mail, defaults to SMTP
+ #
+ # This can either be a symbol (:smtp, :sendmail, :file, :test) or you can pass
+ # in your own delivery class, in which case this will be set.
+ def delivery_method(value = nil)
+ value ? @delivery_method = lookup_delivery_method(value) : @delivery_method ||= Mail::SMTP
end
# Allows you to specify the POP3 Server by passing the hostname
@@ -57,38 +69,66 @@ def smtp(*args)
# Mail.defaults do
# pop3 '127.0.0.1', 110
# end
- def pop3(*args)
+ def pop3(*args, &block)
if args.size > 0
- @pop3 = [args[0], (args[1].to_i > 0 ? args[1] : 110)]
+ host_array = [args[0], (args[1].to_i > 0 ? args[1] : 110)]
end
- @pop3
- end
-
- # Pass in the username to use for POP3 or IMAP access
- def user(value = nil)
- value ? @user = value : @user
- end
-
- def pass(value = nil)
- value ? @pass = value : @pass
- end
-
- def enable_tls
- @tls = true
- end
-
- def disable_tls
- @tls = false
+ set_settings(Mail::POP3, host_array, &block)
end
- def tls?
- @tls || false
+ # Allows you to define the retriever method for mail, defaults to POP3
+ #
+ # This can either be a symbol (:pop3, :imap) or you can pass
+ # in your own retriever class, in which case this will be set.
+ def retriever_method(value = nil)
+ value ? @retriever_method = lookup_retriever_method(value) : @retriever_method ||= Mail::POP3
end
-
+
def param_encode_language(value = nil)
value ? @encode_language = value : @encode_language ||= 'en'
end
+ private
+
+ def set_settings(klass, host_array = nil, &block)
+ if host_array
+ klass.instance.settings do
+ host host_array[0]
+ port host_array[1]
+ end
+ end
+ if block_given?
+ klass.instance.settings(&block)
+ end
+ klass.instance.settings(&block)
+ end
+
+ def lookup_delivery_method(method)
+ case method
+ when :smtp || nil
+ Mail::SMTP
+ when :sendmail
+ Mail::Sendmail
+ when :file
+ Mail::FileDelivery
+ when :test
+ Mail::TestMailer
+ else
+ method
+ end
+ end
+
+ def lookup_retriever_method(method)
+ case method
+ when :pop3 || nil
+ Mail::POP3
+ # when :imap
+ # Mail::IMAP
+ else
+ method
+ end
+ end
+
end
end
View
65 lib/mail/mail.rb
@@ -55,54 +55,95 @@ def Mail.new(*args, &block)
# if port values are omitted from the SMTP and POP3 method calls, then it
# is assumed to use the default ports of 25 and 110 respectively.
#
- # The methods you can call in the default configuration are:
+ # You call defaults in a block, then you can set the basic host and port,
+ # by passing arguments, also, if the method you are using required more
+ # information (like pop3), then pass a block to it and call methods like
+ # user, pass, enable_tls etc.
#
- # smtp(server_name, port):: Sets the SMTP server domain name and port
- # pop3(server_name, port):: Sets the POP3 server domain name and port
- # user(username):: Sets the username used for POP3 sessions
- # pass(password):: Sets the password used for POP3 sessions
+ # The arguments and block are both optional.
#
# Mail.defaults do
# smtp 'smtp.myhost.fr', 587
- # pop3 'pop.myhost.fr'
- # user 'bernardo'
- # pass 'mypass'
- # enable_tls
+ # pop3 'pop.myhost.fr' do
+ # user 'bernardo'
+ # pass 'mypass'
+ # enable_tls
+ # end
+ # end
+ #
+ # You will also want to specify a delivery and retriever type, the defaults
+ # are SMTP and POP3. You set the types by passing a symbol:
+ #
+ # Mail.defaults do
+ # retriever_method :pop3
+ # delivery_method :smtp
+ # end
+ #
+ # The only implemented methods at the moment are :pop3, :smtp and :test
+ #
+ # You can also specify your own delivery class which must respond to :deliver!
+ # or a retriever class which must respond to :get_messages
+ #
+ # Mail.defaults do
+ # retriever_method MyOwnRetriever.new
# end
#
# Once you have set defaults, you can call Mail.deliver to send an email
# through the Mail.deliver method.
def Mail.defaults(&block)
if block_given?
Mail::Configuration.instance.defaults(&block)
+ else
+ Mail::Configuration.instance
end
end
# Send an email using the default configuration. You do need to set a default
# configuration first before you use Mail.deliver, if you don't, an appropriate
# error will be raised telling you to.
#
+ # If you do not specify a delivery type, SMTP will be used.
+ #
# Mail.deliver do
# to 'mikel@test.lindsaar.net'
# from 'ada@test.lindsaar.net'
# subject 'This is a test email'
# body 'Not much to say here'
# end
#
+ # You can also do:
+ #
+ # mail = Mail.read('email.eml')
+ # mail.deliver!
+ #
# And your email object will be created and sent.
-
def Mail.deliver(*args, &block)
- Mail.new(args, &block).deliver!
+ mail = Mail.new(args, &block)
+ Deliverable.perform_delivery!(mail)
+ mail
end
+ # Returns all the messages on the server using the retriever method set up
+ # in Mail.defaults
def Mail.get_all_mail(&block)
- Mail::Message.get_all_mail(&block)
+ if block_given?
+ Retrievable.get_messages.each do |message|
+ yield message
+ end
+ else
+ Retrievable.get_messages
+ end
end
def Mail.read(filename)
Mail.new(File.read(filename))
end
+ # Provides a store of all the emails sent
+ def Mail.deliveries
+ @@deliveries ||= []
+ end
+
protected
def Mail.random_tag
View
22 lib/mail/message.rb
@@ -46,8 +46,6 @@ class Message
include Patterns
include Utilities
- include Deliverable
- include Retrievable
# Creates a new Mail::Message object through .new
def initialize(*args, &block)
@@ -65,6 +63,26 @@ def initialize(*args, &block)
self
end
+ def deliver!
+ Deliverable.perform_delivery!(self)
+ end
+
+ def <=>(other)
+ if other.nil?
+ 1
+ else
+ self.date <=> other.date
+ end
+ end
+
+ def ==(other)
+ unless other.respond_to?(:encoded)
+ false
+ else
+ self.encoded == other.encoded
+ end
+ end
+
# Provides access to the raw source of the message as it was when it
# was instantiated. This is set at initialization and so is untouched
# by the parsers or decoder / encoders
View
47 lib/mail/network/deliverable.rb
@@ -1,46 +1,15 @@
# encoding: utf-8
-
-# Include this module to make a class "deliverable".
-# It uses the defaults set in Configuration to retrieve SMTP settings.
-#
-# Thanks to Nicolas Fouché for this wrapper
-#
module Mail
+ # The deliverable class provides the way mail will send an email out when you
+ # are using the Mail.deliver! &block command or when you call Mail.deliver!(message)
+ #
+ # The default delivery is SMTP, localhost, port 25. You can change this through the
+ # Mail.defaults call.s
module Deliverable
- # Send the message via SMTP.
- # The from and to attributes are optional. If not set, they are retrieve from the Message.
- def deliver!(from = nil, to = nil, rfc2822 = nil)
- config = Mail::Configuration.instance
- if config.smtp.blank? || config.smtp[0].blank?
- raise ArgumentError.new('Please call +Mail.defaults+ to set the SMTP configuration')
- end
-
- # TODO: use the "return-path" field by default instead of the "from" field ? (see ActionMailer)
- from ||= self.from.addresses.first if self.respond_to?(:from) && self.from
- raise ArgumentError.new('An author -from- is required to send a message') if from.blank?
- to ||= self.to.addresses if self.respond_to?(:to) && self.to
- raise ArgumentError.new('At least one recipient -from- is required to send a message') if to.blank?
- rfc2822 ||= self.encoded if self.respond_to?(:encoded)
- raise ArgumentError.new('A encoded content is required to send a message') if rfc2822.blank?
-
- smtp = Net::SMTP.new(config.smtp[0], config.smtp[1] || 25)
- if config.tls?
- if OpenSSL::SSL::VERIFY_NONE.kind_of?(OpenSSL::SSL::SSLContext)
- smtp.enable_tls(OpenSSL::SSL::VERIFY_NONE)
- else
- smtp.enable_tls
- end
- else
- smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
- end
-
- smtp.start(helo = 'localhost.localdomain', config.user, config.pass, authentication = :plain) do |smtp|
- smtp.sendmail(rfc2822, from, to)
- end
-
- self
+ def self.perform_delivery!(mail)
+ Mail.defaults.delivery_method.deliver!(mail)
end
- end
+ end
end
View
18 lib/mail/network/delivery_methods/file_delivery.rb
@@ -0,0 +1,18 @@
+module Mail
+ class FileDelivery
+ include Singleton
+
+ def settings(&block)
+ if block_given?
+ instance_eval(&block)
+ end
+ self
+ end
+
+ def FileDelivery.deliver!(mail)
+ # To be implemented
+ Mail.deliveries << mail
+ end
+
+ end
+end
View
18 lib/mail/network/delivery_methods/sendmail.rb
@@ -0,0 +1,18 @@
+module Mail
+ class Sendmail
+ include Singleton
+
+ def settings(&block)
+ if block_given?
+ instance_eval(&block)
+ end
+ self
+ end
+
+ def Sendmail.deliver!(mail)
+ # To be implemented
+ Mail.deliveries << mail
+ end
+
+ end
+end
View
87 lib/mail/network/delivery_methods/smtp.rb
@@ -0,0 +1,87 @@
+module Mail
+ class SMTP
+ include Singleton
+
+ def settings(&block)
+ if block_given?
+ instance_eval(&block)
+ end
+ self
+ end
+
+ # This is the host that you will send your SMTP mails to, defaults to 'localhost'
+ def host(value = nil)
+ value ? @host = value : @host ||= 'localhost'
+ end
+
+ # This is the port that SMTP email get sent to, default is 25
+ def port(value = nil)
+ value ? @port = value.to_i : @port ||= 25
+ end
+
+ # The username to use during SMTP authentication
+ def user(value = nil)
+ value ? @user = value : @user
+ end
+
+ # Password to use during SMTP authentication
+ def pass(value = nil)
+ value ? @pass = value : @pass
+ end
+
+ # The helo domain used at the begining of an SMTP conversation,
+ # default is 'localhost.localdomain'
+ def helo(value = nil)
+ value ? @help = value : @helo ||= 'localhost.localdomain'
+ end
+
+ # Turn on TLS
+ def enable_tls
+ @tls = true
+ end
+
+ # Turn on TLS
+ def disable_tls
+ @tls = false
+ end
+
+ # TLS Enabled? Default is false
+ def tls?
+ @tls || false
+ end
+
+ # Send the message via SMTP.
+ # The from and to attributes are optional. If not set, they are retrieve from the Message.
+ def SMTP.deliver!(mail)
+
+ config = Mail.defaults
+
+ # TODO: use the "return-path" field by default instead of the "from" field ? (see ActionMailer)
+ from = mail.from.addresses.first if mail.respond_to?(:from) && mail.from
+ raise ArgumentError.new('An author -from- is required to send a message') if from.blank?
+ to ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
+ raise ArgumentError.new('At least one recipient -to, cc, bcc- is required to send a message') if to.blank?
+ msg_str ||= mail.encoded if mail.respond_to?(:encoded)
+ raise ArgumentError.new('A encoded content is required to send a message') if msg_str.blank?
+
+ smtp = Net::SMTP.new(config.smtp.host, config.smtp.port)
+ if config.smtp.tls?
+ if OpenSSL::SSL::VERIFY_NONE.kind_of?(OpenSSL::SSL::SSLContext)
+ smtp.enable_tls(OpenSSL::SSL::VERIFY_NONE)
+ else
+ smtp.enable_tls
+ end
+ else
+ smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
+ end
+
+ smtp.start(config.smtp.helo, config.smtp.user, config.smtp.pass, authentication = :plain) do |smtp|
+ smtp.sendmail(msg_str, from, to)
+ end
+
+ self
+ end
+
+
+ end
+end
View
10 lib/mail/network/delivery_methods/test_mailer.rb
@@ -0,0 +1,10 @@
+module Mail
+ class TestMailer
+ include Singleton
+
+ def TestMailer.deliver!(mail)
+ Mail.deliveries << mail
+ end
+
+ end
+end
View
52 lib/mail/network/retrievable.rb
@@ -7,55 +7,9 @@
#
module Mail
module Retrievable
-
- module ClassMethods
-
- # Get all emails via POP3.
- # TODO :limit option
- # TODO :order option
- def pop3_get_all_mail(&block)
- config = Mail::Configuration.instance
- if config.pop3.blank? || config.pop3[0].blank?
- raise ArgumentError.new('Please call +Mail.defaults+ to set the POP3 configuration')
- end
-
- pop3_start do |pop3|
- if block_given?
- pop3.each_mail do |pop_mail|
- yield self.new(pop_mail.pop)
- end
- else
- emails = []
- pop3.each_mail do |pop_mail|
- emails << self.new(pop_mail.pop)
- end
- emails
- end
- end
-
- end
-
- private
-
- def pop3_start(config = Mail::Configuration.instance, &block)
- raise ArgumentError.new("Mail::Retrievable#pop3_start takes a block") unless block_given?
-
- pop3 = Net::POP3.new(config.pop3[0], config.pop3[1] || 110, isapop = false)
- pop3.enable_ssl(verify = OpenSSL::SSL::VERIFY_NONE) if config.tls?
- pop3.start(config.user, config.pass)
-
- yield pop3
- ensure
- if defined?(pop3) && pop3 && pop3.started?
- pop3.reset # This clears all "deleted" marks from messages.
- pop3.finish
- end
- end
-
- end
-
- def self.included(receiver)
- receiver.extend ClassMethods
+
+ def self.get_messages
+ Mail.defaults.retriever_method.get_messages
end
end
View
17 lib/mail/network/retriever_methods/imap.rb
@@ -0,0 +1,17 @@
+module Mail
+ class IMAP
+ include Singleton
+
+ def settings(&block)
+ if block_given?
+ instance_eval(&block)
+ end
+ self
+ end
+
+ def IMAP.get_messages(&block)
+ # To be implemented
+ end
+
+ end
+end
View
92 lib/mail/network/retriever_methods/pop3.rb
@@ -0,0 +1,92 @@
+module Mail
+ class POP3
+ include Singleton
+
+ require 'net/pop'
+
+ def settings(&block)
+ if block_given?
+ instance_eval(&block)
+ end
+ self
+ end
+
+ # This is the host that you will send your POP3 mails to, defaults to 'localhost'
+ def host(value = nil)
+ value ? @host = value : @host ||= 'localhost'
+ end
+
+ # This is the port that POP3 email get sent to, default is 110
+ def port(value = nil)
+ value ? @port = value.to_i : @port ||= 110
+ end
+
+ # The username to use during POP3 authentication
+ def user(value = nil)
+ value ? @user = value : @user
+ end
+
+ # Password to use during POP3 authentication
+ def pass(value = nil)
+ value ? @pass = value : @pass
+ end
+
+ # Turn on TLS
+ def enable_tls
+ @tls = true
+ end
+
+ # Turn on TLS
+ def disable_tls
+ @tls = false
+ end
+
+ # TLS Enabled? Default is false
+ def tls?
+ @tls || false
+ end
+
+ # Get all emails via POP3.
+ # TODO :limit option
+ # TODO :order option
+ def POP3.get_messages(&block)
+ config = Mail.defaults
+ if config.pop3.host.blank? || config.pop3.port.blank?
+ raise ArgumentError.new('Please call +Mail.defaults+ to set the POP3 configuration')
+ end
+
+ start do |pop3|
+ if block_given?
+ pop3.each_mail do |pop_mail|
+ yield Mail.new(pop_mail.pop)
+ end
+ else
+ emails = []
+ pop3.each_mail do |pop_mail|
+ emails << Mail.new(pop_mail.pop)
+ end
+ emails
+ end
+ end
+
+ end
+
+ private
+
+ def POP3.start(config = Mail::Configuration.instance, &block)
+ raise ArgumentError.new("Mail::Retrievable#pop3_start takes a block") unless block_given?
+
+ pop3 = Net::POP3.new(config.pop3.host, config.pop3.port, isapop = false)
+ pop3.enable_ssl(verify = OpenSSL::SSL::VERIFY_NONE) if config.pop3.tls?
+ pop3.start(config.pop3.user, config.pop3.pass)
+
+ yield pop3
+ ensure
+ if defined?(pop3) && pop3 && pop3.started?
+ pop3.reset # This clears all "deleted" marks from messages.
+ pop3.finish
+ end
+ end
+
+ end
+end
View
2 lib/mail/version.rb
@@ -3,7 +3,7 @@ module Mail
module VERSION
MAJOR = 1
MINOR = 2
- TINY = 2
+ TINY = 5
STRING = [MAJOR, MINOR, TINY].join('.')
end
View
2 mail.gemspec
@@ -3,7 +3,7 @@ MAIL_ROOT = File.dirname(__FILE__)
Gem::Specification.new do |s|
s.name = %q{mail}
- s.version = "1.2.1"
+ s.version = "1.2.5"
s.requirements << 'treetop, Treetop is a Ruby-based DSL for text parsing and interpretation'
s.requirements << 'mime/types, A list of a lot of Mime Types'
View
28 spec/mail/configuration_spec.rb
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+require 'mail'
+
+describe Mail::Configuration do
+
+ describe "network configurations" do
+ it "should be a singleton for the whole class" do
+ config1 = Mail::Configuration.instance
+ config2 = Mail::Configuration.instance
+ config1.should == config2
+ end
+
+ it "should provide itself from Mail.defaults" do
+ config1 = Mail.defaults { smtp 'localhost' }
+ config2 = Mail.defaults
+ config1.should == config2
+ config1.smtp.host.should == 'localhost'
+ end
+
+ it "should be available from the Mail.defaults method" do
+ Mail.defaults { smtp 'myhost.com' }
+ Mail.defaults.smtp.host.should == 'myhost.com'
+ end
+
+ end
+
+end
View
11 spec/mail/message_spec.rb
@@ -1406,6 +1406,17 @@ def basic_email
end
describe "helper methods" do
+
+ it "should implement the spaceship operator on the date field" do
+ mail1 = Mail.new do
+ date 1.year.ago
+ end
+ mail2 = Mail.new do
+ date Time.now
+ end
+ [mail2, mail1].sort.should == [mail2, mail1]
+ end
+
it "should have a destinations method" do
mail = Mail.new do
to 'mikel@test.lindsaar.net'
View
78 spec/mail/network/deliverable_spec.rb
@@ -5,45 +5,41 @@
describe "Deliverable" do
- before(:each) do
- @deliverable = Class.new do
- include Mail::Deliverable
- attr_accessor :from, :to, :encoded
- end
- end
-
describe "general usage" do
before(:each) do
config = Mail.defaults do
- smtp 'smtp.mockup.com', 587
- enable_tls
+ smtp 'smtp.mockup.com', 587 do
+ enable_tls
+ end
end
end
it "should send emails from given settings" do
- from = 'roger@moore.com'
- to = 'marcel@amont.com'
- rfc2822 = 'invalid RFC2822'
- @deliverable.new.deliver!(from, to, rfc2822)
+ mail = Mail.deliver do
+ from 'roger@moore.com'
+ to 'marcel@amont.com'
+ subject 'invalid RFC2822'
+ end
- MockSMTP.deliveries[0][0].should == rfc2822
- MockSMTP.deliveries[0][1].should == from
- MockSMTP.deliveries[0][2].should == to
+ MockSMTP.deliveries[0][0].should == mail.encoded
+ MockSMTP.deliveries[0][1].should == mail.from.addresses.first
+ MockSMTP.deliveries[0][2].should == mail.destinations
end
- it "should send emails and get from/to/rfc2822 for the includer object" do
- deliverable = @deliverable.new
- deliverable.from = Mail::FromField.new('marcel@amont.com')
- deliverable.to = Mail::ToField.new('marcel@amont.com')
- deliverable.encoded = 'really invalid RFC2822'
+ it "should be able to send itself" do
+ mail = Mail.deliver do
+ from 'roger@moore.com'
+ to 'marcel@amont.com'
+ subject 'invalid RFC2822'
+ end
- deliverable.deliver!
+ mail.deliver!
- MockSMTP.deliveries[0][0].should == deliverable.encoded
- MockSMTP.deliveries[0][1].should == deliverable.from.addresses.first
- MockSMTP.deliveries[0][2].should == deliverable.to.addresses
+ MockSMTP.deliveries[0][0].should == mail.encoded
+ MockSMTP.deliveries[0][1].should == mail.from.addresses.first
+ MockSMTP.deliveries[0][2].should == mail.destinations
end
it "should raise if the SMTP configuration is not set" do
@@ -68,33 +64,37 @@ def redefine_verify_none(new_value)
# config can't be setup until redefined
redefine_verify_none(OpenSSL::SSL::SSLContext.new)
config = Mail.defaults do
- smtp 'smtp.mockup.com', 587
- enable_tls
+ smtp 'smtp.mockup.com', 587 do
+ enable_tls
+ end
end
- deliverable = @deliverable.new
- deliverable.from = Mail::FromField.new('marcel@amont.com')
- deliverable.to = Mail::ToField.new('marcel@amont.com')
- deliverable.encoded = 'really invalid RFC2822'
+ mail = Mail.deliver do
+ from 'roger@moore.com'
+ to 'marcel@amont.com'
+ subject 'invalid RFC2822'
+ end
- lambda{deliverable.deliver!}.should_not raise_error(TypeError)
+ doing { mail.deliver! }.should_not raise_error(TypeError)
end
it "should ignore OpenSSL::SSL::VERIFY_NONE if it is 0" do
# config can't be setup until redefined
redefine_verify_none(0)
config = Mail.defaults do
- smtp 'smtp.mockup.com', 587
- enable_tls
+ smtp 'smtp.mockup.com', 587 do
+ enable_tls
+ end
end
- deliverable = @deliverable.new
- deliverable.from = Mail::FromField.new('marcel@amont.com')
- deliverable.to = Mail::ToField.new('marcel@amont.com')
- deliverable.encoded = 'really invalid RFC2822'
+ mail = Mail.deliver do
+ from 'roger@moore.com'
+ to 'marcel@amont.com'
+ subject 'invalid RFC2822'
+ end
- lambda{deliverable.deliver!}.should_not raise_error(TypeError)
+ doing { mail.deliver! }.should_not raise_error(TypeError)
end
end
View
26 spec/mail/network/retrievable_spec.rb
@@ -6,47 +6,39 @@
describe "Retrievable" do
before(:each) do
- @retrievable = Class.new do
- include Mail::Retrievable
-
- attr_reader :raw_data
- def initialize(raw_data)
- @raw_data = raw_data
- end
- end
-
config = Mail.defaults do
- pop3 'pop3.mockup.com', 587
- enable_tls
+ pop3 'pop3.mockup.com', 587 do
+ enable_tls
+ end
end
end
it "should get emails with a given block" do
MockPOP3.should_not be_started
messages = []
- @retrievable.pop3_get_all_mail do |message|
+ Mail.get_all_mail do |message|
messages << message
end
- MockPOP3.popmails.collect {|p| p.pop}.sort.should == messages.collect {|m| m.raw_data}.sort
+ MockPOP3.popmails.length.should == messages.length
MockPOP3.should_not be_started
end
it "should get emails without a given block" do
MockPOP3.should_not be_started
- messages = @retrievable.pop3_get_all_mail
+ messages = Mail.get_all_mail
- MockPOP3.popmails.collect {|p| p.pop}.sort.should == messages.collect {|m| m.raw_data}.sort
+ MockPOP3.popmails.length.should == messages.length
MockPOP3.should_not be_started
end
it "should finish the POP3 connection is an exception is raised" do
MockPOP3.should_not be_started
doing do
- @retrievable.pop3_get_all_mail { |m| raise ArgumentError.new }
+ Mail.get_all_mail { |m| raise ArgumentError.new }
end.should raise_error
MockPOP3.should_not be_started
@@ -57,7 +49,7 @@ def initialize(raw_data)
pop3 ''
end
- doing { @retrievable.pop3_get_all_mail }.should raise_error
+ doing { Mail.get_all_mail }.should raise_error
end
end
View
73 spec/mail/network_spec.rb
@@ -5,40 +5,63 @@
describe "Mail" do
+ it "should provide a default network configuration without any setup" do
+ config = Mail.defaults
+ config.smtp.host.should_not be_blank
+ config.smtp.port.should_not be_blank
+ end
+
it "should accept a default POP3/SMTP configuration" do
config = Mail.defaults do
- smtp 'smtp.myhost.com'
- pop3 'pop.myhost.com'
- user 'roger'
- pass 'moore'
- disable_tls
+ smtp 'smtp.myhost.com' do
+ disable_tls
+ end
+
+ pop3 'pop.myhost.com' do
+ user 'roger'
+ pass 'moore'
+ disable_tls
+ end
- smtp.should == ['smtp.myhost.com', 25]
- pop3.should == ['pop.myhost.com', 110]
- user.should == 'roger'
- pass.should == 'moore'
- tls?.should == false
+ smtp.host.should == 'smtp.myhost.com'
+ smtp.port.should == 25
+ smtp.tls?.should == false
+ pop3.host.should == 'pop.myhost.com'
+ pop3.port.should == 110
+ pop3.user.should == 'roger'
+ pop3.pass.should == 'moore'
+ pop3.tls?.should == false
- enable_tls
+ pop3 do
+ enable_tls
+ end
- tls?.should == true
+ smtp do
+ enable_tls
+ end
+
+ smtp.host.should == 'smtp.myhost.com'
+ smtp.port.should == 25
+ smtp.tls?.should == true
+ pop3.host.should == 'pop.myhost.com'
+ pop3.port.should == 110
+ pop3.user.should == 'roger'
+ pop3.pass.should == 'moore'
+ pop3.tls?.should == true
end
-
- config.smtp.should == ['smtp.myhost.com', 25]
- config.pop3.should == ['pop.myhost.com', 110]
- config.user.should == 'roger'
- config.pass.should == 'moore'
- config.tls?.should == true
+
end
it "should send emails with SMTP" do
config = Mail.defaults do
- smtp 'smtp.mockup.com', 587
- enable_tls
+ smtp 'smtp.mockup.com', 587 do
+ enable_tls
+ end
end
- config.smtp.should == ['smtp.mockup.com', 587]
- config.tls?.should == true
+ config.smtp.host.should == 'smtp.mockup.com'
+ config.smtp.port.should == 587
+ config.smtp.tls?.should == true
message = Mail.deliver do
from 'roger@moore.com'
@@ -55,16 +78,16 @@
it "should retrieve all emails via POP3" do
config = Mail.defaults do
- pop3 'pop.mockup.com'
- enable_tls
+ pop3 'pop.mockup.com' do
+ enable_tls
+ end
end
messages = Mail.get_all_mail
messages.should_not be_empty
for message in messages
message.should be_instance_of(Mail::Message)
- message.raw_source.should =~ /test./
end
end
View
2 spec/spec_helper.rb
@@ -93,7 +93,7 @@ def initialize(rfc2822)
end
def pop
- @rfc2822
+ Mail.new(@rfc2822)
end
end

0 comments on commit e12737e

Please sign in to comment.
Something went wrong with that request. Please try again.