Skip to content

Commit fbe6c3c

Browse files
edavis10NZKoz
authored andcommitted
Adds a :file delivery_method to save email to a file on disk
Signed-off-by: Michael Koziarski <[email protected]> [rails#2438 state:committed]
1 parent 54e7f5b commit fbe6c3c

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

actionmailer/lib/action_mailer/base.rb

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'tmpdir'
2+
13
require "active_support/core_ext/class"
24
# Use the old layouts until actionmailer gets refactored
35
require "action_controller/legacy/layout"
@@ -224,9 +226,13 @@ module ActionMailer #:nodoc:
224226
# * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
225227
# * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
226228
#
229+
# * <tt>file_settings</tt> - Allows you to override options for the <tt>:file</tt> delivery method.
230+
# * <tt>:location</tt> - The directory into which emails will be written. Defaults to the application <tt>tmp/mails</tt>.
231+
#
227232
# * <tt>raise_delivery_errors</tt> - Whether or not errors should be raised if the email fails to be delivered.
228233
#
229-
# * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>.
234+
# * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, <tt>:test</tt>,
235+
# and <tt>:file</tt>.
230236
#
231237
# * <tt>perform_deliveries</tt> - Determines whether <tt>deliver_*</tt> methods are actually carried out. By default they are,
232238
# but this can be turned off to help functional testing.
@@ -279,6 +285,12 @@ class Base
279285
}
280286
cattr_accessor :sendmail_settings
281287

288+
@@file_settings = {
289+
:location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
290+
}
291+
292+
cattr_accessor :file_settings
293+
282294
@@raise_delivery_errors = true
283295
cattr_accessor :raise_delivery_errors
284296

@@ -724,6 +736,14 @@ def perform_delivery_sendmail(mail)
724736
def perform_delivery_test(mail)
725737
deliveries << mail
726738
end
739+
740+
def perform_delivery_file(mail)
741+
FileUtils.mkdir_p file_settings[:location]
742+
743+
(mail.to + mail.cc + mail.bcc).uniq.each do |to|
744+
File.open(File.join(file_settings[:location], to), 'a') { |f| f.write(mail) }
745+
end
746+
end
727747
end
728748

729749
Base.class_eval do

actionmailer/test/delivery_method_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class NonDefaultDeliveryMethodMailer < ActionMailer::Base
77
self.delivery_method = :sendmail
88
end
99

10+
class FileDeliveryMethodMailer < ActionMailer::Base
11+
self.delivery_method = :file
12+
end
13+
1014
class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
1115
def setup
1216
set_delivery_method :smtp
@@ -49,3 +53,21 @@ def test_should_be_the_set_delivery_method
4953
end
5054
end
5155

56+
class FileDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
57+
def setup
58+
set_delivery_method :smtp
59+
end
60+
61+
def teardown
62+
restore_delivery_method
63+
end
64+
65+
def test_should_be_the_set_delivery_method
66+
assert_equal :file, FileDeliveryMethodMailer.delivery_method
67+
end
68+
69+
def test_should_default_location_to_the_tmpdir
70+
assert_equal "#{Dir.tmpdir}/mails", ActionMailer::Base.file_settings[:location]
71+
end
72+
end
73+

actionmailer/test/mail_service_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,18 @@ def test_headers_removed_on_smtp_delivery
889889
assert_no_match %r{^Bcc: [email protected]}, MockSMTP.deliveries[0][0]
890890
end
891891

892+
def test_file_delivery_should_create_a_file
893+
ActionMailer::Base.delivery_method = :file
894+
tmp_location = ActionMailer::Base.file_settings[:location]
895+
896+
TestMailer.deliver_cc_bcc(@recipient)
897+
assert File.exists? tmp_location
898+
assert File.directory? tmp_location
899+
assert File.exists? File.join(tmp_location, @recipient)
900+
assert File.exists? File.join(tmp_location, '[email protected]')
901+
assert File.exists? File.join(tmp_location, '[email protected]')
902+
end
903+
892904
def test_recursive_multipart_processing
893905
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
894906
mail = TMail::Mail.parse(fixture)

0 commit comments

Comments
 (0)