ar_mailer_revised 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.ruby-version +1 -0
- data/.yardopts +1 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +22 -0
- data/MIT-LICENSE +20 -0
- data/README.md +199 -0
- data/Rakefile +28 -0
- data/ar_mailer_revised.gemspec +30 -0
- data/bin/ar_sendmail +10 -0
- data/generators/ar_mailer_revised/ar_mailer_revised_generator.rb +23 -0
- data/generators/ar_mailer_revised/templates/initializer.rb +8 -0
- data/generators/ar_mailer_revised/templates/migration.rb +32 -0
- data/generators/ar_mailer_revised/templates/model.rb +30 -0
- data/lib/action_mailer/ar_mailer.rb +74 -0
- data/lib/ar_mailer_revised.rb +35 -0
- data/lib/ar_mailer_revised/email_scaffold.rb +32 -0
- data/lib/ar_mailer_revised/version.rb +3 -0
- data/lib/tasks/ar_mailer_revised_tasks.rake +4 -0
- data/test/ar_mailer_revised_test.rb +4 -0
- data/test/test_helper.rb +22 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 57ad9dcf569b1f53fcc93b4b7100e67ea077c08f
|
4
|
+
data.tar.gz: bbf2faed6943104ab53f078fbd4ce1c64586de6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8768cb5e338630a98e615513234066f37bb269deba0b73bc0b2ee5053257e82721e4884c7c856759cb3378f9c4e769b0bff763cb78009d4be327bd925bb10291
|
7
|
+
data.tar.gz: ca22e7b7f016f3d5b50f9430a2762c10d71be0580fa54e83089baf3f41bcc7990b90aa9f66104e7e098a8d7bd4ec5a7b56c62c89f0ccc668f78b1a0da55ef5ae
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.1
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
app/**/*.rb lib/**/*.rb --no-private
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in ar_mailer_revised.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use debugger
|
14
|
+
# gem 'debugger'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Stefan Exner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
# ArMailerRevised
|
2
|
+
|
3
|
+
[ArMailer](https://github.com/seattlerb/ar_mailer) is a great gem which allows you to store emails in your application's database and batch deliver
|
4
|
+
them later using a background task.
|
5
|
+
|
6
|
+
However, it was not compatible with newer versions of Rails and also lacking some of the functionality I needed in my applications.
|
7
|
+
|
8
|
+
Especially, I wanted to use
|
9
|
+
|
10
|
+
* custom delivery dates in the future for delayed emails
|
11
|
+
* custom SMTP settings per email
|
12
|
+
* custom attributes directly in the email record to keep track of them
|
13
|
+
|
14
|
+
**Note:** This is the Rails 2.3 version of ARMailer Revised.
|
15
|
+
It does (and always will) **only support generating emails, not actually sending them**.
|
16
|
+
|
17
|
+
To actually send them, a Rails 4 application will be needed, this version was only made
|
18
|
+
to let older existing applications use the email queue.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'ar_mailer_revised'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install ar_mailer_revised
|
33
|
+
|
34
|
+
### Generating Files
|
35
|
+
|
36
|
+
ArMailerRevised needs a few things to work correctly:
|
37
|
+
|
38
|
+
1. A table in the database to store the email queue
|
39
|
+
2. An email model to create and access the email records
|
40
|
+
3. An initializer to set the gem configuration
|
41
|
+
|
42
|
+
All of them can be created using a generator:
|
43
|
+
|
44
|
+
$ ruby script/generate ar_mailer_revised MODEL_NAME
|
45
|
+
|
46
|
+
If you just want to add an old application to an existing Rails 4 email queue,
|
47
|
+
simply delete the migration as you won't need it.
|
48
|
+
Otherwise, migrate your application before continuing.
|
49
|
+
|
50
|
+
$ rake db:migrate
|
51
|
+
|
52
|
+
### Setting the delivery method
|
53
|
+
|
54
|
+
First of all, you have to set ActionMailer to use the gem's delivery method.
|
55
|
+
This can be done per environment or globally for the application using either
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
config.action_mailer.delivery_method = :activerecord
|
59
|
+
```
|
60
|
+
|
61
|
+
or - not inside a configuration file
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
ActionMailer::Base.delivery_method = :activerecord
|
65
|
+
```
|
66
|
+
|
67
|
+
### SMTP-Settings
|
68
|
+
|
69
|
+
ArMailerRevised accepts SMTP settings in the form ActionMailer::Base (v4) does.
|
70
|
+
Application wide settings have to be stored in ActionMailer::Base.smtp_settings.
|
71
|
+
Please have a look at [ActionMailer::Base](http://api.rubyonrails.org/classes/ActionMailer/Base.html)
|
72
|
+
|
73
|
+
The only difference here are additional TLS options as follows:
|
74
|
+
|
75
|
+
1. `:enable_starttls_auto` enables STARTTLS if the serves is capable to handle it
|
76
|
+
2. `:enable_starttls` forces the usage of STARTTLS, whether the server is capable of it or not
|
77
|
+
3. `:tls` forces the usage of TLS (SSL SMTP)
|
78
|
+
|
79
|
+
**Important**: These additional settings are in descending order, meaning that a higher importance
|
80
|
+
setting will override a less important setting.
|
81
|
+
|
82
|
+
`:openssl_verify_mode` is currently not supported, but will possibly be added later on.
|
83
|
+
|
84
|
+
Below will be a growing list of demo SMTP settings for popular providers.
|
85
|
+
|
86
|
+
## Creating Emails
|
87
|
+
|
88
|
+
ArMailerRevised uses the normal ActionMailer::Base templates, so you can write
|
89
|
+
delivery-methods like you would for direct email sending.
|
90
|
+
On delivering, the email will be stored in the database and not being sent directly.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class TestMailer < ActionMailer::Base
|
94
|
+
def basic_email(recipients)
|
95
|
+
from 'test@example.com'
|
96
|
+
to recipients
|
97
|
+
subject 'Hello, World'
|
98
|
+
body 'How are you?'
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
### Setting a custom delivery time
|
104
|
+
|
105
|
+
ArMailerRevised adds a new method to ActionMailer templates to customize
|
106
|
+
the resulting email record. One of them is +ar_mailer_delivery_time+.
|
107
|
+
This method sets a time which determines the earliest sending time for this email,
|
108
|
+
in other words: If you set this time, the email won't be sent prior to it.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
def delayed_email
|
112
|
+
ar_mailer_delivery_time Time.now + 2.hours
|
113
|
+
|
114
|
+
from 'test@example.com'
|
115
|
+
to recipients
|
116
|
+
subject 'Delayed Email'
|
117
|
+
body 'Yes, I am indeed delayed.'
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
**Important**: It may happen that the Rails logging output of the generated mail may still contain
|
122
|
+
custom attributes (like the delivery time) in its header. This happens because ActionMailer will
|
123
|
+
log the email before actually delivering it. The generated email will **not** contain these headers any more.
|
124
|
+
|
125
|
+
### Setting custom SMTP settings
|
126
|
+
|
127
|
+
It is possible to set own SMTP settings for each email in the system which will then be used for delivery.
|
128
|
+
These settings may contain everything the global settings do (see above).
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
def custom_smtp_email
|
132
|
+
ar_mailer_smtp_settings({
|
133
|
+
:address => 'localhost',
|
134
|
+
:port => 25,
|
135
|
+
:domain => 'localhost.localdomain',
|
136
|
+
:user_name => 'some.user',
|
137
|
+
:password => 'some.password',
|
138
|
+
:authentication => :plain,
|
139
|
+
:enable_starttls_auto => true
|
140
|
+
})
|
141
|
+
|
142
|
+
from 'test@example.com'
|
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
|
+
```
|
148
|
+
|
149
|
+
**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
|
+
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.
|
151
|
+
|
152
|
+
### Other custom attributes
|
153
|
+
|
154
|
+
It is possible to set custom attributes in the email record before it is saved, e.g.
|
155
|
+
to keep better track of emails (by adding an identifier of the reason the email was generated at all).
|
156
|
+
|
157
|
+
You can add custom attributes to the email table simply by altering the generated migration, e.g.
|
158
|
+
|
159
|
+
t.integer 'a_number'
|
160
|
+
|
161
|
+
In the email delivering method, these attributes may then be filled with the actual data using the `ar_mailer_attribute` helper method:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
def custom_attribute_email
|
165
|
+
ar_mailer_attribute :a_number, 42
|
166
|
+
|
167
|
+
from 'test@example.com'
|
168
|
+
to recipients
|
169
|
+
subject 'Custom Attribute Email'
|
170
|
+
body 'I have a custom attribute.'
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
174
|
+
### Sending Emails
|
175
|
+
|
176
|
+
As mentioned above, this version of ARMailer Revised does not allow sending emails.
|
177
|
+
|
178
|
+
Please use the Rails 4 version instead, e.g. with an own little application
|
179
|
+
which uses the same database and only provides the email sending functionality.
|
180
|
+
|
181
|
+
### SMTP settings for common providers (to be extended)
|
182
|
+
|
183
|
+
GoogleMail:
|
184
|
+
|
185
|
+
:address => 'smtp.googlemail.com',
|
186
|
+
:port => 465,
|
187
|
+
:domain => 'googlemail.com',
|
188
|
+
:user_name => 'USERNAME@googlemail.com',
|
189
|
+
:password => 'SOME_PASSWORD',
|
190
|
+
:authentication => :plain,
|
191
|
+
:tls => true
|
192
|
+
|
193
|
+
# Contributing
|
194
|
+
|
195
|
+
1. Fork it
|
196
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
197
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
198
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
199
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ArMailerRevised'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
Bundler::GemHelper.install_tasks
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'lib'
|
23
|
+
t.libs << 'test'
|
24
|
+
t.pattern = 'test/**/*_test.rb'
|
25
|
+
t.verbose = false
|
26
|
+
end
|
27
|
+
|
28
|
+
task default: :test
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'ar_mailer_revised/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'ar_mailer_revised'
|
9
|
+
spec.version = ArMailerRevised::VERSION
|
10
|
+
spec.authors = ['Stefan Exner']
|
11
|
+
spec.email = ['stex@sterex.de']
|
12
|
+
spec.description = %q{Extension of the great ArMailer gem by Eric Hodel.}
|
13
|
+
spec.summary = 'Batch email sending for rails applications'
|
14
|
+
spec.homepage = 'http://www.github.com/stex/ar_mailer_revised'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.3'
|
24
|
+
spec.add_development_dependency 'yard', '~> 0.8'
|
25
|
+
spec.add_development_dependency 'redcarpet', '~> 2.3'
|
26
|
+
|
27
|
+
spec.add_dependency 'rails', '~> 2.3'
|
28
|
+
|
29
|
+
spec.required_ruby_version = '>= 1.8.7'
|
30
|
+
end
|
data/bin/ar_sendmail
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
puts <<-EOS
|
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
|
10
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class ArMailerRevisedGenerator < Rails::Generator::NamedBase
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
model_name = (name || 'Email').classify
|
5
|
+
|
6
|
+
#Create the model
|
7
|
+
m.template 'model.rb', "app/models/#{model_name.downcase.underscore}.rb", :assigns => {:model_name => model_name}
|
8
|
+
|
9
|
+
#Create the migration
|
10
|
+
m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "create_#{model_name.downcase.underscore.pluralize}",
|
11
|
+
:assigns => {:model_name => model_name}
|
12
|
+
|
13
|
+
#Create the initializer
|
14
|
+
m.template 'initializer.rb', 'config/initializers/ar_mailer_revised.rb', :assigns => {:model_name => model_name}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def banner
|
21
|
+
"Usage: #{$0} ar_mailer_revised MODEL_NAME"
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
ArMailerRevised.configuration do |config|
|
2
|
+
|
3
|
+
#The model your application is using for email sending.
|
4
|
+
#If you created it using the ArMailerRevised generator, the below
|
5
|
+
#model name should already be correct.
|
6
|
+
config.email_class = <%= model_name.classify %>
|
7
|
+
|
8
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Create<%= model_name.classify.pluralize %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :<%= model_name.underscore.pluralize %> do |t|
|
4
|
+
t.string 'from'
|
5
|
+
t.string 'to'
|
6
|
+
|
7
|
+
#Timestamp for the last send attempt, 0 means, that there was no send attempt yet
|
8
|
+
t.integer 'last_send_attempt', :default => 0
|
9
|
+
|
10
|
+
#Mail body including headers
|
11
|
+
t.text 'mail'
|
12
|
+
|
13
|
+
#Custom delivery time, ArMailer won't send the email prior to this time
|
14
|
+
t.datetime 'delivery_time'
|
15
|
+
|
16
|
+
#Custom SMTP settings per email
|
17
|
+
t.text 'smtp_settings'
|
18
|
+
|
19
|
+
#You can add further attributes here, they can then be assigned
|
20
|
+
#to the email record using the +ar_mailer_attribute+ method from
|
21
|
+
#within mailer methods. Example:
|
22
|
+
#
|
23
|
+
# In the migration:
|
24
|
+
# t.integer :client_id
|
25
|
+
#
|
26
|
+
# Inside the mailer method:
|
27
|
+
# ar_mailer_attribute :client_id, @client.id
|
28
|
+
|
29
|
+
t.timestamps
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#
|
2
|
+
# Class used as email model for ArMailerRevised
|
3
|
+
#
|
4
|
+
# @attr [String] from
|
5
|
+
# The email sender
|
6
|
+
#
|
7
|
+
# @attr [String] to
|
8
|
+
# The email recipient
|
9
|
+
#
|
10
|
+
# @attr [Integer] last_send_attempt
|
11
|
+
# Unix timestamp containing the last time the system tried to deliver this email.
|
12
|
+
# The value will be +nil+ if there wasn't a send attempt yet
|
13
|
+
#
|
14
|
+
# @attr [String] mail
|
15
|
+
# The mail body, including the mail header information (from, to, encoding, ...)
|
16
|
+
#
|
17
|
+
# @attr [Date] delivery_date
|
18
|
+
# If this is set, the email won't be sent before the given date.
|
19
|
+
#
|
20
|
+
# @attr [Hash] smtp_settings
|
21
|
+
# Serialized Hash storing custom SMTP settings just for this email.
|
22
|
+
# If this value is +nil+, the system will use the default SMTP settings set up in the application
|
23
|
+
#
|
24
|
+
class <%= model_name.classify %> < ActiveRecord::Base
|
25
|
+
#Helper methods and named scopes provided by ArMailerRevised
|
26
|
+
include ArMailerRevised::EmailScaffold
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
#
|
2
|
+
# Adds sending email through an ActiveRecord table as a delivery method for
|
3
|
+
# ActionMailer.
|
4
|
+
#
|
5
|
+
class ActionMailer::Base
|
6
|
+
#
|
7
|
+
# Sets a custom email class attribute. It can be used
|
8
|
+
# if the user wants to set custom attributes on his email records,
|
9
|
+
# e.g. to track them later.
|
10
|
+
# They are automatically set as attributes in the resulting AR record,
|
11
|
+
# so make sure that they actually exist as database columns!
|
12
|
+
#
|
13
|
+
# @example Setting a client id to track which client sent the email
|
14
|
+
# ar_mailer_attribute :client_id, @client.id
|
15
|
+
#
|
16
|
+
def ar_mailer_attribute(key, value = nil)
|
17
|
+
attrs = ar_mailer_setting(:custom_attributes) || {}
|
18
|
+
if value
|
19
|
+
attrs[key.to_s] = value
|
20
|
+
ar_mailer_setting(:custom_attributes, attrs)
|
21
|
+
else
|
22
|
+
attrs[key.to_s]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# Sets custom SMTP settings just for this email
|
28
|
+
#
|
29
|
+
def ar_mailer_smtp_settings(new_settings = nil)
|
30
|
+
ar_mailer_setting(:smtp_settings, new_settings)
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Sets a delivery time for this email. If left at +nil+,
|
35
|
+
# the email is sent immediately.
|
36
|
+
#
|
37
|
+
def ar_mailer_delivery_time(new_time = nil)
|
38
|
+
ar_mailer_setting(:delivery_time, new_time)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
#
|
44
|
+
# Sets or simply returns an ar_mailer_setting
|
45
|
+
#
|
46
|
+
def ar_mailer_setting(key, value = nil)
|
47
|
+
@ar_mailer_settings ||= {}
|
48
|
+
|
49
|
+
if value
|
50
|
+
@ar_mailer_settings[key.to_s] = value
|
51
|
+
else
|
52
|
+
@ar_mailer_settings[key.to_s]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# Custom ActionMailer delivery method.
|
58
|
+
# It does not actually send the email, but will create a record
|
59
|
+
# in the database instead to be sent later by the ar_sendmail executable.
|
60
|
+
#
|
61
|
+
def perform_delivery_activerecord(mail)
|
62
|
+
email_options = {}
|
63
|
+
email_options[:delivery_time] = ar_mailer_setting(:delivery_time)
|
64
|
+
email_options[:smtp_settings] = ar_mailer_setting(:smtp_settings)
|
65
|
+
email_options[:mail] = mail.encoded
|
66
|
+
email_options[:from] = (mail['return-path'] && mail['return-path'].spec) || mail.from.first
|
67
|
+
|
68
|
+
email_options.reverse_merge!(ar_mailer_setting(:custom_attributes) || {})
|
69
|
+
|
70
|
+
mail.destinations.each do |destination|
|
71
|
+
ArMailerRevised.email_class.create!(email_options.merge({:to => destination}))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'ar_mailer_revised/version'
|
2
|
+
require 'action_mailer/ar_mailer'
|
3
|
+
require 'ar_mailer_revised/email_scaffold'
|
4
|
+
|
5
|
+
#Register the new delivery method
|
6
|
+
module ArMailerRevised
|
7
|
+
def self.configuration(&proc)
|
8
|
+
@@config ||= OpenStruct.new({
|
9
|
+
:email_class => 'Email'
|
10
|
+
})
|
11
|
+
if block_given?
|
12
|
+
yield @@config
|
13
|
+
@@config.email_class = (@@config.email_class || 'Email').to_s.classify
|
14
|
+
else
|
15
|
+
@@config
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# @return [ActiveRecord::Base] (Email)
|
21
|
+
# The class used to create new emails in the system
|
22
|
+
#
|
23
|
+
def self.email_class
|
24
|
+
self.email_class_name.constantize
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# @return [String] (Email)
|
29
|
+
# The email class' name
|
30
|
+
#
|
31
|
+
def self.email_class_name
|
32
|
+
self.configuration.email_class.classify
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# Helper methods for the chosen email class
|
3
|
+
#
|
4
|
+
module ArMailerRevised
|
5
|
+
module EmailScaffold
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.serialize :smtp_settings
|
9
|
+
|
10
|
+
#Only emails which are to be sent immediately
|
11
|
+
base.named_scope :without_delayed, lambda { {:conditions => {:delivery_time => nil} }}
|
12
|
+
|
13
|
+
#All emails which are to be sent in the future
|
14
|
+
base.named_scope :delayed, lambda { {:conditions => ['delivery_time > ?', Time.now]}}
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# @return [Boolean] +true+ if the system tried to send
|
19
|
+
# the email before.
|
20
|
+
#
|
21
|
+
def previously_attempted?
|
22
|
+
last_send_attempt > 0
|
23
|
+
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
|
+
end
|
32
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
|
+
|
6
|
+
require 'minitest'
|
7
|
+
require 'shoulda'
|
8
|
+
require "rails/test_help"
|
9
|
+
|
10
|
+
Rails.backtrace_cleaner.remove_silencers!
|
11
|
+
|
12
|
+
# Load support files
|
13
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
14
|
+
|
15
|
+
# Load fixtures from the engine
|
16
|
+
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
|
17
|
+
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
|
18
|
+
end
|
19
|
+
|
20
|
+
# For generators
|
21
|
+
require "rails/generators/test_case"
|
22
|
+
require "generators/ar_mailer_revised/install_generator"
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ar_mailer_revised
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefan Exner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.8'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redcarpet
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.3'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.3'
|
83
|
+
description: Extension of the great ArMailer gem by Eric Hodel.
|
84
|
+
email:
|
85
|
+
- stex@sterex.de
|
86
|
+
executables:
|
87
|
+
- ar_sendmail
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".ruby-version"
|
93
|
+
- ".yardopts"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- MIT-LICENSE
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- ar_mailer_revised.gemspec
|
100
|
+
- bin/ar_sendmail
|
101
|
+
- generators/ar_mailer_revised/ar_mailer_revised_generator.rb
|
102
|
+
- generators/ar_mailer_revised/templates/initializer.rb
|
103
|
+
- generators/ar_mailer_revised/templates/migration.rb
|
104
|
+
- generators/ar_mailer_revised/templates/model.rb
|
105
|
+
- lib/action_mailer/ar_mailer.rb
|
106
|
+
- lib/ar_mailer_revised.rb
|
107
|
+
- lib/ar_mailer_revised/email_scaffold.rb
|
108
|
+
- lib/ar_mailer_revised/version.rb
|
109
|
+
- lib/tasks/ar_mailer_revised_tasks.rake
|
110
|
+
- test/ar_mailer_revised_test.rb
|
111
|
+
- test/test_helper.rb
|
112
|
+
homepage: http://www.github.com/stex/ar_mailer_revised
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.8.7
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.4.2
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Batch email sending for rails applications
|
136
|
+
test_files:
|
137
|
+
- test/ar_mailer_revised_test.rb
|
138
|
+
- test/test_helper.rb
|
139
|
+
has_rdoc:
|