Skip to content
This repository
Browse code

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

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2438 state:committed]
  • Loading branch information...
commit fbe6c3c19553fd05edc904af62fbfc8aee1d907d 1 parent 54e7f5b
Eric Davis authored August 07, 2009 NZKoz committed August 08, 2009
22  actionmailer/lib/action_mailer/base.rb
... ...
@@ -1,3 +1,5 @@
  1
+require 'tmpdir'
  2
+
1 3
 require "active_support/core_ext/class"
2 4
 # Use the old layouts until actionmailer gets refactored
3 5
 require "action_controller/legacy/layout"
@@ -224,9 +226,13 @@ module ActionMailer #:nodoc:
224 226
   #   * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
225 227
   #   * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
226 228
   #
  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
+  #
227 232
   # * <tt>raise_delivery_errors</tt> - Whether or not errors should be raised if the email fails to be delivered.
228 233
   #
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>.
230 236
   #
231 237
   # * <tt>perform_deliveries</tt> - Determines whether <tt>deliver_*</tt> methods are actually carried out. By default they are,
232 238
   #   but this can be turned off to help functional testing.
@@ -279,6 +285,12 @@ class Base
279 285
     }
280 286
     cattr_accessor :sendmail_settings
281 287
 
  288
+    @@file_settings = {
  289
+      :location       => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
  290
+    }
  291
+
  292
+    cattr_accessor :file_settings
  293
+
282 294
     @@raise_delivery_errors = true
283 295
     cattr_accessor :raise_delivery_errors
284 296
 
@@ -724,6 +736,14 @@ def perform_delivery_sendmail(mail)
724 736
       def perform_delivery_test(mail)
725 737
         deliveries << mail
726 738
       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
727 747
   end
728 748
 
729 749
   Base.class_eval do
22  actionmailer/test/delivery_method_test.rb
@@ -7,6 +7,10 @@ class NonDefaultDeliveryMethodMailer < ActionMailer::Base
7 7
   self.delivery_method = :sendmail
8 8
 end
9 9
 
  10
+class FileDeliveryMethodMailer < ActionMailer::Base
  11
+  self.delivery_method = :file
  12
+end
  13
+
10 14
 class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
11 15
   def setup
12 16
     set_delivery_method :smtp
@@ -49,3 +53,21 @@ def test_should_be_the_set_delivery_method
49 53
   end
50 54
 end
51 55
 
  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
+
12  actionmailer/test/mail_service_test.rb
@@ -889,6 +889,18 @@ def test_headers_removed_on_smtp_delivery
889 889
     assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0]
890 890
   end
891 891
 
  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, 'nobody@loudthinking.com')
  901
+     assert File.exists? File.join(tmp_location, 'root@loudthinking.com')
  902
+   end
  903
+
892 904
   def test_recursive_multipart_processing
893 905
     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
894 906
     mail = TMail::Mail.parse(fixture)

1 note on commit fbe6c3c

José Valim
Owner

Awesome!

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