multi_smtp 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b4c0d11e7bf9ef7fdbe9599079af40962c5619f
4
+ data.tar.gz: 82f6f530e1025b1e87ddf17dc515dc052abaf79b
5
+ SHA512:
6
+ metadata.gz: e4716c7ea2d5d1c583fd7eee596b21eb2bfdb20d5d24c5803dc64f94637b1a88db2ec4377bd25a9da75b33688f7ae1c7c920528fa72c4abb48c3928d23a59528
7
+ data.tar.gz: 091f3eff7730e6afd13a2d2fc777d0cc7d278c6b01f33afe314e9d6abd1fc7510bba12218556f4d3c9a590a684b30be8c7c1340587499802ea86e2a77956fc48
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_smtp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Harlow Ward
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/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # MultiSMTP
2
+
3
+ Email delivery is a critical component of many web applications. Often
4
+ third-party Email services can experience temporary downtime.
5
+
6
+ We can achieve automatic failover by overriding the default delivery method with the
7
+ MultiSMTP class.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "multi_smtp"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Configuration
22
+
23
+ Set the delivery method to `:multi_smtp` for each environment that should use
24
+ the automatic failover.
25
+
26
+ ```ruby
27
+ # config/environments/{staging,production}.rb
28
+ ExampleApp::Application.configure do
29
+ config.action_mailer.delivery_method = :multi_smtp
30
+ end
31
+ ```
32
+
33
+ In an initializer configure the MultiSMTP class with an array of (1..N) SMTP
34
+ Providers.
35
+
36
+ ```ruby
37
+ # config/initializers/multi_smtp.rb
38
+ sendgrid_settings = {
39
+ address: 'smtp.sendgrid.net',
40
+ authentication: :plain,
41
+ domain: 'hoteltonight.com',
42
+ password: ENV['SENDGRID_PASSWORD'],
43
+ port: '587',
44
+ user_name: ENV['SENDGRID_USERNAME'],
45
+ }
46
+
47
+ other_smtp_settings = {
48
+ # Other SMTP settings
49
+ }
50
+
51
+ MultiSMTP.smtp_providers = [sendgrid_settings, other_smtp_settings]
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it ( https://github.com/[my-github-username]/multi_smtp/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,40 @@
1
+ require "mail"
2
+
3
+ module MultiSMTP
4
+ class Mail < Mail::SMTP
5
+ def initialize(default_settings)
6
+ @default_settings = default_settings
7
+ end
8
+
9
+ def deliver!(mail)
10
+ smtp_providers.each_with_index do |smtp_provider, index|
11
+ self.settings = default_settings.merge(smtp_provider)
12
+
13
+ begin
14
+ super(mail)
15
+ break
16
+ rescue Exception => e
17
+ if all_providers_failed?(index) && error_notifier
18
+ error_notifier.notify(mail)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def smtp_providers
27
+ @smtp_providers ||= MultiSMTP.smtp_providers
28
+ end
29
+
30
+ def error_notifier
31
+ @error_notifier ||= MultiSMTP.error_notifier
32
+ end
33
+
34
+ def all_providers_failed?(index)
35
+ (smtp_providers.size - 1) == index
36
+ end
37
+
38
+ attr_reader :default_settings
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ module MultiSMTP
2
+ module Notifiers
3
+ module Airbrake
4
+ def self.notify(e, mail)
5
+ Airbrake.notify(e,
6
+ error_message: "Email delivery failed with all SMTP providers.",
7
+ parameters: { mail: extract_mail_params(mail) }
8
+ )
9
+ end
10
+
11
+ def self.extract_mail_params(mail)
12
+ {
13
+ delivery_handler: mail.delivery_handler.to_s,
14
+ from: mail.from,
15
+ subject: mail.subject,
16
+ to: mail.to
17
+ }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module MultiSMTP
2
+ VERSION = "0.0.1"
3
+ end
data/lib/multi_smtp.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "multi_smtp/mail"
2
+ require "multi_smtp/version"
3
+
4
+ module MultiSMTP
5
+ def self.error_notifier=(notifier)
6
+ @error_notifier = notifier
7
+ end
8
+
9
+ def self.error_notifier
10
+ @error_notifier || false
11
+ end
12
+
13
+ def self.smtp_providers=(providers)
14
+ @smtp_providers = providers
15
+ end
16
+
17
+ def self.smtp_providers
18
+ @smtp_providers || raise("MultiSMTP Error: Please specify smtp_providers.")
19
+ end
20
+ end
21
+
22
+ if defined?(Rails)
23
+ ActionMailer::Base.add_delivery_method(:multi_smtp, MultiSMTP::Mail)
24
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "multi_smtp/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_smtp"
8
+ spec.version = MultiSMTP::VERSION
9
+ spec.authors = ["Harlow Ward"]
10
+ spec.email = ["harlow@hward.com"]
11
+ spec.summary = %q{Email failover in Rails with MultiSMTP}
12
+ spec.description = %q{Email failover in Rails with MultiSMTP}
13
+ spec.homepage = "https://github.com/harlow/multi_smtp"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "mail", "~> 2.6.3"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.1.0"
26
+ end
@@ -0,0 +1,70 @@
1
+ require "spec_helper"
2
+
3
+ describe MultiSMTP::Mail, "#deliver!" do
4
+ let(:error_notifier) { double(:notifier) }
5
+
6
+ before do
7
+ allow(error_notifier).to receive(:notify)
8
+ MultiSMTP.error_notifier = error_notifier
9
+ MultiSMTP.smtp_providers = [
10
+ provider("smtp.sendgrid.net", "sendgrid"),
11
+ provider("smtp.amazaon_ses.com", "amazon_ses")
12
+ ]
13
+ end
14
+
15
+ context "primary email provider succeeds" do
16
+ it "sends an email" do
17
+ allow_any_instance_of(Net::SMTP).to receive(:start).and_return(true)
18
+
19
+ MultiSMTP::Mail.new({}).deliver!(mail)
20
+
21
+ expect(error_notifier).not_to have_received(:notify)
22
+ end
23
+ end
24
+
25
+ context "primary email provider fails" do
26
+ it "sends an email with secondary provider" do
27
+ allow_any_instance_of(Net::SMTP).to receive(:start).
28
+ with("test.com", "sendgrid", "password", :login).
29
+ and_raise(Net::SMTPFatalError)
30
+
31
+ allow_any_instance_of(Net::SMTP).to receive(:start).
32
+ with("test.com", "amazon_ses", "password", :login).
33
+ and_return(true)
34
+
35
+ MultiSMTP::Mail.new({}).deliver!(mail)
36
+
37
+ expect(error_notifier).to_not have_received(:notify)
38
+ end
39
+ end
40
+
41
+ context "all smtp providers fail" do
42
+ it "notifies airbrake" do
43
+ allow_any_instance_of(Net::SMTP).to receive(:start).
44
+ and_raise(Net::SMTPFatalError)
45
+
46
+ MultiSMTP::Mail.new({}).deliver!(mail)
47
+
48
+ expect(error_notifier).to have_received(:notify).once
49
+ end
50
+ end
51
+
52
+ def mail
53
+ Mail.new(
54
+ delivery_handler: "test",
55
+ from: "from@test.com",
56
+ subject: "test",
57
+ to: "to@test.com"
58
+ )
59
+ end
60
+
61
+ def provider(address, user_name)
62
+ {
63
+ address: address,
64
+ authentication: :login,
65
+ domain: "test.com",
66
+ password: "password",
67
+ user_name: user_name
68
+ }
69
+ end
70
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe MultiSMTP, ".smtp_providers" do
4
+ it "returns an array of providers" do
5
+ MultiSMTP.smtp_providers = [:provider1, :provider2]
6
+
7
+ result = MultiSMTP.smtp_providers
8
+
9
+ expect(result).to eq [:provider1, :provider2]
10
+ end
11
+
12
+ context "no providers set" do
13
+ it "raises and exception" do
14
+ expect { MultiSMTP.smtp_providers }.to raise_exception
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
3
+
4
+ require "rubygems"
5
+ require "rspec"
6
+ require "multi_smtp"
7
+
8
+ RSpec.configure do |config|
9
+ config.after :each do
10
+ MultiSMTP.smtp_providers = nil
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_smtp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Harlow Ward
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.6.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.6.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ description: Email failover in Rails with MultiSMTP
70
+ email:
71
+ - harlow@hward.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/multi_smtp.rb
82
+ - lib/multi_smtp/mail.rb
83
+ - lib/multi_smtp/notifiers/airbrake.rb
84
+ - lib/multi_smtp/version.rb
85
+ - multi_smtp.gemspec
86
+ - spec/lib/multi_smtp/mail_spec.rb
87
+ - spec/lib/multi_smtp_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: https://github.com/harlow/multi_smtp
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Email failover in Rails with MultiSMTP
113
+ test_files:
114
+ - spec/lib/multi_smtp/mail_spec.rb
115
+ - spec/lib/multi_smtp_spec.rb
116
+ - spec/spec_helper.rb