adzap-ar_mailer 1.4.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,12 @@
1
+ = 2.0.0
2
+
3
+ * Removed need to use ARMailer subclass. Just set the delivery method and you are ready to go. Backwards compatible with a deprecation notice if you subclass old ARMailer class.
4
+ * Only include SMTP TLS patch if Ruby version < 1.8.7 as it has an alternative. Changes based on Calvin Yu's [cyu] fork.
5
+ * Renamed default migration name to the modern Rails default
6
+ * Only authenticate if emails waiting to be sent
7
+ * Added --version switch to ar_sendmail binary
8
+ * Created a lighthouse account for this project (adzap fork only). See README.
9
+
1
10
  = 1.4.4
2
11
 
3
12
  * Exit init.d script with message if no mailers defined.
data/README.rdoc ADDED
@@ -0,0 +1,124 @@
1
+ = ar_mailer
2
+
3
+ A two-phase delivery agent for ActionMailer
4
+
5
+ Rubyforge Project:
6
+
7
+ http://rubyforge.org/projects/seattlerb
8
+
9
+ Documentation:
10
+
11
+ http://seattlerb.org/ar_mailer
12
+
13
+ and for forked additions
14
+
15
+ http://github.com/adzap/ar_mailer/wikis
16
+
17
+ Bugs:
18
+
19
+ http://adzap.lighthouseapp.com/projects/26997-ar_mailer
20
+
21
+ == About
22
+
23
+ Even delivering email to the local machine may take too long when you have to
24
+ send hundreds of messages. ar_mailer allows you to store messages into the
25
+ database for later delivery by a separate process, ar_sendmail.
26
+
27
+ == Installing ar_mailer (forked)
28
+
29
+ Before installing you will need to make sure the original gem is uninstalled as they can't coexist:
30
+
31
+ $ sudo gem uninstall ar_mailer
32
+
33
+ Install the gem from GitHub gems server:
34
+
35
+ First, if you haven't already:
36
+
37
+ $ sudo gem sources -a http://gems.github.com
38
+
39
+ Then
40
+
41
+ $ sudo gem install adzap-ar_mailer
42
+
43
+ For Rails >= 2.1, in your environment.rb:
44
+
45
+ config.gem "adzap-ar_mailer", :lib => 'action_mailer/ar_mailer', :source => 'http://gems.github.com'
46
+
47
+ For Rails 2.0, in an initializer file:
48
+
49
+ require 'action_mailer/ar_mailer'
50
+
51
+ == Usage
52
+
53
+ Go to your Rails project:
54
+
55
+ $ cd your_rails_project
56
+
57
+ Create a new migration:
58
+
59
+ $ ar_sendmail --create-migration
60
+
61
+ You'll need to redirect this into a file. If you want a different name
62
+ provide the --table-name option.
63
+
64
+ Create a new model:
65
+
66
+ $ ar_sendmail --create-model
67
+
68
+ You'll need to redirect this into a file. If you want a different name
69
+ provide the --table-name option.
70
+
71
+ You'll need to be sure to set the From address for your emails. Something
72
+ like:
73
+
74
+ def list_send(recipient)
75
+ from 'no_reply@example.com'
76
+ # ...
77
+
78
+ Edit config/environments/production.rb and set the delivery method:
79
+
80
+ config.action_mailer.delivery_method = :activerecord
81
+
82
+ Or if you need to, you can set each mailer class delivery method individually:
83
+
84
+ class MyMailer < ActionMailer::Base
85
+ self.delivery_method = :activerecord
86
+ end
87
+
88
+ This can be useful when using plugins like ExceptionNotification. Where it
89
+ might be foolish to tie the sending of the email alert to the database when the
90
+ database might be causing the exception being raised. In this instance you could
91
+ override ExceptionNofitier delivery method to be smtp or set the other
92
+ mailer classes to use ARMailer explicitly.
93
+
94
+ Then to run it:
95
+
96
+ $ ar_sendmail
97
+
98
+ You can also run it from cron with -o, or as a daemon with -d.
99
+
100
+ See <tt>ar_sendmail -h</tt> for full details.
101
+
102
+ === Alternate Mail Storage
103
+
104
+ If you want to set the ActiveRecord model that emails will be stored in,
105
+ see ActionMailer::Base.email_class=
106
+
107
+
108
+ === Help
109
+
110
+ See ar_sendmail -h for options to ar_sendmail.
111
+
112
+ NOTE: You may need to delete an smtp_tls.rb file if you have one lying
113
+ around. ar_mailer supplies it own.
114
+
115
+ == Run as a service (init.d/rc.d scripts)
116
+
117
+ For Linux both script and demo config files are in share/linux.
118
+ See ar_sendmail.conf for setting up your config. Copy the ar_sendmail file
119
+ to /etc/init.d/ and make it executable. Then for Debian based distros run
120
+ 'sudo update-rc.d ar_sendmail defaults' and it should work. Make sure you have
121
+ the config file /etc/ar_sendmail.conf in place before starting.
122
+
123
+ For FreeBSD or NetBSD script is share/bsd/ar_sendmail. This is old and does not
124
+ support the config file unless someone wants to submit a patch.
data/Rakefile CHANGED
@@ -11,20 +11,18 @@ ar_mailer_gemspec = Gem::Specification.new do |s|
11
11
  s.name = %q{ar_mailer}
12
12
  s.version = ActionMailer::ARSendmail::VERSION
13
13
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
14
- s.authors = ["Eric Hodel"]
15
- s.date = %q{2008-07-04}
14
+ s.authors = ["Eric Hodel", "Adam Meehan"]
16
15
  s.default_executable = %q{ar_sendmail}
17
16
  s.description = %q{Even delivering email to the local machine may take too long when you have to send hundreds of messages. ar_mailer allows you to store messages into the database for later delivery by a separate process, ar_sendmail.}
18
- s.email = %q{drbrain@segment7.net}
17
+ s.email = %q{adam.meehan@gmail.com}
19
18
  s.executables = ["ar_sendmail"]
20
- s.extra_rdoc_files = ["History.txt", "LICENSE.txt", "Manifest.txt", "README.txt"]
21
- s.files = ["History.txt", "LICENSE.txt", "Manifest.txt", "README.txt", "Rakefile", "bin/ar_sendmail", "lib/action_mailer/ar_mailer.rb", "lib/action_mailer/ar_sendmail.rb", "lib/smtp_tls.rb", "share/bsd/ar_sendmail", "share/linux/ar_sendmail", "share/linux/ar_sendmail.conf", "test/action_mailer.rb", "test/test_armailer.rb", "test/test_arsendmail.rb"]
19
+ s.extra_rdoc_files = ["History.txt", "LICENSE.txt", "Manifest.txt", "README.rdoc"]
20
+ s.files = ["History.txt", "LICENSE.txt", "Manifest.txt", "README.rdoc", "Rakefile", "bin/ar_sendmail", "lib/action_mailer/ar_mailer.rb", "lib/action_mailer/ar_sendmail.rb", "lib/smtp_tls.rb", "share/bsd/ar_sendmail", "share/linux/ar_sendmail", "share/linux/ar_sendmail.conf", "test/action_mailer.rb", "test/test_armailer.rb", "test/test_arsendmail.rb"]
22
21
  s.has_rdoc = true
23
- s.homepage = %q{http://seattlerb.org/ar_mailer}
24
- s.rdoc_options = ["--main", "README.txt"]
22
+ s.homepage = %q{http://github.com/adzap/ar_mailer}
23
+ s.rdoc_options = ["--main", "README.rdoc"]
25
24
  s.require_paths = ["lib"]
26
25
  s.rubyforge_project = %q{seattlerb}
27
- s.rubygems_version = %q{1.2.0}
28
26
  s.summary = %q{A two-phase delivery agent for ActionMailer}
29
27
  s.test_files = ["test/test_armailer.rb", "test/test_arsendmail.rb"]
30
28
  end
@@ -4,69 +4,17 @@ require 'action_mailer'
4
4
  # Adds sending email through an ActiveRecord table as a delivery method for
5
5
  # ActionMailer.
6
6
  #
7
- # == Converting to ActionMailer::ARMailer
8
- #
9
- # Go to your Rails project:
10
- #
11
- # $ cd your_rails_project
12
- #
13
- # Create a new migration:
14
- #
15
- # $ ar_sendmail --create-migration
16
- #
17
- # You'll need to redirect this into a file. If you want a different name
18
- # provide the --table-name option.
19
- #
20
- # Create a new model:
21
- #
22
- # $ ar_sendmail --create-model
23
- #
24
- # You'll need to redirect this into a file. If you want a different name
25
- # provide the --table-name option.
26
- #
27
- # Change your email classes to inherit from ActionMailer::ARMailer instead of
28
- # ActionMailer::Base:
29
- #
30
- # --- app/model/emailer.rb.orig 2006-08-10 13:16:33.000000000 -0700
31
- # +++ app/model/emailer.rb 2006-08-10 13:16:43.000000000 -0700
32
- # @@ -1,4 +1,4 @@
33
- # -class Emailer < ActionMailer::Base
34
- # +class Emailer < ActionMailer::ARMailer
35
- #
36
- # def comment_notification(comment)
37
- # from comment.author.email
38
- #
39
- # You'll need to be sure to set the From address for your emails. Something
40
- # like:
41
- #
42
- # def list_send(recipient)
43
- # from 'no_reply@example.com'
44
- # # ...
45
- #
46
- # Edit config/environment.rb and require ar_mailer.rb:
47
- #
48
- # require 'action_mailer/ar_mailer'
49
- #
50
- # Edit config/environments/production.rb and set the delivery agent:
51
- #
52
- # $ grep delivery_method config/environments/production.rb
53
- # ActionMailer::Base.delivery_method = :activerecord
54
- #
55
- # Run ar_sendmail:
56
- #
57
- # $ ar_sendmail
58
- #
59
- # You can also run it from cron with -o, or as a daemon with -d.
60
- #
61
- # See <tt>ar_sendmail -h</tt> for full details.
62
- #
63
- # == Alternate Mail Storage
64
- #
65
- # If you want to set the ActiveRecord model that emails will be stored in,
66
- # see ActionMailer::ARMailer::email_class=
67
7
 
68
8
  class ActionMailer::ARMailer < ActionMailer::Base
69
9
 
10
+ def self.inherited(sub)
11
+ logger.warn('The ActionMailer::ARMailer class has been deprecated. Will be removed in version 2.1. Just use ActionMailer::Base.')
12
+ end
13
+
14
+ end
15
+
16
+ class ActionMailer::Base
17
+
70
18
  @@email_class = Email
71
19
 
72
20
  ##
@@ -1,6 +1,6 @@
1
1
  require 'optparse'
2
2
  require 'net/smtp'
3
- require 'smtp_tls'
3
+ require 'smtp_tls' unless Net::SMTP.instance_methods.include?("enable_starttls_auto")
4
4
  require 'rubygems'
5
5
 
6
6
  class Object # :nodoc:
@@ -54,7 +54,7 @@ class ActionMailer::ARSendmail
54
54
  ##
55
55
  # The version of ActionMailer::ARSendmail you are running.
56
56
 
57
- VERSION = '1.4.3'
57
+ VERSION = '2.0.0'
58
58
 
59
59
  ##
60
60
  # Maximum number of times authentication will be consecutively retried
@@ -80,7 +80,7 @@ class ActionMailer::ARSendmail
80
80
  # Be verbose
81
81
 
82
82
  attr_accessor :verbose
83
-
83
+
84
84
  ##
85
85
  # ActiveRecord class that holds emails
86
86
 
@@ -112,7 +112,7 @@ class ActionMailer::ARSendmail
112
112
  def self.create_migration(table_name)
113
113
  require 'active_support'
114
114
  puts <<-EOF
115
- class Add#{table_name.classify} < ActiveRecord::Migration
115
+ class Create#{table_name.classify} < ActiveRecord::Migration
116
116
  def self.up
117
117
  create_table :#{table_name.tableize} do |t|
118
118
  t.column :from, :string
@@ -309,13 +309,17 @@ end
309
309
  usage opts
310
310
  end
311
311
 
312
+ opts.on("--version", "Version of ARMailer") do
313
+ usage "ar_mailer #{VERSION} (adzap fork)"
314
+ end
315
+
312
316
  opts.separator ''
313
317
  end
314
318
 
315
319
  opts.parse! args
316
320
 
317
321
  return options if options.include? :Migrate or options.include? :Model
318
-
322
+
319
323
  ENV['RAILS_ENV'] = options[:RailsEnv]
320
324
 
321
325
  Dir.chdir options[:Chdir] do
@@ -436,17 +440,26 @@ end
436
440
  # Delivers +emails+ to ActionMailer's SMTP server and destroys them.
437
441
 
438
442
  def deliver(emails)
439
- user = smtp_settings[:user] || smtp_settings[:user_name]
440
- Net::SMTP.start smtp_settings[:address], smtp_settings[:port],
441
- smtp_settings[:domain], user,
442
- smtp_settings[:password],
443
- smtp_settings[:authentication],
444
- smtp_settings[:tls] do |smtp|
443
+ settings = [
444
+ smtp_settings[:domain],
445
+ (smtp_settings[:user] || smtp_settings[:user_name]),
446
+ smtp_settings[:password],
447
+ smtp_settings[:authentication]
448
+ ]
449
+
450
+ smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
451
+ if smtp.respond_to?(:enable_starttls_auto)
452
+ smtp.enable_starttls_auto
453
+ else
454
+ settings << smtp_settings[:tls]
455
+ end
456
+
457
+ smtp.start(*settings) do |session|
445
458
  @failed_auth_count = 0
446
459
  until emails.empty? do
447
460
  email = emails.shift
448
461
  begin
449
- res = smtp.send_message email.mail, email.from, email.to
462
+ res = session.send_message email.mail, email.from, email.to
450
463
  email.destroy
451
464
  log "sent email %011d from %s to %s: %p" %
452
465
  [email.id, email.from, email.to, res]
@@ -454,7 +467,7 @@ end
454
467
  log "5xx error sending email %d, removing from queue: %p(%s):\n\t%s" %
455
468
  [email.id, e.message, e.class, e.backtrace.join("\n\t")]
456
469
  email.destroy
457
- smtp.reset
470
+ session.reset
458
471
  rescue Net::SMTPServerBusy => e
459
472
  log "server too busy, sleeping #{@delay} seconds"
460
473
  sleep delay
@@ -464,7 +477,7 @@ end
464
477
  email.save rescue nil
465
478
  log "error sending email %d: %p(%s):\n\t%s" %
466
479
  [email.id, e.message, e.class, e.backtrace.join("\n\t")]
467
- smtp.reset
480
+ session.reset
468
481
  end
469
482
  end
470
483
  end
@@ -530,7 +543,8 @@ end
530
543
  now = Time.now
531
544
  begin
532
545
  cleanup
533
- deliver find_emails
546
+ emails = find_emails
547
+ deliver(emails) unless emails.empty?
534
548
  rescue ActiveRecord::Transactions::TransactionError
535
549
  end
536
550
  break if @once
@@ -551,4 +565,3 @@ end
551
565
  end
552
566
 
553
567
  end
554
-
@@ -1,5 +1,5 @@
1
1
  require 'net/smtp'
2
- require 'smtp_tls'
2
+ require 'smtp_tls' unless Net::SMTP.instance_methods.include?("enable_starttls_auto")
3
3
  require 'time'
4
4
 
5
5
  class Net::SMTP
@@ -22,17 +22,20 @@ class Net::SMTP
22
22
 
23
23
  end
24
24
 
25
- def self.start(*args)
26
- @start_block.call if @start_block
27
- yield new(nil)
28
- end
29
-
30
25
  def self.on_send_message(&block)
31
26
  @send_message_block = block
32
27
  end
33
28
 
34
29
  def self.on_start(&block)
35
- @start_block = block
30
+ if block_given?
31
+ @start_block = block
32
+ else
33
+ @start_block
34
+ end
35
+ end
36
+
37
+ def self.clear_on_start
38
+ @start_block = nil
36
39
  end
37
40
 
38
41
  def self.reset
@@ -42,6 +45,11 @@ class Net::SMTP
42
45
  @reset_called = 0
43
46
  end
44
47
 
48
+ def start(*args)
49
+ self.class.on_start.call if self.class.on_start
50
+ yield self
51
+ end
52
+
45
53
  alias test_old_reset reset if instance_methods.include? 'reset'
46
54
 
47
55
  def reset
@@ -68,6 +76,10 @@ class ActionMailer::Base
68
76
 
69
77
  @server_settings = {}
70
78
 
79
+ class << self
80
+ attr_accessor :delivery_method
81
+ end
82
+
71
83
  def self.logger
72
84
  o = Object.new
73
85
  def o.info(arg) end
@@ -5,7 +5,8 @@ require 'action_mailer/ar_mailer'
5
5
  ##
6
6
  # Pretend mailer
7
7
 
8
- class Mailer < ActionMailer::ARMailer
8
+ class Mailer < ActionMailer::Base
9
+ self.delivery_method = :activerecord
9
10
 
10
11
  def mail
11
12
  @mail = Object.new
@@ -3,6 +3,7 @@ require 'action_mailer'
3
3
  require 'action_mailer/ar_sendmail'
4
4
  require 'rubygems'
5
5
  require 'test/zentest_assertions'
6
+ require 'mocha'
6
7
 
7
8
  class ActionMailer::ARSendmail
8
9
  attr_accessor :slept
@@ -22,6 +23,8 @@ class TestARSendmail < Test::Unit::TestCase
22
23
  @sm = ActionMailer::ARSendmail.new
23
24
  @sm.verbose = true
24
25
 
26
+ Net::SMTP.clear_on_start
27
+
25
28
  @include_c_e = ! $".grep(/config\/environment.rb/).empty?
26
29
  $" << 'config/environment.rb' unless @include_c_e
27
30
  end
@@ -36,7 +39,7 @@ class TestARSendmail < Test::Unit::TestCase
36
39
  end
37
40
 
38
41
  expected = <<-EOF
39
- class AddMail < ActiveRecord::Migration
42
+ class CreateMail < ActiveRecord::Migration
40
43
  def self.up
41
44
  create_table :mail do |t|
42
45
  t.column :from, :string
@@ -76,8 +79,8 @@ end
76
79
  :mail => 'body1'
77
80
  last = Email.create :from => nobody, :to => 'recip@h2.example.com',
78
81
  :mail => 'body2'
79
-
80
- last.last_send_attempt = Time.parse('Thu Aug 10 2006 11:40:05').to_i
82
+ last_attempt_time = Time.parse('Thu Aug 10 2006 11:40:05')
83
+ last.last_send_attempt = last_attempt_time.to_i
81
84
 
82
85
  out, err = util_capture do
83
86
  ActionMailer::ARSendmail.mailq 'Email'
@@ -98,7 +101,7 @@ Last send attempt: Thu Aug 10 11:40:05 %s 2006
98
101
  -- 0 Kbytes in 3 Requests.
99
102
  EOF
100
103
 
101
- expected = expected % Time.new.strftime('%z')
104
+ expected = expected % last_attempt_time.strftime('%z')
102
105
  assert_equal expected, out
103
106
  end
104
107
 
@@ -399,6 +402,12 @@ Last send attempt: Thu Aug 10 11:40:05 %s 2006
399
402
  assert_equal "sent email 00000000001 from from to to: \"queued\"\n", err
400
403
  end
401
404
 
405
+ def test_deliver_not_called_when_no_emails
406
+ sm = ActionMailer::ARSendmail.new({:Once => true})
407
+ sm.expects(:deliver).never
408
+ sm.run
409
+ end
410
+
402
411
  def test_deliver_auth_error
403
412
  Net::SMTP.on_start do
404
413
  e = Net::SMTPAuthenticationError.new 'try again'
metadata CHANGED
@@ -1,20 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adzap-ar_mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Hodel
8
+ - Adam Meehan
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2008-07-04 00:00:00 -07:00
13
+ date: 2009-03-12 00:00:00 -07:00
13
14
  default_executable: ar_sendmail
14
15
  dependencies: []
15
16
 
16
17
  description: Even delivering email to the local machine may take too long when you have to send hundreds of messages. ar_mailer allows you to store messages into the database for later delivery by a separate process, ar_sendmail.
17
- email: drbrain@segment7.net
18
+ email: adam.meehan@gmail.com
18
19
  executables:
19
20
  - ar_sendmail
20
21
  extensions: []
@@ -23,12 +24,12 @@ extra_rdoc_files:
23
24
  - History.txt
24
25
  - LICENSE.txt
25
26
  - Manifest.txt
26
- - README.txt
27
+ - README.rdoc
27
28
  files:
28
29
  - History.txt
29
30
  - LICENSE.txt
30
31
  - Manifest.txt
31
- - README.txt
32
+ - README.rdoc
32
33
  - Rakefile
33
34
  - bin/ar_sendmail
34
35
  - lib/action_mailer/ar_mailer.rb
@@ -41,11 +42,11 @@ files:
41
42
  - test/test_armailer.rb
42
43
  - test/test_arsendmail.rb
43
44
  has_rdoc: true
44
- homepage: http://seattlerb.org/ar_mailer
45
+ homepage: http://github.com/adzap/ar_mailer
45
46
  post_install_message:
46
47
  rdoc_options:
47
48
  - --main
48
- - README.txt
49
+ - README.rdoc
49
50
  require_paths:
50
51
  - lib
51
52
  required_ruby_version: !ruby/object:Gem::Requirement
data/README.txt DELETED
@@ -1,55 +0,0 @@
1
- = ar_mailer
2
-
3
- A two-phase delivery agent for ActionMailer
4
-
5
- Rubyforge Project:
6
-
7
- http://rubyforge.org/projects/seattlerb
8
-
9
- Documentation:
10
-
11
- http://seattlerb.org/ar_mailer
12
-
13
- and for forked additions
14
-
15
- http://github.com/adzap/ar_mailer/wikis
16
-
17
- Bugs:
18
-
19
- http://rubyforge.org/tracker/?func=add&group_id=1513&atid=5921
20
-
21
- == About
22
-
23
- Even delivering email to the local machine may take too long when you have to
24
- send hundreds of messages. ar_mailer allows you to store messages into the
25
- database for later delivery by a separate process, ar_sendmail.
26
-
27
- == Installing ar_mailer (forked)
28
-
29
- Install the gem from GitHub gems server:
30
-
31
- First, if you haven't already
32
-
33
- $ sudo gem sources -a http://gems.github.com
34
-
35
- Then
36
-
37
- $ sudo gem install adzap-ar_mailer
38
-
39
- See ActionMailer::ARMailer for instructions on converting to ARMailer.
40
-
41
- See ar_sendmail -h for options to ar_sendmail.
42
-
43
- NOTE: You may need to delete an smtp_tls.rb file if you have one lying
44
- around. ar_mailer supplies it own.
45
-
46
- === init.d/rc.d scripts
47
-
48
- For Linux both script and demo config files are in share/linux.
49
- See ar_sendmail.conf for setting up your config. Copy the ar_sendmail file
50
- to /etc/init.d/ and make it executable. Then for Debian based distros run
51
- 'sudo update-rc.d ar_sendmail defaults' and it should work. Make sure you have
52
- the config file /etc/ar_sendmail.conf in place before starting.
53
-
54
- For FreeBSD or NetBSD script is share/bsd/ar_sendmail. This is old and does not
55
- support the config file unless someone wants to submit a patch.