ar_mailer_revised 0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +16 -0
- data/README.md +54 -70
- data/ar_mailer_revised.gemspec +7 -2
- data/bin/ar_sendmail +2 -7
- data/lib/action_mailer/ar_mailer.rb +45 -18
- data/lib/ar_mailer_revised.rb +3 -2
- data/lib/ar_mailer_revised/email_scaffold.rb +17 -11
- data/lib/ar_mailer_revised/helpers/command_line.rb +198 -0
- data/lib/ar_mailer_revised/helpers/general.rb +81 -0
- data/lib/ar_mailer_revised/mailman.rb +263 -0
- data/lib/ar_mailer_revised/version.rb +1 -1
- data/lib/generators/ar_mailer_revised/install_generator.rb +37 -0
- data/{generators → lib/generators}/ar_mailer_revised/templates/migration.rb +0 -0
- data/{generators → lib/generators}/ar_mailer_revised/templates/model.rb +1 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/mailers/test_mailer.rb +32 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/models/email.rb +31 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +83 -0
- data/test/dummy/config/environments/test.rb +34 -0
- data/{generators/ar_mailer_revised/templates/initializer.rb → test/dummy/config/initializers/ar_mailer_revised.rb} +2 -2
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20140518140150_create_emails.rb +35 -0
- data/test/dummy/db/schema.rb +28 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/log/development.log +119 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/mailers/previews/test_mailer_preview.rb +4 -0
- data/test/dummy/test/mailers/test_mailer_test.rb +54 -0
- data/test/generators/install_generator_test.rb +14 -0
- metadata +184 -12
- data/generators/ar_mailer_revised/ar_mailer_revised_generator.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0aef767abd393af9428a1bbea305cd024d6cf994
|
4
|
+
data.tar.gz: 4011b3fa47058260ebdeb930c6c2c49ba9396539
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7f9318aa6523598867b1d62c968d8d3fdc49ecd3842a0922c5525b7a0a87539f2a5c85c95a5048bd4858f8ed8d0687c3eb3a22aadfb58e66f863ce406d5548f
|
7
|
+
data.tar.gz: 3b51d939495ce060b28aae0fac4df7df49eeec32374d881ce67533badc69d41b6edee65260062f526cd2db53e70b35e0f34d1bb730d4308ab9c285824ad05def
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# ArMailerRevised
|
2
2
|
|
3
|
-
[
|
3
|
+
[](https://codeclimate.com/github/Stex/ar_mailer_revised)
|
4
|
+

|
5
|
+
|
6
|
+
[ArMailer](https://github.com/seattlerb/ar_mailer) was a great gem which allows you to store emails in your application's database and batch deliver
|
4
7
|
them later using a background task.
|
5
8
|
|
6
9
|
However, it was not compatible with newer versions of Rails and also lacking some of the functionality I needed in my applications.
|
@@ -11,11 +14,9 @@ Especially, I wanted to use
|
|
11
14
|
* custom SMTP settings per email
|
12
15
|
* custom attributes directly in the email record to keep track of them
|
13
16
|
|
14
|
-
|
15
|
-
It does (and always will) **only support generating emails, not actually sending them**.
|
17
|
+
ArMailerRevised contains this functionality, currently only for Rails >= 4.
|
16
18
|
|
17
|
-
|
18
|
-
to let older existing applications use the email queue.
|
19
|
+
**Important**: ArMailerRevised does only deliver emails using SMTP, not a sendmail executable.
|
19
20
|
|
20
21
|
## Installation
|
21
22
|
|
@@ -41,11 +42,13 @@ ArMailerRevised needs a few things to work correctly:
|
|
41
42
|
|
42
43
|
All of them can be created using a generator:
|
43
44
|
|
44
|
-
$
|
45
|
+
$ rails g ar_mailer_revised:install MODEL_NAME
|
45
46
|
|
46
|
-
If you
|
47
|
-
|
48
|
-
|
47
|
+
If you don't specify a model name, the default name `Email` is used.
|
48
|
+
|
49
|
+
Please migrate your database after the migration was created.
|
50
|
+
If you need custom attributes (see below) in your email table, please
|
51
|
+
add them to the migration before migrating.
|
49
52
|
|
50
53
|
$ rake db:migrate
|
51
54
|
|
@@ -54,19 +57,15 @@ Otherwise, migrate your application before continuing.
|
|
54
57
|
First of all, you have to set ActionMailer to use the gem's delivery method.
|
55
58
|
This can be done per environment or globally for the application using either
|
56
59
|
|
57
|
-
|
58
|
-
config.action_mailer.delivery_method = :activerecord
|
59
|
-
```
|
60
|
+
config.action_mailer.delivery_method = :activerecord
|
60
61
|
|
61
62
|
or - not inside a configuration file
|
62
63
|
|
63
|
-
|
64
|
-
ActionMailer::Base.delivery_method = :activerecord
|
65
|
-
```
|
64
|
+
ActionMailer::Base.delivery_method = :activerecord
|
66
65
|
|
67
66
|
### SMTP-Settings
|
68
67
|
|
69
|
-
ArMailerRevised accepts SMTP settings in the form ActionMailer::Base
|
68
|
+
ArMailerRevised accepts SMTP settings in the form ActionMailer::Base does.
|
70
69
|
Application wide settings have to be stored in ActionMailer::Base.smtp_settings.
|
71
70
|
Please have a look at [ActionMailer::Base](http://api.rubyonrails.org/classes/ActionMailer/Base.html)
|
72
71
|
|
@@ -86,19 +85,16 @@ Below will be a growing list of demo SMTP settings for popular providers.
|
|
86
85
|
## Creating Emails
|
87
86
|
|
88
87
|
ArMailerRevised uses the normal ActionMailer::Base templates, so you can write
|
89
|
-
|
90
|
-
On delivering, the email will be stored in the database and not being sent
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
100
|
-
end
|
101
|
-
```
|
88
|
+
deliver-methods like you would for direct email sending.
|
89
|
+
On delivering, the email will be stored in the database and not being sent diretly.
|
90
|
+
|
91
|
+
class TestMailer < ActionMailer::Base
|
92
|
+
default from: 'from@example.com'
|
93
|
+
|
94
|
+
def basic_email
|
95
|
+
mail(to: 'basic_email@example.com', subject: 'Basic Email Subject', body: 'Basic Email Body')
|
96
|
+
end
|
97
|
+
end
|
102
98
|
|
103
99
|
### Setting a custom delivery time
|
104
100
|
|
@@ -107,16 +103,10 @@ the resulting email record. One of them is +ar_mailer_delivery_time+.
|
|
107
103
|
This method sets a time which determines the earliest sending time for this email,
|
108
104
|
in other words: If you set this time, the email won't be sent prior to it.
|
109
105
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
from 'test@example.com'
|
115
|
-
to recipients
|
116
|
-
subject 'Delayed Email'
|
117
|
-
body 'Yes, I am indeed delayed.'
|
118
|
-
end
|
119
|
-
```
|
106
|
+
def delayed_email
|
107
|
+
ar_mailer_delivery_time Time.now + 2.hours
|
108
|
+
mail(to: 'delayed_email@example.com', subject: 'Delayed Email Subject', :body => 'Delayed Email Body')
|
109
|
+
end
|
120
110
|
|
121
111
|
**Important**: It may happen that the Rails logging output of the generated mail may still contain
|
122
112
|
custom attributes (like the delivery time) in its header. This happens because ActionMailer will
|
@@ -127,24 +117,19 @@ log the email before actually delivering it. The generated email will **not** co
|
|
127
117
|
It is possible to set own SMTP settings for each email in the system which will then be used for delivery.
|
128
118
|
These settings may contain everything the global settings do (see above).
|
129
119
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
to recipients
|
144
|
-
subject 'Custom Settings Email'
|
145
|
-
body 'I use custom settings (but will go back to the default ones if these are incorrect)'
|
146
|
-
end
|
147
|
-
```
|
120
|
+
def custom_smtp_email
|
121
|
+
ar_mailer_smtp_settings({
|
122
|
+
:address => 'localhost',
|
123
|
+
:port => 25,
|
124
|
+
:domain => 'localhost.localdomain',
|
125
|
+
:user_name => 'some.user',
|
126
|
+
:password => 'some.password',
|
127
|
+
:authentication => :plain,
|
128
|
+
:enable_starttls_auto => true
|
129
|
+
})
|
130
|
+
|
131
|
+
mail(to: 'custom_smtp_email@example.com', subject: 'Custom SMTP Email Subject', :body => 'Custom SMTP Email Body')
|
132
|
+
end
|
148
133
|
|
149
134
|
**Important**: As the mailer has to use the password to connect to the SMTP server, it is stored in the database in plain text!
|
150
135
|
If this means a security issue to you, please use only the global settings which are loaded from the environment and not stored in the database.
|
@@ -160,23 +145,22 @@ You can add custom attributes to the email table simply by altering the generate
|
|
160
145
|
|
161
146
|
In the email delivering method, these attributes may then be filled with the actual data using the `ar_mailer_attribute` helper method:
|
162
147
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
from 'test@example.com'
|
168
|
-
to recipients
|
169
|
-
subject 'Custom Attribute Email'
|
170
|
-
body 'I have a custom attribute.'
|
171
|
-
end
|
172
|
-
```
|
148
|
+
def custom_attribute_email
|
149
|
+
ar_mailer_attribute :a_number, 42
|
150
|
+
mail(to: 'custom_attribute_email@example.com', subject: 'Custom Attribute Email Subject', :body => 'Custom Attribute Email Body')
|
151
|
+
end
|
173
152
|
|
174
153
|
### Sending Emails
|
175
154
|
|
176
|
-
|
155
|
+
ArMailerRevised comes with an executable called `ar_sendmail` which can
|
156
|
+
be accessed from the application's root directory.
|
157
|
+
|
158
|
+
It accepts the argument `-h` (or `--help`), showing all available options.
|
159
|
+
If you call it without options, it will run a single batch sending in the foreground and exist afterwards.
|
177
160
|
|
178
|
-
|
179
|
-
|
161
|
+
There will be daemon functionality in the future (mostly to avoid loading the application envirionment
|
162
|
+
every single time emails are being sent), for now I suggest using a gem like [whenever](https://github.com/javan/whenever)
|
163
|
+
to run the executable every X minutes.
|
180
164
|
|
181
165
|
### SMTP settings for common providers (to be extended)
|
182
166
|
|
data/ar_mailer_revised.gemspec
CHANGED
@@ -23,8 +23,13 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency 'rake', '~> 10.3'
|
24
24
|
spec.add_development_dependency 'yard', '~> 0.8'
|
25
25
|
spec.add_development_dependency 'redcarpet', '~> 2.3'
|
26
|
+
spec.add_development_dependency 'sqlite3'
|
27
|
+
spec.add_development_dependency 'minitest'
|
28
|
+
spec.add_development_dependency 'shoulda'
|
26
29
|
|
27
|
-
spec.add_dependency 'rails', '~>
|
30
|
+
spec.add_dependency 'rails', '~> 4'
|
31
|
+
spec.add_dependency 'log4r'
|
32
|
+
spec.add_dependency 'hirb'
|
28
33
|
|
29
|
-
spec.required_ruby_version = '
|
34
|
+
spec.required_ruby_version = '~> 2'
|
30
35
|
end
|
data/bin/ar_sendmail
CHANGED
@@ -1,10 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
ar_sendmail is currently only available in the rails 4 version of ar_mailer_revised.
|
6
|
-
The rails 2 version was only created to support email creation on older systems
|
7
|
-
while still keeping the mail queue on a more up-to-date one.
|
8
|
-
|
9
|
-
EOS
|
3
|
+
require 'ar_mailer_revised/mailman'
|
4
|
+
ArMailerRevised::Mailman.run
|
10
5
|
|
@@ -2,6 +2,7 @@
|
|
2
2
|
# Adds sending email through an ActiveRecord table as a delivery method for
|
3
3
|
# ActionMailer.
|
4
4
|
#
|
5
|
+
|
5
6
|
class ActionMailer::Base
|
6
7
|
#
|
7
8
|
# Sets a custom email class attribute. It can be used
|
@@ -44,31 +45,57 @@ class ActionMailer::Base
|
|
44
45
|
# Sets or simply returns an ar_mailer_setting
|
45
46
|
#
|
46
47
|
def ar_mailer_setting(key, value = nil)
|
47
|
-
|
48
|
+
if headers[:ar_mailer_settings]
|
49
|
+
settings = YAML.load(headers[:ar_mailer_settings]).stringify_keys
|
50
|
+
else
|
51
|
+
settings = {}
|
52
|
+
end
|
48
53
|
|
49
54
|
if value
|
50
|
-
|
55
|
+
settings[key.to_s] = value
|
56
|
+
headers[:ar_mailer_settings] = YAML.dump(settings)
|
51
57
|
else
|
52
|
-
|
58
|
+
settings[key.to_s]
|
53
59
|
end
|
54
60
|
end
|
55
61
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# This class contains the actual sending functionality
|
66
|
+
#
|
67
|
+
module ActionMailer
|
68
|
+
class DeliveryMethodActiveRecord
|
69
|
+
#
|
70
|
+
# The delivery method seems to be called with a settings hash from the mail gem.
|
71
|
+
#
|
72
|
+
def initialize(settings)
|
73
|
+
@settings = settings
|
74
|
+
end
|
67
75
|
|
68
|
-
|
76
|
+
#
|
77
|
+
# Actually creates the email record in the database
|
78
|
+
#
|
79
|
+
def deliver!(mail)
|
80
|
+
if mail['ar_mailer_settings']
|
81
|
+
ar_settings = YAML.load(mail['ar_mailer_settings'].value).stringify_keys
|
82
|
+
mail['ar_mailer_settings'] = nil
|
83
|
+
else
|
84
|
+
ar_settings = {}
|
85
|
+
end
|
69
86
|
|
70
|
-
|
71
|
-
|
87
|
+
email_options = {}
|
88
|
+
email_options[:delivery_time] = ar_settings.delete('delivery_time')
|
89
|
+
email_options[:smtp_settings] = ar_settings.delete('smtp_settings')
|
90
|
+
email_options[:mail] = mail.encoded
|
91
|
+
email_options[:from] = (mail['return-path'] && mail['return-path'].spec) || mail.from.first
|
92
|
+
email_options.reverse_merge!(ar_settings['custom_attributes'] || {})
|
93
|
+
|
94
|
+
mail.destinations.each do |destination|
|
95
|
+
ArMailerRevised.email_class.create!(email_options.merge({:to => destination}))
|
96
|
+
end
|
72
97
|
end
|
73
98
|
end
|
74
|
-
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
data/lib/ar_mailer_revised.rb
CHANGED
@@ -3,6 +3,8 @@ require 'action_mailer/ar_mailer'
|
|
3
3
|
require 'ar_mailer_revised/email_scaffold'
|
4
4
|
|
5
5
|
#Register the new delivery method
|
6
|
+
ActionMailer::Base.add_delivery_method :activerecord, ActionMailer::DeliveryMethodActiveRecord
|
7
|
+
|
6
8
|
module ArMailerRevised
|
7
9
|
def self.configuration(&proc)
|
8
10
|
@@config ||= OpenStruct.new({
|
@@ -29,7 +31,6 @@ module ArMailerRevised
|
|
29
31
|
# The email class' name
|
30
32
|
#
|
31
33
|
def self.email_class_name
|
32
|
-
|
34
|
+
@@config.email_class.classify
|
33
35
|
end
|
34
|
-
|
35
36
|
end
|
@@ -1,17 +1,30 @@
|
|
1
1
|
#
|
2
2
|
# Helper methods for the chosen email class
|
3
3
|
#
|
4
|
+
|
4
5
|
module ArMailerRevised
|
5
6
|
module EmailScaffold
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
serialize :smtp_settings
|
9
12
|
|
10
13
|
#Only emails which are to be sent immediately
|
11
|
-
|
14
|
+
scope :without_delayed, lambda { where(:delivery_time => nil) }
|
15
|
+
|
16
|
+
#All emails which are ready to be sent.
|
17
|
+
#They are automatically sorted so that emails which already had a send attempt
|
18
|
+
#will be last in the queue as they might fail again.
|
19
|
+
scope :ready_to_deliver, lambda { where('delivery_time IS NULL OR delivery_time <= ?', Time.now).order('last_send_attempt ASC') }
|
12
20
|
|
13
21
|
#All emails which are to be sent in the future
|
14
|
-
|
22
|
+
scope :delayed, lambda { where('delivery_time > ?', Time.now) }
|
23
|
+
|
24
|
+
#Applies a +limit+ to the finder if batch_size is set
|
25
|
+
scope :with_batch_size, lambda { |batch_size|
|
26
|
+
batch_size ? {:limit => batch_size} : {}
|
27
|
+
}
|
15
28
|
end
|
16
29
|
|
17
30
|
#
|
@@ -21,12 +34,5 @@ module ArMailerRevised
|
|
21
34
|
def previously_attempted?
|
22
35
|
last_send_attempt > 0
|
23
36
|
end
|
24
|
-
|
25
|
-
#
|
26
|
-
# @return [Boolean] +true+ if this email is to be sent in the future
|
27
|
-
#
|
28
|
-
def delayed?
|
29
|
-
!!(delivery_time && delivery_time > Time.now)
|
30
|
-
end
|
31
37
|
end
|
32
38
|
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'hirb'
|
2
|
+
require 'optparse'
|
3
|
+
|
4
|
+
module ArMailerRevised
|
5
|
+
module Helpers
|
6
|
+
module CommandLine
|
7
|
+
|
8
|
+
class RailsEnvironmentFailed < StandardError;
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.class_eval do
|
13
|
+
base.send :extend, ClassMethods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
|
19
|
+
##
|
20
|
+
# Processes +args+ and runs as appropriate
|
21
|
+
|
22
|
+
def run(args = ARGV)
|
23
|
+
options = process_args(args)
|
24
|
+
|
25
|
+
if options[:display_queue]
|
26
|
+
display_mail_queue(options[:display_queue])
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
|
30
|
+
new(options).run
|
31
|
+
|
32
|
+
rescue SystemExit
|
33
|
+
raise
|
34
|
+
rescue SignalException
|
35
|
+
exit
|
36
|
+
rescue Exception => e
|
37
|
+
$stderr.puts "Unhandled exception #{e.message}(#{e.class}):"
|
38
|
+
$stderr.puts "\t#{e.backtrace.join "\n\t"}"
|
39
|
+
exit -2
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Prints a list of unsent emails and the last delivery attempt, if any.
|
44
|
+
# Only emails which are ready to deliver are displayed
|
45
|
+
#
|
46
|
+
def display_mail_queue(what)
|
47
|
+
emails = case what
|
48
|
+
when 'all' then
|
49
|
+
puts 'Showing all emails in the system'
|
50
|
+
ArMailerRevised.email_class.all
|
51
|
+
when 'deliverable' then
|
52
|
+
puts 'Showing emails ready to deliver'
|
53
|
+
ArMailerRevised.email_class.ready_to_deliver
|
54
|
+
when 'delayed' then
|
55
|
+
puts 'Showing delayed emails'
|
56
|
+
ArMailerRevised.email_class.delayed
|
57
|
+
else
|
58
|
+
[]
|
59
|
+
end
|
60
|
+
puts 'Mail queue is empty' and return if emails.empty?
|
61
|
+
puts Hirb::Helpers::AutoTable.render emails, :fields => [:from, :to, :delivery_time, :last_send_attempt, :updated_at]
|
62
|
+
end
|
63
|
+
|
64
|
+
def process_args(args)
|
65
|
+
name = File.basename $0
|
66
|
+
|
67
|
+
options = {}
|
68
|
+
options[:chdir] = '.'
|
69
|
+
options[:max_age] = 86400 * 7
|
70
|
+
options[:rails_env] = ENV['RAILS_ENV']
|
71
|
+
options[:log_level] = 'info'
|
72
|
+
options[:verbose] = false
|
73
|
+
|
74
|
+
opts = OptionParser.new do |opts|
|
75
|
+
opts.banner = "Usage: #{name} [options]"
|
76
|
+
opts.separator ''
|
77
|
+
|
78
|
+
opts.separator "#{name} scans the email table for new messages and sends them to the"
|
79
|
+
opts.separator "website's configured SMTP host."
|
80
|
+
opts.separator ''
|
81
|
+
opts.separator "#{name} must be run from a Rails application's root."
|
82
|
+
|
83
|
+
opts.separator ''
|
84
|
+
opts.separator 'Sendmail options:'
|
85
|
+
|
86
|
+
opts.on("-b", "--batch-size BATCH_SIZE",
|
87
|
+
"Maximum number of emails to send per delay",
|
88
|
+
"Default: Deliver all available emails", Integer) do |batch_size|
|
89
|
+
options[:batch_size] = batch_size
|
90
|
+
end
|
91
|
+
|
92
|
+
opts.on("--max-age MAX_AGE",
|
93
|
+
"Maxmimum age for an email. After this",
|
94
|
+
"it will be removed from the queue.",
|
95
|
+
"Set to 0 to disable queue cleanup.",
|
96
|
+
"Default: #{options[:max_age]} seconds", Integer) do |max_age|
|
97
|
+
options[:max_age] = max_age
|
98
|
+
end
|
99
|
+
|
100
|
+
opts.on('--mailq [all|deliverable|delayed]',
|
101
|
+
'Display a list of emails waiting to be sent',
|
102
|
+
'Default: all') do |mailq|
|
103
|
+
options[:display_queue] = mailq || 'all'
|
104
|
+
end
|
105
|
+
|
106
|
+
opts.separator ''
|
107
|
+
opts.separator 'Generic Options:'
|
108
|
+
|
109
|
+
opts.on('-l', '--log-file PATH',
|
110
|
+
'Custom log file location at PATH. May also be "stdout" or "stderr" for console output',
|
111
|
+
'Default: log/environment.log') do |path|
|
112
|
+
dir = File.dirname(path)
|
113
|
+
usage opts, "#{dir} is not an existing directory" unless File.exists?(dir) && File.directory?(dir)
|
114
|
+
usage opts, "#{path} is a directory" if File.directory?(path)
|
115
|
+
options[:log_file] = path
|
116
|
+
end
|
117
|
+
|
118
|
+
opts.on('--log-level LEVEL',
|
119
|
+
"Set the mailer's log LEVEL",
|
120
|
+
"Default: #{options[:log_level]}") do |level|
|
121
|
+
usage opts, "Invalid log-level: #{level}" unless %w[debug info warn error fatal].include?(level.to_s.downcase)
|
122
|
+
options[:log_level] = level
|
123
|
+
end
|
124
|
+
|
125
|
+
opts.on("-c", "--chdir PATH",
|
126
|
+
"Use PATH for the application path",
|
127
|
+
"Default: #{options[:chdir]}") do |path|
|
128
|
+
usage opts, "#{path} is not a directory" unless File.directory? path
|
129
|
+
usage opts, "#{path} is not readable" unless File.readable? path
|
130
|
+
options[:chdir] = path
|
131
|
+
end
|
132
|
+
|
133
|
+
opts.on("-e", "--environment RAILS_ENV",
|
134
|
+
"Set the RAILS_ENV constant",
|
135
|
+
"Default: #{options[:rails_env]}") do |env|
|
136
|
+
options[:rails_env] = env
|
137
|
+
end
|
138
|
+
|
139
|
+
opts.on("-v", "--[no-]verbose",
|
140
|
+
"Be verbose",
|
141
|
+
"Default: #{options[:verbose]}") do |verbose|
|
142
|
+
options[:verbose] = verbose
|
143
|
+
end
|
144
|
+
|
145
|
+
opts.on("-h", "--help",
|
146
|
+
"You're looking at it") do
|
147
|
+
usage opts
|
148
|
+
end
|
149
|
+
|
150
|
+
opts.on("--version", "Version of ARMailer") do
|
151
|
+
usage "ar_mailer_revised #{VERSION}"
|
152
|
+
end
|
153
|
+
|
154
|
+
opts.separator ''
|
155
|
+
end
|
156
|
+
|
157
|
+
opts.parse! args
|
158
|
+
|
159
|
+
ENV['RAILS_ENV'] = options[:rails_env]
|
160
|
+
|
161
|
+
begin
|
162
|
+
load_rails_environment(options[:chdir])
|
163
|
+
rescue RailsEnvironmentFailed
|
164
|
+
usage opts, <<-EOF
|
165
|
+
#{name} must be run from a Rails application's root to deliver email.
|
166
|
+
#{Dir.pwd} does not appear to be a Rails application root.
|
167
|
+
EOF
|
168
|
+
end
|
169
|
+
|
170
|
+
options
|
171
|
+
end
|
172
|
+
|
173
|
+
#
|
174
|
+
# Loads the complete rails environment
|
175
|
+
#
|
176
|
+
def load_rails_environment(base_path)
|
177
|
+
Dir.chdir(base_path) do
|
178
|
+
require File.join(base_path, 'config/environment')
|
179
|
+
require 'action_mailer/ar_mailer'
|
180
|
+
end
|
181
|
+
rescue LoadError => e
|
182
|
+
puts e
|
183
|
+
raise RailsEnvironmentFailed
|
184
|
+
end
|
185
|
+
|
186
|
+
def usage(opts, message = nil)
|
187
|
+
if message then
|
188
|
+
$stderr.puts message
|
189
|
+
$stderr.puts
|
190
|
+
end
|
191
|
+
|
192
|
+
$stderr.puts opts
|
193
|
+
exit 1
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|