aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-08-09 18:39:44 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-08-09 18:39:44 +0100
commit2e50110eac439f3d5d292f1519ae7c79991eb91a (patch)
tree5ba10ebd6ddf0a7487754798993d3862ee9ec3b3 /actionmailer
parenta7f09bc12236d9e7bdc2ee34d5fe3c782d6ad385 (diff)
parentbb1e1776914edf3be7e46b55036c18a64595f919 (diff)
downloadrails-2e50110eac439f3d5d292f1519ae7c79991eb91a.tar.gz
rails-2e50110eac439f3d5d292f1519ae7c79991eb91a.tar.bz2
rails-2e50110eac439f3d5d292f1519ae7c79991eb91a.zip
Merge commit 'mainstream/master'
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/lib/action_mailer/base.rb31
-rw-r--r--actionmailer/test/delivery_method_test.rb22
-rw-r--r--actionmailer/test/mail_service_test.rb27
3 files changed, 69 insertions, 11 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index b5a0d0ab96..5ecefe7c09 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -1,3 +1,5 @@
+require 'tmpdir'
+
require "active_support/core_ext/class"
# Use the old layouts until actionmailer gets refactored
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>: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>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,
# but this can be turned off to help functional testing.
@@ -279,6 +285,12 @@ module ActionMailer #:nodoc:
}
cattr_accessor :sendmail_settings
+ @@file_settings = {
+ :location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
+ }
+
+ cattr_accessor :file_settings
+
@@raise_delivery_errors = true
cattr_accessor :raise_delivery_errors
@@ -499,7 +511,7 @@ module ActionMailer #:nodoc:
# ====
# TODO: Revisit this
# template_exists = @parts.empty?
- # template_exists ||= template_root.find_by_parts("#{mailer_name}/#{@template}")
+ # template_exists ||= template_root.find("#{mailer_name}/#{@template}")
# @body = render_message(@template, @body) if template_exists
# Finally, if there are other message parts and a textual body exists,
@@ -556,6 +568,7 @@ module ActionMailer #:nodoc:
@headers ||= {}
@body ||= {}
@mime_version = @@default_mime_version.dup if @@default_mime_version
+ @sent_on ||= Time.now
end
def render_template(template, body)
@@ -566,7 +579,7 @@ module ActionMailer #:nodoc:
@template = initialize_template_class(body)
layout = _pick_layout(layout, true) unless
ActionController::Base.exempt_from_layout.include?(template.handler)
- @template._render_template_with_layout(template, layout, {})
+ @template._render_template(template, layout, {})
ensure
@current_template_content_type = nil
end
@@ -585,14 +598,14 @@ module ActionMailer #:nodoc:
if file
prefix = mailer_name unless file =~ /\//
- template = view_paths.find_by_parts(file, {:formats => formats}, prefix)
+ template = view_paths.find(file, {:formats => formats}, prefix)
end
layout = _pick_layout(layout,
!template || ActionController::Base.exempt_from_layout.include?(template.handler))
if template
- @template._render_template_with_layout(template, layout, opts)
+ @template._render_template(template, layout, opts)
elsif inline = opts[:inline]
@template._render_inline(inline, layout, opts)
end
@@ -723,6 +736,14 @@ module ActionMailer #:nodoc:
def perform_delivery_test(mail)
deliveries << mail
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
Base.class_eval do
diff --git a/actionmailer/test/delivery_method_test.rb b/actionmailer/test/delivery_method_test.rb
index 0731512ea4..1b8c3ba523 100644
--- a/actionmailer/test/delivery_method_test.rb
+++ b/actionmailer/test/delivery_method_test.rb
@@ -7,6 +7,10 @@ class NonDefaultDeliveryMethodMailer < ActionMailer::Base
self.delivery_method = :sendmail
end
+class FileDeliveryMethodMailer < ActionMailer::Base
+ self.delivery_method = :file
+end
+
class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
def setup
set_delivery_method :smtp
@@ -49,3 +53,21 @@ class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
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
+
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb
index 30d1b836c4..5584afa8be 100644
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/mail_service_test.rb
@@ -18,7 +18,6 @@ class TestMailer < ActionMailer::Base
@recipients = recipient
@subject = "[Signed up] Welcome #{recipient}"
@from = "system@loudthinking.com"
- @sent_on = Time.local(2004, 12, 12)
@body["recipient"] = recipient
end
@@ -357,12 +356,14 @@ class ActionMailerTest < Test::Unit::TestCase
end
def test_signed_up
+ Time.stubs(:now => Time.now)
+
expected = new_mail
expected.to = @recipient
expected.subject = "[Signed up] Welcome #{@recipient}"
expected.body = "Hello there, \n\nMr. #{@recipient}"
expected.from = "system@loudthinking.com"
- expected.date = Time.local(2004, 12, 12)
+ expected.date = Time.now
created = nil
assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
@@ -573,12 +574,14 @@ class ActionMailerTest < Test::Unit::TestCase
@info_contents, @debug_contents = "", ""
end
- def info(str)
- @info_contents << str
+ def info(str = nil, &blk)
+ @info_contents << str if str
+ @info_contents << blk.call if block_given?
end
- def debug(str)
- @debug_contents << str
+ def debug(str = nil, &blk)
+ @debug_contents << str if str
+ @debug_contents << blk.call if block_given?
end
end
@@ -886,6 +889,18 @@ EOF
assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0]
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
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
mail = TMail::Mail.parse(fixture)