Adds a :file delivery_method to save email to a file on disk

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2438 state:committed]
This commit is contained in:
Eric Davis 2009-08-07 20:56:54 -07:00 committed by Michael Koziarski
parent 54e7f5ba43
commit fbe6c3c195
3 changed files with 55 additions and 1 deletions

View File

@ -1,3 +1,5 @@
require 'tmpdir'
require "active_support/core_ext/class" require "active_support/core_ext/class"
# Use the old layouts until actionmailer gets refactored # Use the old layouts until actionmailer gets refactored
require "action_controller/legacy/layout" require "action_controller/legacy/layout"
@ -224,9 +226,13 @@ module ActionMailer #:nodoc:
# * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>. # * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
# * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>. # * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
# #
# * <tt>file_settings</tt> - Allows you to override options for the <tt>:file</tt> delivery method.
# * <tt>:location</tt> - The directory into which emails will be written. Defaults to the application <tt>tmp/mails</tt>.
#
# * <tt>raise_delivery_errors</tt> - Whether or not errors should be raised if the email fails to be delivered. # * <tt>raise_delivery_errors</tt> - Whether or not errors should be raised if the email fails to be delivered.
# #
# * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>. # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, <tt>:test</tt>,
# and <tt>:file</tt>.
# #
# * <tt>perform_deliveries</tt> - Determines whether <tt>deliver_*</tt> methods are actually carried out. By default they are, # * <tt>perform_deliveries</tt> - Determines whether <tt>deliver_*</tt> methods are actually carried out. By default they are,
# but this can be turned off to help functional testing. # but this can be turned off to help functional testing.
@ -279,6 +285,12 @@ module ActionMailer #:nodoc:
} }
cattr_accessor :sendmail_settings cattr_accessor :sendmail_settings
@@file_settings = {
:location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
}
cattr_accessor :file_settings
@@raise_delivery_errors = true @@raise_delivery_errors = true
cattr_accessor :raise_delivery_errors cattr_accessor :raise_delivery_errors
@ -724,6 +736,14 @@ module ActionMailer #:nodoc:
def perform_delivery_test(mail) def perform_delivery_test(mail)
deliveries << mail deliveries << mail
end end
def perform_delivery_file(mail)
FileUtils.mkdir_p file_settings[:location]
(mail.to + mail.cc + mail.bcc).uniq.each do |to|
File.open(File.join(file_settings[:location], to), 'a') { |f| f.write(mail) }
end
end
end end
Base.class_eval do Base.class_eval do

View File

@ -7,6 +7,10 @@ class NonDefaultDeliveryMethodMailer < ActionMailer::Base
self.delivery_method = :sendmail self.delivery_method = :sendmail
end end
class FileDeliveryMethodMailer < ActionMailer::Base
self.delivery_method = :file
end
class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
def setup def setup
set_delivery_method :smtp set_delivery_method :smtp
@ -49,3 +53,21 @@ class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
end end
end end
class FileDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
def setup
set_delivery_method :smtp
end
def teardown
restore_delivery_method
end
def test_should_be_the_set_delivery_method
assert_equal :file, FileDeliveryMethodMailer.delivery_method
end
def test_should_default_location_to_the_tmpdir
assert_equal "#{Dir.tmpdir}/mails", ActionMailer::Base.file_settings[:location]
end
end

View File

@ -889,6 +889,18 @@ EOF
assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0] assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0]
end end
def test_file_delivery_should_create_a_file
ActionMailer::Base.delivery_method = :file
tmp_location = ActionMailer::Base.file_settings[:location]
TestMailer.deliver_cc_bcc(@recipient)
assert File.exists? tmp_location
assert File.directory? tmp_location
assert File.exists? File.join(tmp_location, @recipient)
assert File.exists? File.join(tmp_location, 'nobody@loudthinking.com')
assert File.exists? File.join(tmp_location, 'root@loudthinking.com')
end
def test_recursive_multipart_processing def test_recursive_multipart_processing
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7") fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
mail = TMail::Mail.parse(fixture) mail = TMail::Mail.parse(fixture)