ar_mailer_revised 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b1e7179059d80144067968beac5f41151eda097
4
- data.tar.gz: 42554e87ed8bfb15baef54d94a7c0e28e0c7b515
3
+ metadata.gz: e31b96ff2c52c7ea6de5612eaf78aaac96f0c848
4
+ data.tar.gz: 8bee7faa04dd6dc0b6e873fd8c54d9bd6f2832b9
5
5
  SHA512:
6
- metadata.gz: 0cb4f0f7c22ebf0e85ff61ded487ac20acf38036e0b8f45dd074923f153aeda836e416af01d73fa14263fe9f2203f76e7b8a653d938e8408f2d670899ccd2d2e
7
- data.tar.gz: 827bb354ebb58010182591543be6ee60774146de9e5b9f5eda8649f39daf44d6c309c2cd986b3bb7a73e7e9812ce4b133099507b4b6b3613d6b4d0d846f966db
6
+ metadata.gz: e2c952822e0eef6031bcb68d4441dc8dbc50c8940f04390c9dcffa59fd3a7c1bc4f456e8b0252cb6e646a136489b8cf0a8fd2f4cac699fa7e496d1108d9ebe5d
7
+ data.tar.gz: 09de5500a627fb78d1259476b458d17c5a62629e6e472700be4c736491a071b706beef0a849d9fdee269b385bbe25e8d686b3ba3b96bf32bd5d7c2be779cbd95
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ Version 1.0.2
2
+ -------------
3
+
4
+ - Remove custom SMTP settings on SMTP SyntaxErrors.
5
+ This makes more sense than simply trying to re-deliver them
6
+ in the next batch as it will most likely still be bad syntax.
7
+ - Emails with Syntax or Authentication errors without custom SMTP
8
+ settings are now handled by adjusting their last send attempt.
9
+ This causes them to go to the end of the sending queue where they
10
+ will either be deleted after some time or - hopefully - eventually
11
+ sent when the developer corrects his application's SMTP settings.
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'sqlite3'
27
27
  spec.add_development_dependency 'minitest'
28
28
  spec.add_development_dependency 'shoulda'
29
+ spec.add_development_dependency 'mailcatcher'
29
30
 
30
31
  spec.add_dependency 'rails', '~> 4'
31
32
  spec.add_dependency 'log4r'
@@ -46,14 +46,14 @@ class ActionMailer::Base
46
46
  #
47
47
  def ar_mailer_setting(key, value = nil)
48
48
  if headers[:ar_mailer_settings]
49
- settings = YAML.load(headers[:ar_mailer_settings]).stringify_keys
49
+ settings = YAML::load(headers[:ar_mailer_settings]).stringify_keys
50
50
  else
51
51
  settings = {}
52
52
  end
53
53
 
54
54
  if value
55
55
  settings[key.to_s] = value
56
- headers[:ar_mailer_settings] = YAML.dump(settings)
56
+ headers[:ar_mailer_settings] = YAML::dump(settings)
57
57
  else
58
58
  settings[key.to_s]
59
59
  end
@@ -78,7 +78,7 @@ module ActionMailer
78
78
  #
79
79
  def deliver!(mail)
80
80
  if mail['ar_mailer_settings']
81
- ar_settings = YAML.load(mail['ar_mailer_settings'].value).stringify_keys
81
+ ar_settings = YAML::load(mail['ar_mailer_settings'].value).stringify_keys
82
82
  mail['ar_mailer_settings'] = nil
83
83
  else
84
84
  ar_settings = {}
@@ -86,7 +86,7 @@ module ActionMailer
86
86
 
87
87
  email_options = {}
88
88
  email_options[:delivery_time] = ar_settings.delete('delivery_time')
89
- email_options[:smtp_settings] = ar_settings.delete('smtp_settings')
89
+ email_options[:smtp_settings] = ar_settings.delete('smtp_settings').try(:symbolize_keys)
90
90
  email_options[:mail] = mail.encoded
91
91
  email_options[:from] = (mail['return-path'] && mail['return-path'].spec) || mail.from.first
92
92
  email_options.reverse_merge!(ar_settings['custom_attributes'] || {})
@@ -58,6 +58,7 @@ module ArMailerRevised
58
58
  group_emails_by_settings(emails).each do |settings_hash, grouped_emails|
59
59
  setting = OpenStruct.new(settings_hash)
60
60
  logger.info "Using setting #{setting.address}:#{setting.port}/#{setting.user_name}"
61
+ logger.info setting.inspect
61
62
 
62
63
  smtp = Net::SMTP.new(setting.address, setting.port)
63
64
  smtp.open_timeout = 10
@@ -73,12 +74,14 @@ module ArMailerRevised
73
74
  end
74
75
  rescue Net::SMTPAuthenticationError => e
75
76
  handle_smtp_authentication_error(setting, e, grouped_emails)
77
+ rescue Net::SMTPSyntaxError => e
78
+ handle_smtp_syntax_error(setting, e, grouped_emails)
76
79
  rescue Net::SMTPServerBusy => e
77
80
  logger.warn 'Server is busy, trying again next batch.'
78
81
  logger.warn 'Complete Error: ' + e.to_s
79
82
  rescue Net::OpenTimeout, Net::ReadTimeout => e
80
83
  handle_smtp_timeout(setting, e, grouped_emails)
81
- rescue Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
84
+ rescue Net::SMTPFatalError, Net::SMTPUnknownError => e
82
85
  #TODO: Should we remove the custom SMTP settings here as well?
83
86
  logger.warn 'Other SMTP error, trying again next batch.'
84
87
  logger.warn 'Complete Error: ' + e.to_s
@@ -207,9 +210,9 @@ module ArMailerRevised
207
210
  logger.warn 'Complete Error: ' + exception.to_s
208
211
 
209
212
  if setting.custom_setting
213
+ logger.warn 'Setting default SMTP settings for all affected emails, they will be sent next batch.'
210
214
  emails.each do |email|
211
215
  if email.previously_attempted?
212
- logger.warn 'Setting default SMTP settings for all affected emails, they will be sent next batch.'
213
216
  remove_custom_smtp_settings!(email)
214
217
  else
215
218
  adjust_last_send_attempt!(email)
@@ -231,12 +234,23 @@ module ArMailerRevised
231
234
 
232
235
  if setting.custom_setting
233
236
  logger.warn 'Setting default SMTP settings for all affected emails, they will be sent next batch.'
234
-
235
- if setting.custom_setting
236
- emails.each { |email| remove_custom_smtp_settings!(email) }
237
- end
237
+ emails.each { |email| remove_custom_smtp_settings!(email) }
238
238
  else
239
239
  logger.error "Your application's base setting ('#{setting.host}:#{setting.port}') produced an authentication error!"
240
+ emails.each { |email| adjust_last_send_attempt!(email) }
241
+ end
242
+ end
243
+
244
+ def handle_smtp_syntax_error(setting, exception, emails)
245
+ logger.warn "SMTP syntax error while connecting to '#{setting.host}:#{setting.port}'"
246
+ logger.warn 'Complete Error: ' + exception.to_s
247
+
248
+ if setting.custom_setting
249
+ logger.warn 'Setting default SMTP settings for all affected emails, they will be sent next batch.'
250
+ emails.each { |email| remove_custom_smtp_settings!(email) }
251
+ else
252
+ emails.each { |email| adjust_last_send_attempt!(email) }
253
+ logger.error "Your application's base setting ('#{setting.host}:#{setting.port}') produced a syntax error!"
240
254
  end
241
255
  end
242
256
 
@@ -1,3 +1,3 @@
1
1
  module ArMailerRevised
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -1,7 +1,13 @@
1
- require 'test_helper'
1
+ # require 'test_helper'
2
+ require 'open-uri'
3
+ require 'ar_mailer_revised/mailman'
2
4
 
3
5
  class TestMailerTest < ActionMailer::TestCase
4
6
 
7
+ #----------------------------------------------------------------
8
+ # Email Generation Testing
9
+ #----------------------------------------------------------------
10
+
5
11
  context 'ActionMailer::Base.deliver' do
6
12
  setup do
7
13
  #@todo: Check if there is a way to set this in the dummy application itself,
@@ -51,4 +57,75 @@ class TestMailerTest < ActionMailer::TestCase
51
57
  assert_equal 42, email.a_number
52
58
  end
53
59
  end
60
+
61
+ #----------------------------------------------------------------
62
+ # SMTP Testing
63
+ #----------------------------------------------------------------
64
+
65
+ context 'Email sending' do
66
+ setup do
67
+ ActionMailer::Base.delivery_method = :activerecord
68
+ if mailcatcher_running?
69
+ setup_mailcatcher_settings
70
+ @old_email_count = received_emails.size
71
+ else
72
+ puts 'Mailcatcher is not running, SMTP tests are skipped'
73
+ end
74
+ end
75
+
76
+ should 'send out basic emails correctly' do
77
+ if mailcatcher_running?
78
+ assert TestMailer.basic_email.deliver
79
+ run_ar_sendmail
80
+ assert_equal @old_email_count + 1, received_emails.count, 'Email was not sent to local SMTP server'
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ private
87
+
88
+ #
89
+ # Executes ar_sendmail to send deliverable emails
90
+ #
91
+ def run_ar_sendmail
92
+ run_options = {
93
+ :chdir => Rails.root,
94
+ :rails_env => 'test',
95
+ :log_level => 'debug'
96
+ }
97
+ ArMailerRevised::Mailman.new(run_options).run
98
+ end
99
+
100
+ #
101
+ # @return [Boolean] +true+ if mailcatcher is running
102
+ #
103
+ def mailcatcher_running?
104
+ begin
105
+ open('http://localhost:1080')
106
+ true
107
+ rescue Errno::ECONNREFUSED
108
+ false
109
+ end
110
+ end
111
+
112
+ def setup_mailcatcher_settings
113
+ ActionMailer::Base.smtp_settings = {
114
+ :address => 'localhost',
115
+ :port => 1025,
116
+ :domain => 'localhost.localdomain',
117
+ :user_name => 'some.user',
118
+ :password => 'some.password',
119
+ :authentication => :plain,
120
+ :enable_starttls_auto => true
121
+ }
122
+ end
123
+
124
+ #
125
+ # @return [Array<Hash>] Message Meta-Data for all received emails
126
+ #
127
+ def received_emails
128
+ json = open('http://localhost:1080/messages')
129
+ JSON.load(json)
130
+ end
54
131
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_mailer_revised
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Exner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-20 00:00:00.000000000 Z
11
+ date: 2014-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: mailcatcher
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rails
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -162,6 +176,7 @@ files:
162
176
  - ".ruby-version"
163
177
  - ".travis.yml"
164
178
  - ".yardopts"
179
+ - CHANGELOG.md
165
180
  - Gemfile
166
181
  - LICENSE.txt
167
182
  - MIT-LICENSE
@@ -252,7 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
252
267
  version: '0'
253
268
  requirements: []
254
269
  rubyforge_project:
255
- rubygems_version: 2.2.2
270
+ rubygems_version: 2.4.2
256
271
  signing_key:
257
272
  specification_version: 4
258
273
  summary: Batch email sending for rails applications
@@ -309,3 +324,4 @@ test_files:
309
324
  - test/dummy/test/mailers/test_mailer_test.rb
310
325
  - test/generators/install_generator_test.rb
311
326
  - test/test_helper.rb
327
+ has_rdoc: