dvdplm-ar_mailer 2.1.2 → 2.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/action_mailer/ar_sendmail.rb +31 -17
- metadata +1 -1
@@ -7,18 +7,18 @@ require 'rubygems'
|
|
7
7
|
# Hack in RSET
|
8
8
|
|
9
9
|
module Net # :nodoc:
|
10
|
-
class SMTP # :nodoc:
|
10
|
+
class SMTP # :nodoc:
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
unless instance_methods.include? 'reset' then
|
13
|
+
##
|
14
|
+
# Resets the SMTP connection.
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
def reset
|
17
|
+
getok 'RSET'
|
18
|
+
end
|
18
19
|
end
|
19
|
-
end
|
20
20
|
|
21
|
-
end
|
21
|
+
end
|
22
22
|
end
|
23
23
|
|
24
24
|
module ActionMailer; end # :nodoc:
|
@@ -46,7 +46,7 @@ class ActionMailer::ARSendmail
|
|
46
46
|
##
|
47
47
|
# The version of ActionMailer::ARSendmail you are running.
|
48
48
|
|
49
|
-
VERSION = '2.1.
|
49
|
+
VERSION = '2.1.3'
|
50
50
|
|
51
51
|
##
|
52
52
|
# Maximum number of times authentication will be consecutively retried
|
@@ -224,6 +224,7 @@ EOF
|
|
224
224
|
options[:RailsEnv] = ENV['RAILS_ENV']
|
225
225
|
options[:TableName] = 'Email'
|
226
226
|
options[:Pidfile] = options[:Chdir] + '/log/ar_sendmail.pid'
|
227
|
+
options[:DryRun] = false
|
227
228
|
|
228
229
|
opts = OptionParser.new do |opts|
|
229
230
|
opts.banner = "Usage: #{name} [options]"
|
@@ -274,18 +275,24 @@ EOF
|
|
274
275
|
|
275
276
|
opts.on("-d", "--daemonize",
|
276
277
|
"Run as a daemon process",
|
277
|
-
"Default: #{options[:Daemon]}") do |daemon|
|
278
|
+
"Default: #{options[:Daemon]}\n") do |daemon|
|
278
279
|
options[:Daemon] = true
|
279
280
|
end
|
280
281
|
|
281
282
|
opts.on("-p", "--pidfile PIDFILE",
|
282
283
|
"Set the pidfile location",
|
283
|
-
"Default: #{options[:Chdir]}#{options[:Pidfile]}", String) do |pidfile|
|
284
|
+
"Default: #{options[:Chdir]}#{options[:Pidfile]}\n", String) do |pidfile|
|
284
285
|
options[:Pidfile] = pidfile
|
285
286
|
end
|
287
|
+
|
288
|
+
opts.on("-f", "--dry-run",
|
289
|
+
"Dry run: don't send any emails",
|
290
|
+
"Default: Deliver all available emails\n", options[:DryRun]) do |dry_run|
|
291
|
+
options[:DryRun] = dry_run
|
292
|
+
end
|
286
293
|
|
287
294
|
opts.on( "--mailq",
|
288
|
-
"Display a list of emails waiting to be sent") do |mailq|
|
295
|
+
"Display a list of emails waiting to be sent\n") do |mailq|
|
289
296
|
options[:MailQ] = true
|
290
297
|
end
|
291
298
|
|
@@ -438,6 +445,8 @@ EOF
|
|
438
445
|
# <tt>:TableName</tt>:: Table name that stores the emails
|
439
446
|
# <tt>:Once</tt>:: Only attempt to deliver emails once when run is called
|
440
447
|
# <tt>:Verbose</tt>:: Be verbose.
|
448
|
+
# <tt>:MaxRetryAge</tt>:: Retry sending unsent emails after n seconds from last attempt (defaults to 300s).
|
449
|
+
# <tt>:MaxArchiveAge</tt>:: Store successfully sent emails in the DB for this long (defaults to 30 days).
|
441
450
|
|
442
451
|
def initialize(options = {})
|
443
452
|
options[:Delay] ||= 60
|
@@ -452,6 +461,7 @@ EOF
|
|
452
461
|
@verbose = options[:Verbose]
|
453
462
|
@max_retry_age = options[:MaxRetryAge]
|
454
463
|
@max_archive_age = options[:MaxArchiveAge]
|
464
|
+
@dry_run = options[:DryRun]
|
455
465
|
|
456
466
|
@failed_auth_count = 0
|
457
467
|
end
|
@@ -469,7 +479,7 @@ EOF
|
|
469
479
|
return if @max_retry_age == 0
|
470
480
|
timeout = Time.now - @max_retry_age
|
471
481
|
conditions = ['last_send_attempt > 0 AND created_at < ? AND sent_at IS NULL', timeout]
|
472
|
-
mail = @email_class.update_all({:failed => true}, conditions)
|
482
|
+
mail = @email_class.update_all({:failed => true}, conditions) unless @dry_run
|
473
483
|
|
474
484
|
log "#{self.class}#cleanup_unsent expired #{mail} emails from the queue"
|
475
485
|
end
|
@@ -478,7 +488,7 @@ EOF
|
|
478
488
|
def cleanup_old_emails
|
479
489
|
conditions = ['sent_at < ?', @max_archive_age.days.ago.to_date]
|
480
490
|
log "#{self.class}#cleanup_old_emails Deleting #{@email_class.count(:conditions => conditions)} emails from the archive."
|
481
|
-
@email_class.delete_all(conditions)
|
491
|
+
@email_class.delete_all(conditions) unless @dry_run
|
482
492
|
end
|
483
493
|
|
484
494
|
##
|
@@ -507,7 +517,11 @@ EOF
|
|
507
517
|
email.last_send_attempt = Time.now.to_i
|
508
518
|
email.increment :attempts
|
509
519
|
begin
|
510
|
-
|
520
|
+
if @dry_run
|
521
|
+
res = 'DRY RUN'
|
522
|
+
else
|
523
|
+
res = session.send_message email.mail, email.from, email.to
|
524
|
+
end
|
511
525
|
email.failed = false
|
512
526
|
email.sent_at = Time.now
|
513
527
|
email.success_status = res.string rescue res.inspect # NOTE: some rubies return an Net::HTTP::Response here, others just a stupid String (dvd, 11-05-2009)
|
@@ -531,7 +545,7 @@ EOF
|
|
531
545
|
[email.id, e.message, e.class, e.backtrace.join("\n\t")]
|
532
546
|
session.reset
|
533
547
|
end
|
534
|
-
email.save!
|
548
|
+
email.save! unless @dry_run
|
535
549
|
end
|
536
550
|
|
537
551
|
end
|
@@ -584,7 +598,7 @@ EOF
|
|
584
598
|
|
585
599
|
def log(message)
|
586
600
|
$stderr.puts message if @verbose
|
587
|
-
ActionMailer::Base.logger.info "ar_sendmail ==> #{message}"
|
601
|
+
ActionMailer::Base.logger.info "ar_sendmail, #{Time.now.strftime("%b %d %H:%M:%S")} ==> #{message}"
|
588
602
|
end
|
589
603
|
|
590
604
|
##
|