safety_mailer 0.0.10 → 0.1.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 +5 -5
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +46 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +147 -0
- data/Rakefile +11 -21
- data/assets/logo_ruby_hardhat.png +0 -0
- data/lib/safety_mailer/railtie.rb +3 -0
- data/lib/safety_mailer/safety_mailer.rb +54 -15
- data/lib/safety_mailer/version.rb +4 -2
- data/lib/safety_mailer.rb +5 -3
- data/sig/safety_mailer.rbs +4 -0
- metadata +24 -75
- data/Gemfile +0 -4
- data/README.rdoc +0 -60
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f938efe60a4a91fe62371d6d49ac6d17b30caada23b12dd7ff12e3043a18fc7c
|
4
|
+
data.tar.gz: c28ec8454f91e49e5376bc43c438dafe718cd145789226b915526107811691fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a6e8a3831a85672c5f67f6385782361dc6799bec3a8eab18336fef9ff8913a617eab039fb1a30f175078f7930b1c29434eabaf356f20bf00822cd627d8ae109
|
7
|
+
data.tar.gz: 6d61949b2ad02dba4bd4aeb82e05434fb89f1deaab0b7d0e38d0231bc331601006b740649c0ea1079c21103f4fee398d300a395f1d8fbf9881237fb6e1c6cf6e
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
## [Unreleased]
|
2
|
+
|
3
|
+
## [0.1.0] - 2025-09-01
|
4
|
+
|
5
|
+
- Rebuilding on a modern gem framework
|
6
|
+
- Shiny new logo!
|
7
|
+
- Port testing to minitest
|
8
|
+
- Fix support for non-rails (Mail gem) usage
|
9
|
+
- Add support for custom loggers (e.g., non-rails)
|
10
|
+
|
11
|
+
## [0.0.10] - 2014-12-04
|
12
|
+
|
13
|
+
- Support use separate from Mail gem
|
14
|
+
|
15
|
+
## [0.0.9] - 2014-10-23
|
16
|
+
|
17
|
+
- Make `whitelisted?` public
|
18
|
+
|
19
|
+
## [0.0.8] - 2014-06-14
|
20
|
+
|
21
|
+
- Added support for ActionMailer custom delivery methods
|
22
|
+
|
23
|
+
## [0.0.7] - 2013-06-10
|
24
|
+
|
25
|
+
- Development enhancements
|
26
|
+
|
27
|
+
## [0.0.6] - 2013-06-10
|
28
|
+
|
29
|
+
- SendGrid support
|
30
|
+
- X-SMTPAPI support
|
31
|
+
|
32
|
+
## [0.0.5] - 2013-06-10
|
33
|
+
|
34
|
+
- Doc update
|
35
|
+
|
36
|
+
## [0.0.4] - 2012-03-13
|
37
|
+
|
38
|
+
- Mail gem compatibility fix
|
39
|
+
|
40
|
+
## [0.0.3] - 2012-02-12
|
41
|
+
|
42
|
+
- Rails 3.1 compatibility
|
43
|
+
|
44
|
+
## [0.0.2] - 2012-02-09
|
45
|
+
|
46
|
+
- Initial release
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Bill Kirtley
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
# SafetyMailer
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
Restrict email sent by your application to only approved domains or accounts.
|
6
|
+
|
7
|
+
Specify a domain (or set of domains, or magic word in email address) email is allowed to go to, and email to all other domains is silently dropped.
|
8
|
+
|
9
|
+
This is useful for testing or staging environments where you want to be certain email to real customers doesn't escape the lab.
|
10
|
+
|
11
|
+
Layered on the Mail gem, so Rails >= 3.0 applications (as well as plain ruby apps) can use safety_mailer.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add the gem to your +Gemfile+, specifying groups (probably not production) to include it in.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem "safety_mailer", :group => :development
|
19
|
+
```
|
20
|
+
|
21
|
+
Don't forget to `bundle install` to install
|
22
|
+
|
23
|
+
In your environment file `config/environments/development.rb` configure it, and some regular expressions.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
config.action_mailer.delivery_method = :safety_mailer
|
27
|
+
config.action_mailer.safety_mailer_settings = {
|
28
|
+
allowed_matchers: [ /mydomain.com/, /mytestacct@gmail.com/, /\+safety_mailer@/ ],
|
29
|
+
delivery_method: :smtp,
|
30
|
+
delivery_method_settings: {
|
31
|
+
address: "smtp.mydomain.com",
|
32
|
+
port: 25,
|
33
|
+
domain: "mydomain.com",
|
34
|
+
authentication: :plain,
|
35
|
+
user_name: "mydomain_mailer@mydomain.com",
|
36
|
+
password: "password"
|
37
|
+
}
|
38
|
+
}
|
39
|
+
```
|
40
|
+
|
41
|
+
## Logging
|
42
|
+
|
43
|
+
SafetyMailer supports custom logging to help you monitor email filtering decisions. By default, SafetyMailer will automatically use `Rails.logger` if available, or remain silent if no logger is configured.
|
44
|
+
|
45
|
+
### Rails Applications
|
46
|
+
|
47
|
+
In Rails applications, SafetyMailer automatically uses `Rails.logger` by default. You'll see log messages like:
|
48
|
+
|
49
|
+
```
|
50
|
+
SafetyMailer: Processing 3 recipient(s) - 2 allowed, 1 suppressed
|
51
|
+
SafetyMailer: Allowed delivery for user@mydomain.com
|
52
|
+
SafetyMailer: Suppressed delivery for user@external.com (no matching allowed pattern)
|
53
|
+
SafetyMailer: Delivering email to 2 allowed recipient(s)
|
54
|
+
```
|
55
|
+
|
56
|
+
### Custom Logger Configuration
|
57
|
+
|
58
|
+
You can specify a custom logger in your configuration:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
require 'logger'
|
62
|
+
|
63
|
+
# Create a custom logger
|
64
|
+
safety_logger = Logger.new(Rails.root.join('log', 'safety_mailer.log'))
|
65
|
+
|
66
|
+
config.action_mailer.delivery_method = :safety_mailer
|
67
|
+
config.action_mailer.safety_mailer_settings = {
|
68
|
+
allowed_matchers: [ /mydomain.com/, /mytestacct@gmail.com/ ],
|
69
|
+
logger: safety_logger, # Custom logger
|
70
|
+
delivery_method: :smtp,
|
71
|
+
# ... other settings
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
### Disabling Logging
|
76
|
+
|
77
|
+
To disable logging entirely, set the logger to `nil`:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
config.action_mailer.safety_mailer_settings = {
|
81
|
+
allowed_matchers: [ /mydomain.com/ ],
|
82
|
+
logger: nil, # Disable all logging
|
83
|
+
delivery_method: :smtp,
|
84
|
+
# ... other settings
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
### Non-Rails Applications
|
89
|
+
|
90
|
+
For non-Rails applications, SafetyMailer remains silent by default. You can enable logging by providing a custom logger:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require "safety_mailer"
|
94
|
+
require "logger"
|
95
|
+
|
96
|
+
# Configure with custom logger
|
97
|
+
custom_logger = Logger.new(STDOUT)
|
98
|
+
|
99
|
+
Mail.defaults do
|
100
|
+
delivery_method SafetyMailer::Carrier, {
|
101
|
+
allowed_matchers: [ /mydomain.com/ ],
|
102
|
+
logger: custom_logger,
|
103
|
+
delivery_method: :smtp
|
104
|
+
}
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
Now, email to `anyone@mydomain.com`, `mytestacct@gmail.com`, `bob+safety_mailer@yahoo.com` all get sent, and email to other recipients (like the real users in the production database you copied to a test server) is suppressed.
|
109
|
+
|
110
|
+
## Non-Rails
|
111
|
+
|
112
|
+
Any user of the Mail gem can configure safety_mailer:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
require "safety_mailer"
|
116
|
+
Mail.defaults do
|
117
|
+
delivery_method SafetyMailer::Carrier, {
|
118
|
+
... same settings as above
|
119
|
+
}
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
## Non-Mail
|
124
|
+
|
125
|
+
If you're not using the Mail gem (or use it sometimes but want to use the same logic / configuration in other contexts), you can filter directly:
|
126
|
+
|
127
|
+
```
|
128
|
+
filtered_array = SafetyMailer::Carrier.new(ActionMailer::Base.safety_mailer_settings).filter(unfiltered_email_addresses)
|
129
|
+
```
|
130
|
+
|
131
|
+
## Development
|
132
|
+
|
133
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
134
|
+
|
135
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
136
|
+
|
137
|
+
## Contributing
|
138
|
+
|
139
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cluesque/safety_mailer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/cluesque/safety_mailer/blob/main/CODE_OF_CONDUCT.md).
|
140
|
+
|
141
|
+
## License
|
142
|
+
|
143
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
144
|
+
|
145
|
+
## Code of Conduct
|
146
|
+
|
147
|
+
Everyone interacting in the SafetyMailer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cluesque/safety_mailer/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -1,26 +1,16 @@
|
|
1
|
-
|
2
|
-
require "bundler/gem_tasks"
|
3
|
-
require 'rspec/core/rake_task'
|
4
|
-
Bundler::GemHelper.install_tasks
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
Rake::Task["install"].invoke
|
9
|
-
puts "Loading development console..."
|
10
|
-
system("irb -r safety_mailer")
|
11
|
-
end
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
12
5
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb', 'test/*_test.rb']
|
17
10
|
end
|
18
11
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
t.pattern = "spec/**/*_spec.rb"
|
23
|
-
end
|
24
|
-
end
|
12
|
+
require 'rubocop/rake_task'
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
25
15
|
|
26
|
-
task :
|
16
|
+
task default: %i[test rubocop]
|
Binary file
|
@@ -1,27 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
1
5
|
module SafetyMailer
|
6
|
+
# Carrier class implements a delivery method for ActionMailer
|
2
7
|
class Carrier
|
3
|
-
attr_accessor :matchers, :settings, :mail
|
8
|
+
attr_accessor :matchers, :settings, :mail, :logger
|
4
9
|
|
5
10
|
def initialize(params = {})
|
6
11
|
self.matchers = params[:allowed_matchers] || []
|
7
12
|
self.settings = params[:delivery_method_settings] || {}
|
8
|
-
|
9
|
-
|
13
|
+
self.logger = determine_logger(params[:logger])
|
14
|
+
delivery_method_name = params[:delivery_method] || :smtp
|
15
|
+
@delivery_method = if defined?(ActionMailer)
|
16
|
+
ActionMailer::Base.delivery_methods[delivery_method_name].new(settings)
|
17
|
+
else
|
18
|
+
Mail::Configuration.instance.lookup_delivery_method(delivery_method_name).new(settings)
|
19
|
+
end
|
10
20
|
@sendgrid_options = {}
|
11
21
|
end
|
12
22
|
|
13
23
|
def deliver!(mail)
|
14
24
|
self.mail = mail
|
15
|
-
|
25
|
+
original_recipients = recipients
|
26
|
+
allowed = filter(original_recipients)
|
27
|
+
|
28
|
+
log_delivery_summary(original_recipients, allowed)
|
16
29
|
|
17
30
|
if allowed.empty?
|
18
|
-
log
|
31
|
+
log 'SafetyMailer: No allowed recipients found - suppressing delivery altogether'
|
19
32
|
return
|
20
33
|
end
|
21
34
|
|
22
|
-
|
35
|
+
if sendgrid?
|
36
|
+
sendgrid_header = prepare_sendgrid_delivery(allowed)
|
37
|
+
mail.header.fields.delete_if { |f| f.name =~ /X-SMTPAPI/i }
|
38
|
+
mail['X-SMTPAPI'] = sendgrid_header
|
39
|
+
log 'SafetyMailer: Updated SendGrid header with filtered recipients'
|
40
|
+
end
|
23
41
|
mail.to = allowed
|
24
42
|
|
43
|
+
log "SafetyMailer: Delivering email to #{allowed.size} allowed recipient(s)"
|
25
44
|
@delivery_method.deliver!(mail)
|
26
45
|
end
|
27
46
|
|
@@ -30,15 +49,17 @@ module SafetyMailer
|
|
30
49
|
end
|
31
50
|
|
32
51
|
def filter(addresses)
|
52
|
+
return [] if addresses.nil? || addresses.empty?
|
53
|
+
|
33
54
|
allowed, rejected = addresses.partition { |r| whitelisted?(r) }
|
34
55
|
|
35
|
-
rejected.each { |addr| log "
|
36
|
-
allowed.each { |addr| log "
|
56
|
+
rejected.each { |addr| log "SafetyMailer: Suppressed delivery for #{addr} (no matching allowed pattern)" }
|
57
|
+
allowed.each { |addr| log "SafetyMailer: Allowed delivery for #{addr}" }
|
37
58
|
|
38
59
|
allowed
|
39
60
|
end
|
40
61
|
|
41
|
-
|
62
|
+
private
|
42
63
|
|
43
64
|
def recipients
|
44
65
|
sendgrid?
|
@@ -48,10 +69,11 @@ module SafetyMailer
|
|
48
69
|
|
49
70
|
def sendgrid?
|
50
71
|
@sendgrid ||= !!if mail['X-SMTPAPI']
|
51
|
-
|
52
|
-
|
72
|
+
@sendgrid_options = JSON.parse(mail['X-SMTPAPI'].value)
|
73
|
+
end
|
53
74
|
rescue JSON::ParserError
|
54
|
-
log
|
75
|
+
log 'SafetyMailer: Unable to parse X-SMTPAPI header - invalid JSON format'
|
76
|
+
false
|
55
77
|
end
|
56
78
|
|
57
79
|
# Handles clean-up for additional SendGrid features that may be required
|
@@ -67,12 +89,11 @@ module SafetyMailer
|
|
67
89
|
# whitelisted addresses.
|
68
90
|
#
|
69
91
|
# @see http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/substitution-tags/
|
70
|
-
if substitutions = @sendgrid_options['sub']
|
92
|
+
if (substitutions = @sendgrid_options['sub'])
|
71
93
|
substitutions.each do |template, values|
|
72
94
|
values = recipients.zip(values).map do |addr, value|
|
73
95
|
value if addresses.include?(addr)
|
74
96
|
end
|
75
|
-
|
76
97
|
substitutions[template] = values.compact
|
77
98
|
end
|
78
99
|
|
@@ -83,8 +104,26 @@ module SafetyMailer
|
|
83
104
|
end
|
84
105
|
|
85
106
|
def log(msg)
|
86
|
-
|
107
|
+
return unless logger
|
108
|
+
|
109
|
+
logger.warn(msg)
|
110
|
+
end
|
111
|
+
|
112
|
+
def determine_logger(custom_logger)
|
113
|
+
return custom_logger if custom_logger
|
114
|
+
return Rails.logger if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
115
|
+
|
116
|
+
nil
|
87
117
|
end
|
88
118
|
|
119
|
+
def log_delivery_summary(original_recipients, allowed_recipients)
|
120
|
+
return unless logger
|
121
|
+
|
122
|
+
total = original_recipients.size
|
123
|
+
allowed = allowed_recipients.size
|
124
|
+
suppressed = total - allowed
|
125
|
+
|
126
|
+
log "SafetyMailer: Processing #{total} recipient(s) - #{allowed} allowed, #{suppressed} suppressed"
|
127
|
+
end
|
89
128
|
end
|
90
129
|
end
|
data/lib/safety_mailer.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'safety_mailer/version'
|
4
|
+
require_relative 'safety_mailer/safety_mailer'
|
5
|
+
require_relative 'safety_mailer/railtie' if defined? Rails
|
metadata
CHANGED
@@ -1,108 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safety_mailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bill Kirtley
|
8
|
-
|
9
|
-
bindir: bin
|
8
|
+
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
- - ~>
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ~>
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: mail
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ~>
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ~>
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: actionmailer
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ~>
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
description: Specify a domain (or set of domains, or magic word in email address)
|
70
|
-
email is allowed to go to, and email to all other domains is silently dropped. Useful
|
71
|
-
for testing and staging environments.
|
72
|
-
email: bill.kirtley@gmail.com
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: Restrict email sent by your application. Useful for testing and staging
|
13
|
+
environments.
|
14
|
+
email:
|
15
|
+
- bill.kirtley@gmail.com
|
73
16
|
executables: []
|
74
17
|
extensions: []
|
75
18
|
extra_rdoc_files: []
|
76
19
|
files:
|
77
|
-
-
|
78
|
-
-
|
20
|
+
- ".rubocop.yml"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- CODE_OF_CONDUCT.md
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
79
25
|
- Rakefile
|
26
|
+
- assets/logo_ruby_hardhat.png
|
80
27
|
- lib/safety_mailer.rb
|
81
28
|
- lib/safety_mailer/railtie.rb
|
82
29
|
- lib/safety_mailer/safety_mailer.rb
|
83
30
|
- lib/safety_mailer/version.rb
|
31
|
+
- sig/safety_mailer.rbs
|
84
32
|
homepage: http://github.com/cluesque/safety_mailer
|
85
33
|
licenses:
|
86
34
|
- MIT
|
87
|
-
metadata:
|
88
|
-
|
35
|
+
metadata:
|
36
|
+
homepage_uri: http://github.com/cluesque/safety_mailer
|
37
|
+
source_code_uri: http://github.com/cluesque/safety_mailer
|
38
|
+
changelog_uri: http://github.com/cluesque/safety_mailer/blob/master/CHANGELOG.md
|
39
|
+
rubygems_mfa_required: 'true'
|
89
40
|
rdoc_options: []
|
90
41
|
require_paths:
|
91
42
|
- lib
|
92
43
|
required_ruby_version: !ruby/object:Gem::Requirement
|
93
44
|
requirements:
|
94
|
-
- -
|
45
|
+
- - ">="
|
95
46
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
47
|
+
version: 2.7.5
|
97
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
49
|
requirements:
|
99
|
-
- -
|
50
|
+
- - ">="
|
100
51
|
- !ruby/object:Gem::Version
|
101
52
|
version: '0'
|
102
53
|
requirements: []
|
103
|
-
|
104
|
-
rubygems_version: 2.4.2
|
105
|
-
signing_key:
|
54
|
+
rubygems_version: 3.6.9
|
106
55
|
specification_version: 4
|
107
56
|
summary: Restrict email sent by your application to only approved domains or accounts.
|
108
57
|
test_files: []
|
data/Gemfile
DELETED
data/README.rdoc
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
== safety_mailer
|
2
|
-
|
3
|
-
Restrict email sent by your application to only approved domains or accounts.
|
4
|
-
|
5
|
-
Specify a domain (or set of domains, or magic word in email address) email is allowed to go to, and email to all other domains is silently dropped.
|
6
|
-
|
7
|
-
This is useful for testing or staging environments where you want to be certain email to real customers doesn't escape the lab.
|
8
|
-
|
9
|
-
Layered on the Mail gem, so Rails >= 3.0 applications can use safety_mailer.
|
10
|
-
|
11
|
-
== Rails >= 3.0
|
12
|
-
|
13
|
-
Add the gem to your +Gemfile+, specifying groups (probably not production) to include it in.
|
14
|
-
|
15
|
-
gem "safety_mailer", :group => :development
|
16
|
-
|
17
|
-
Don't forget to <tt>bundle install</tt> to install
|
18
|
-
|
19
|
-
In your environment file <tt>config/environments/development.rb</tt> configure it, and some regular expressions.
|
20
|
-
|
21
|
-
config.action_mailer.delivery_method = :safety_mailer
|
22
|
-
config.action_mailer.safety_mailer_settings = {
|
23
|
-
allowed_matchers: [ /mydomain.com/, /mytestacct@gmail.com/, /super_secret_test/ ],
|
24
|
-
delivery_method: :smtp,
|
25
|
-
delivery_method_settings: {
|
26
|
-
:address => "smtp.mydomain.com",
|
27
|
-
:port => 25,
|
28
|
-
:domain => "mydomain.com",
|
29
|
-
:authentication => :plain,
|
30
|
-
:user_name => "mydomain_mailer@mydomain.com",
|
31
|
-
:password => "password"
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
... and now, email to anyone@mydomain.com, mytestacct@gmail.com, bob+super_secret_test@yahoo.com all get sent
|
36
|
-
and email to other recipients (like the real users in the production database you copied to a test server) is suppressed.
|
37
|
-
|
38
|
-
== Non-Rails
|
39
|
-
|
40
|
-
Any user of the Mail gem can configure safety_mailer:
|
41
|
-
|
42
|
-
require "safety_mailer"
|
43
|
-
Mail.defaults do
|
44
|
-
delivery_method SafetyMailer::Carrier, {
|
45
|
-
... same settings as above
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
== Non-Mail
|
50
|
-
|
51
|
-
If you're not using the Mail gem (or use it sometimes but want to use the same logic / configuration in other contexts),
|
52
|
-
you can filter directly:
|
53
|
-
|
54
|
-
filtered_array = SafetyMailer::Carrier.new(ActionMailer::Base.safety_mailer_settings).filter(unfiltered_email_addresses)
|
55
|
-
|
56
|
-
== License
|
57
|
-
|
58
|
-
safety_mailer is released under the MIT license:
|
59
|
-
|
60
|
-
* http://www.opensource.org/licenses/MIT
|