public
Description: Support for multiple SMTP servers in ActionMailer
Clone URL: git://github.com/nakajima/many-mailers.git
Search Repo:
added many_mailers to plugin repos

git-svn-id: http://wush.net/svn/animoto/plugins/many_mailers@3702 
6c340329-a920-0410-8062-e7f800300859
patnakajima (author)
Thu Feb 21 00:20:12 -0800 2008
commit  e857bfdf1ccbfc284cdb2a90e4c6208217237d60
tree    3bdefcbbac65e712c829360689e8e8fa8bb62b45
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -0,0 +1,20 @@
0
+Copyright (c) 2008 [name of plugin creator]
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
0
@@ -0,0 +1,50 @@
0
+ManyMailers
0
+===========
0
+
0
+Multiple SMTP servers for ActionMailer.
0
+
0
+Example
0
+=======
0
+
0
+ ProjectMailer.with_settings(:internal) do |mailer|
0
+ mailer.deliver_notification(blah)
0
+ end
0
+
0
+USE
0
+===
0
+
0
+Create a YAML file in your config directory named "mail_servers.yml" Put
0
+your mail settings in there like so (see mail_servers.yaml in this plugin's
0
+test directory for a better example):
0
+
0
+ |default:
0
+ | address: mail.example.com
0
+ | port: 25
0
+ | domain: example.com
0
+ | user_name: test_user
0
+ | password: test_password
0
+ |
0
+ |internal:
0
+ | address: internal.example.com
0
+ | port: 25
0
+ | domain: example.com
0
+ | user_name: test_user
0
+ | password: test_password
0
+
0
+The plugin will look for a server named "default" to use by default.
0
+To specify the default server, do this:
0
+
0
+ ActionMailer.default_server = :internal
0
+
0
+ActionMailer (and all of its subclasses, ie, your mailers) will use this
0
+server by default.
0
+
0
+TODO
0
+====
0
+
0
+* Per-mailer settings (non-global)
0
+* ActionMailer::Base#with_settings shouldn't modify SMTP settings for other
0
+ mailers besides the one that called it.
0
+
0
+
0
+Copyright (c) 2008 Animoto Productions, released under the MIT license
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -0,0 +1,22 @@
0
+require 'rake'
0
+require 'rake/testtask'
0
+require 'rake/rdoctask'
0
+
0
+desc 'Default: run unit tests.'
0
+task :default => :test
0
+
0
+desc 'Test the many_mailers plugin.'
0
+Rake::TestTask.new(:test) do |t|
0
+ t.libs << 'lib'
0
+ t.pattern = 'test/**/*_test.rb'
0
+ t.verbose = true
0
+end
0
+
0
+desc 'Generate documentation for the many_mailers plugin.'
0
+Rake::RDocTask.new(:rdoc) do |rdoc|
0
+ rdoc.rdoc_dir = 'rdoc'
0
+ rdoc.title = 'ManyMailers'
0
+ rdoc.options << '--line-numbers' << '--inline-source'
0
+ rdoc.rdoc_files.include('README')
0
+ rdoc.rdoc_files.include('lib/**/*.rb')
0
+end
...
 
0
...
1
2
0
@@ -0,0 +1 @@
0
+require 'many_mailers'
0
\ No newline at end of file
...
 
0
...
1
2
0
@@ -0,0 +1 @@
0
+puts File.read(File.dirname(__FILE__) + '/README')
0
\ No newline at end of file
...
 
0
...
1
2
0
@@ -0,0 +1 @@
0
+require 'many_mailers/active_mailer/base'
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
0
@@ -0,0 +1,41 @@
0
+module Animoto
0
+ module ManyMailers
0
+ def self.included(base)
0
+ base.extend(ClassMethods)
0
+ base.class_eval do
0
+ @@mail_servers = { }
0
+ @@default_server = :default
0
+ cattr_accessor :mail_servers
0
+ cattr_reader :default_server
0
+ load_settings!
0
+ end
0
+ end
0
+
0
+ module ClassMethods
0
+ def load_settings!(file_path = "#{RAILS_ROOT}/config/mail_servers.yml")
0
+ begin
0
+ YAML.load_file(file_path).each { |key, value| mail_servers[key.to_sym] = value.to_options! }
0
+ self.smtp_settings = mail_servers[default_server]
0
+ rescue
0
+ logger.warn "=> \"#{file_path}\" not found! Using default SMTP settings (if any)."
0
+ end
0
+ end
0
+
0
+ def with_settings(name, &block)
0
+ init_settings = self.smtp_settings
0
+ use_server(name)
0
+ yield self
0
+ self.smtp_settings = init_settings
0
+ end
0
+
0
+ def use_server(name)
0
+ @@default_server = name
0
+ self.smtp_settings = mail_servers[@@default_server]
0
+ end
0
+
0
+ alias_method :default_server=, :use_server
0
+ end
0
+ end
0
+end
0
+
0
+ActionMailer::Base.send :include, Animoto::ManyMailers
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -0,0 +1,12 @@
0
+namespace :mailers do
0
+ desc "Show mailer servers"
0
+ task :show do
0
+ path = RAILS_ROOT + '/config/mail_servers.yml'
0
+ if File.file?(path)
0
+ puts "** displaying mailer settings"
0
+ puts File.read(path)
0
+ else
0
+ puts "** Can't find mail_servers.yml!"
0
+ end
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+ENV['RAILS_ENV'] ||= 'test'
0
+require File.dirname(__FILE__) + '/../../../../config/environment.rb'
0
+require 'fixtures/user_mailer'
0
+require 'test/unit'
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
0
@@ -0,0 +1,9 @@
0
+class UserMailer < ActionMailer::Base
0
+ def feedback(message)
0
+ from 'Service <service@example.com>'
0
+ recipients 'Feedback <feedback@example.com>'
0
+ subject "Feedback"
0
+ body :message => message
0
+ end
0
+end
0
+
...
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
@@ -0,0 +1,13 @@
0
+default:
0
+ address: mail.example.com
0
+ port: 25
0
+ domain: example.com
0
+ user_name: test_user
0
+ password: test_password
0
+
0
+internal:
0
+ address: internal.example.com
0
+ port: 25
0
+ domain: example.com
0
+ user_name: internal_test_user
0
+ password: internal_test_password
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
0
@@ -0,0 +1,41 @@
0
+require 'abstract_unit'
0
+
0
+class ManyMailersTest < Test::Unit::TestCase
0
+
0
+ def setup
0
+ ActionMailer::Base.default_server = :default
0
+ ActionMailer::Base.load_settings!(File.dirname(__FILE__) + '/mail_servers.yml')
0
+ end
0
+
0
+ def test_should_load_settings_properly
0
+ settings = ActionMailer::Base.mail_servers[:default]
0
+ assert_equal 'mail.example.com', settings[:address]
0
+ assert_equal 25, settings[:port]
0
+ assert_equal 'example.com', settings[:domain]
0
+ assert_equal 'test_user', settings[:user_name]
0
+ assert_equal 'test_password', settings[:password]
0
+
0
+ settings = ActionMailer::Base.mail_servers[:internal]
0
+ assert_equal 'internal.example.com', settings[:address]
0
+ assert_equal 'internal_test_user', settings[:user_name]
0
+ assert_equal 'internal_test_password', settings[:password]
0
+ end
0
+
0
+ def test_should_default_to_default
0
+ assert_equal ActionMailer::Base.mail_servers[:default], ActionMailer::Base.smtp_settings
0
+ end
0
+
0
+ def test_should_allow_custom_default_server
0
+ UserMailer.default_server = :internal
0
+ assert_equal ActionMailer::Base.mail_servers[:internal], UserMailer.smtp_settings
0
+ end
0
+
0
+ def test_should_allow_temporary_custom_server
0
+ assert_equal ActionMailer::Base.mail_servers[:default], UserMailer.smtp_settings
0
+ UserMailer.with_settings(:internal) do |mailer|
0
+ assert_equal ActionMailer::Base.mail_servers[:internal], UserMailer.smtp_settings
0
+ end
0
+ assert_equal ActionMailer::Base.mail_servers[:default], UserMailer.smtp_settings
0
+ end
0
+
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.