mail_plugger 1.3.0 → 1.6.0

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
  SHA256:
3
- metadata.gz: f909bb4d7136983266dbbe01519966ec0a3f5166cd7576401de5129e8fb6d255
4
- data.tar.gz: 79a650bdbe95199bb28ff1dddefbf405d09e2dcf661d8bb0557b7c4594d85cc4
3
+ metadata.gz: 7ec4ac88872c10e0533aac02a9947fdb9f93a7027d004e380cd5a0f854db3840
4
+ data.tar.gz: 82f4adcbdcac821c94161cc61da1cb2228f3fee00fbfdd7306a17fe5e87a5347
5
5
  SHA512:
6
- metadata.gz: 82416b31f9a56713434d37055caf55c83d361ea0bb22f1d643116106bc13cb5803974d9f89945ef938471fb63626742299353f40673df24f49f2ef098172daee
7
- data.tar.gz: 2ee3ad7f530aad9e8dcf864bef0dc17c3dd23fea193fa796cffd676bf917f11d36a0af95632edb46fd2bb0267c12035f95b4783dd80fea1cff2125d5fcfc90f6
6
+ metadata.gz: de83d9be11f14e045b70bdfcc6bac03353d3b84fee8e5858cf24558236925d750015117f0d04efb953161b735bf8c3beaa1ac474483e07856a215778633132c8
7
+ data.tar.gz: 03ff6162170252b04c7af6f7d1754ecd7d8a6e8c1843a59986b5c1b8a279f54542abd744e57e923e524f8526dc1866296a873ae0e1776567da2b94c1f37780e6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
1
1
  # Change log
2
2
 
3
+ ## 1.6.0 (2022-05-26)
4
+
5
+ ### Changes
6
+
7
+ * Drop Ruby 2.6 support.
8
+ * Fix some grammar issues and typos.
9
+ * Update bundler and gems.
10
+
11
+
12
+ ## 1.5.0 (2021-12-31)
13
+
14
+ ### Changes
15
+
16
+ * Add Ruby 3.1 support.
17
+ * Drop Ruby 2.5 support.
18
+ * Require MFA on RubyGems.
19
+ * Update bundler and gems.
20
+ * Fix typo in the README.md.
21
+ * Replace Travis with GitHub Actions.
22
+
23
+
24
+ ## 1.4.0 (2021-06-08)
25
+
26
+ ### New features
27
+
28
+ * Add SMTP support. With this modifications we can use SMTP and API delivery methods as well.
29
+
30
+ ### Changes
31
+
32
+ * Update bundler and gems.
33
+
34
+
3
35
  ## 1.3.0 (2021-04-14)
4
36
 
5
37
  ### New features
data/README.md CHANGED
@@ -3,11 +3,11 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/mail_plugger.svg)](https://badge.fury.io/rb/mail_plugger)
4
4
  [![MIT license](https://img.shields.io/badge/license-MIT-brightgreen)](https://github.com/MailToolbox/mail_plugger/blob/main/LICENSE.txt)
5
5
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
6
- [![Build Status](https://travis-ci.com/MailToolbox/mail_plugger.svg?branch=main)](https://travis-ci.com/MailToolbox/mail_plugger)
6
+ [![MailPlugger CI](https://github.com/MailToolbox/mail_plugger/actions/workflows/mail_plugger_ci.yml/badge.svg)](https://github.com/MailToolbox/mail_plugger/actions/workflows/mail_plugger_ci.yml)
7
7
  [![Maintainability](https://api.codeclimate.com/v1/badges/137881380fc475b4a836/maintainability)](https://codeclimate.com/github/MailToolbox/mail_plugger/maintainability)
8
8
  [![Test Coverage](https://api.codeclimate.com/v1/badges/137881380fc475b4a836/test_coverage)](https://codeclimate.com/github/MailToolbox/mail_plugger/test_coverage)
9
9
 
10
- **MailPlugger** helps you to use different mail providers' **API**. You can use any APIs which one would like to use. It allows you to send different emails with different APIs. Also it can help to move between providers, load balacing or cost management.
10
+ **MailPlugger** helps you to use different mail providers' **SMTP** and **API**. You can use any SMTPs and APIs which one would like to use. It allows you to send different emails with different SMTPs and APIs. Also, it can help to move between providers, load balancing or cost management.
11
11
 
12
12
  ## Installation
13
13
 
@@ -31,26 +31,29 @@ Or install it yourself as:
31
31
  - [How to use MailPlugger::DeliveryMethod class](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_delivery_method.md)
32
32
  - [How to use MailPlugger in a Ruby script or IRB console](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_in_script_or_console.md)
33
33
  - [How to use MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md)
34
- - [How to add API specific options to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_special_options_in_ruby_on_rails.md)
35
- - [How to add attachments to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_attachments_in_ruby_on_rails.md)
36
- - [How to use more delivey systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_more_delivery_system_in_ruby_on_rails.md)
37
- - [How to use one delivey system with more send methods in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_one_delivery_system_with_more_send_methods_in_ruby_on_rails.md)
38
- - [How to use AWS SES with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_aws_ses_in_ruby_on_rails.md)
39
- - [How to use SparkPost with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sparkpost_in_ruby_on_rails.md)
40
- - [How to use SendGrid with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sendgrid_in_ruby_on_rails.md)
41
- - [How to use Mandrill with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mandrill_in_ruby_on_rails.md)
42
- - [How to use Postmark with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_postmark_in_ruby_on_rails.md)
43
- - [How to use Mailgun with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mailgun_in_ruby_on_rails.md)
34
+ - [How to use more SMTP delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_more_smtp_delivery_systems_in_ruby_on_rails.md)
35
+ - [How to combine SMTP and API delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_smtp_and_api_delivery_systems_in_ruby_on_rails.md)
36
+ - [How to add API specific options to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_api_specific_options_in_ruby_on_rails.md)
37
+ - [How to use API delivery system that add attachments to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_api_attachments_in_ruby_on_rails.md)
38
+ - [How to use more API delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_more_api_delivery_systems_in_ruby_on_rails.md)
39
+ - [How to use one API delivery system with more send methods in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_one_api_delivery_system_with_more_send_methods_in_ruby_on_rails.md)
40
+ - [How to use AWS SES API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_aws_ses_api_in_ruby_on_rails.md)
41
+ - [How to use SparkPost API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sparkpost_api_in_ruby_on_rails.md)
42
+ - [How to use SendGrid API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sendgrid_api_in_ruby_on_rails.md)
43
+ - [How to use Mandrill API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mandrill_api_in_ruby_on_rails.md)
44
+ - [How to use Postmark API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_postmark_api_in_ruby_on_rails.md)
45
+ - [How to use Mailgun API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mailgun_api_in_ruby_on_rails.md)
44
46
 
45
47
  # F<img src="https://raw.githubusercontent.com/MailToolbox/mail_plugger/main/images/fake_plugger800x500.png" height="22" />kePlugger
46
48
 
47
- **FakePlugger** is a delivery method to mock **MailPlugger**. It is working similarly like **MailPlugger**, but it won't send any emails (but if we would like it is possible, just we should do manually). Also it can write out debug informations or we can manipulate the response with it. Another option that we can use **[MailGrabber](https://github.com/MailToolbox/mail_grabber)** web interface if the delivery method is `:fake_plugger`.
49
+ **FakePlugger** is a delivery method to mock **MailPlugger**. It is working similarly like **MailPlugger**, but it won't send any emails (but if we would like it is possible, just we should do manually). Also, it can write out debug information, or we can manipulate the response with it. Another option that we can use **[MailGrabber](https://github.com/MailToolbox/mail_grabber)** web interface if the delivery method is `:fake_plugger`.
48
50
 
49
51
  ## Usage
50
52
 
51
53
  - [How to use FakePlugger::DeliveryMethod class](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_delivery_method.md)
52
54
  - [How to use FakePlugger in a Ruby script or IRB console](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_in_script_or_console.md)
53
55
  - [How to use FakePlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_in_ruby_on_rails.md)
56
+ - [How to use FakePlugger with more delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_with_more_delivery_systems_in_ruby_on_rails.md)
54
57
  - [How to use FakePlugger with MailGrabber in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_with_mail_grabber_in_ruby_on_rails.md)
55
58
 
56
59
  ## Development
@@ -23,10 +23,10 @@ module FakePlugger
23
23
  # e.g. 'defined_api'
24
24
  #
25
25
  # @option options [Boolean] debug
26
- # if true it will show debug informations
26
+ # if true, it will show debug information
27
27
  #
28
28
  # @option options [Boolean] raw_message
29
- # if true it will show raw message
29
+ # if true, it will show raw message
30
30
  #
31
31
  # @option options [String/Symbol/Array/Hash] response
32
32
  # the deliver! method will return with this value or if this value is nil
@@ -38,30 +38,37 @@ module FakePlugger
38
38
  def initialize(options = {})
39
39
  super
40
40
 
41
- @debug = options[:debug] ||
42
- settings[:fake_plugger_debug] || false
43
- @raw_message = options[:raw_message] ||
44
- settings[:fake_plugger_raw_message] || false
45
- @response = options[:response] || settings[:fake_plugger_response]
46
- @use_mail_grabber = options[:use_mail_grabber] ||
47
- settings[:fake_plugger_use_mail_grabber] || false
41
+ @debug = options[:debug]
42
+
43
+ @raw_message = options[:raw_message]
44
+
45
+ @response = options[:response]
46
+
47
+ @use_mail_grabber = options[:use_mail_grabber]
48
48
  end
49
49
 
50
+ # Using SMTP:
51
+ # Mock send message via SMTP protocol if the 'delivery_settings' contains a
52
+ # 'smtp_settings' key and the value is a hash with the settings.
53
+ #
54
+ # Using API:
50
55
  # Mock send message with the given client if the message parameter is a
51
- # Mail::Message object. If 'response' parameter is nil then it will extract
52
- # those information from the Mail::Message object which was provided in the
56
+ # Mail::Message object. If 'response' parameter is nil, then it will extract
57
+ # this information from the Mail::Message object which was provided in the
53
58
  # 'delivery_options'. After that it generates a hash with these data and
54
59
  # returns with the provided client class which has a 'deliver' method, but
55
60
  # it won't call the 'deliver' method.
56
61
  # If the 'response' parameter is a hash with 'return_delivery_data: true'
57
- # then it will retrun with the extracted delivery data.
58
- # If the 'response' parameter is not nil then retruns with that given data
62
+ # then it will return with the extracted delivery data.
63
+ #
64
+ #
65
+ # If the 'response' parameter is not nil, then returns with that given data
59
66
  # without call any other methods.
60
- # Except if 'debug' is true. In this case it will call those methods which
61
- # is calling in normal operation as well.
62
- # If 'debug' is true then it prints out some debug informations.
63
- # If 'raw_message' is true then it prints out raw message.
64
- # if 'use_mail_grabber' is true then it stores the message in a database.
67
+ # Except if 'debug' is true. In this case, it will call those methods which
68
+ # are calling in normal operation as well.
69
+ # If 'debug' is true, then it prints out some debug information.
70
+ # If 'raw_message' is true, then it prints out raw message.
71
+ # If 'use_mail_grabber' is true, then it stores the message in a database.
65
72
  #
66
73
  # @param [Mail::Message] message what we would like to send
67
74
  #
@@ -69,6 +76,48 @@ module FakePlugger
69
76
  #
70
77
  # @example
71
78
  #
79
+ # # Using SMTP:
80
+ #
81
+ # MailPlugger.plug_in('test_smtp_client') do |smtp|
82
+ # smtp.delivery_settings = {
83
+ # smtp_settings: {
84
+ # address: 'smtp.server.com',
85
+ # port: 587,
86
+ # domain: 'test.domain.com',
87
+ # enable_starttls_auto: true,
88
+ # user_name: 'test_user',
89
+ # password: '1234',
90
+ # authentication: :plain
91
+ # }
92
+ # }
93
+ # end
94
+ #
95
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
96
+ # subject: 'Test email', body: 'Test email body')
97
+ #
98
+ # FakePlugger::DeliveryMethod.new.deliver!(message)
99
+ #
100
+ # # or
101
+ #
102
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
103
+ # subject: 'Test email', body: 'Test email body')
104
+ #
105
+ # FakePlugger::DeliveryMethod.new(
106
+ # delivery_settings: {
107
+ # smtp_settings: {
108
+ # address: 'smtp.server.com',
109
+ # port: 587,
110
+ # domain: 'test.domain.com',
111
+ # enable_starttls_auto: true,
112
+ # user_name: 'test_user',
113
+ # password: '1234',
114
+ # authentication: :plain
115
+ # }
116
+ # }
117
+ # ).deliver!(message)
118
+ #
119
+ # # Using API:
120
+ #
72
121
  # MailPlugger.plug_in('test_api_client') do |api|
73
122
  # api.delivery_options = %i[from to subject body]
74
123
  # api.delivery_settings = {
@@ -107,6 +156,8 @@ module FakePlugger
107
156
 
108
157
  @message = message
109
158
 
159
+ update_settings
160
+
110
161
  call_extra_options
111
162
 
112
163
  return_with_response
@@ -114,8 +165,8 @@ module FakePlugger
114
165
 
115
166
  private
116
167
 
117
- # Call extra options like show debug informations, show raw message,
118
- # use mail grabber
168
+ # Call extra options like show debug information, show raw message,
169
+ # use mail grabber.
119
170
  def call_extra_options
120
171
  show_debug_info if @debug
121
172
  show_raw_message if @raw_message
@@ -125,18 +176,8 @@ module FakePlugger
125
176
  MailGrabber::DeliveryMethod.new.deliver!(@message)
126
177
  end
127
178
 
128
- # Return with a response which depends on the conditions.
129
- def return_with_response
130
- return client.new(delivery_data) if @response.nil?
131
- if @response.is_a?(Hash) && @response[:return_delivery_data]
132
- return delivery_data
133
- end
134
-
135
- @response
136
- end
137
-
138
- # Show debug informations from variables and methods.
139
- def show_debug_info
179
+ # Debug information for API
180
+ def debug_info_for_api
140
181
  puts <<~DEBUG_INFO
141
182
 
142
183
  ===================== FakePlugger::DeliveryMethod =====================
@@ -170,6 +211,62 @@ module FakePlugger
170
211
  DEBUG_INFO
171
212
  end
172
213
 
214
+ # Debug information for SMTP
215
+ def debug_info_for_smtp
216
+ puts <<~DEBUG_INFO
217
+
218
+ ===================== FakePlugger::DeliveryMethod =====================
219
+
220
+ ------------------------------ Variables ------------------------------
221
+
222
+ ==> @delivery_settings: #{@delivery_settings.inspect}
223
+
224
+ ==> @default_delivery_system: #{@default_delivery_system.inspect}
225
+
226
+ ==> @message: #{@message.inspect}
227
+
228
+ ------------------------------- Methods -------------------------------
229
+
230
+ ==> delivery_system: #{delivery_system.inspect}
231
+
232
+ ==> settings: #{settings.inspect}
233
+
234
+ =======================================================================
235
+
236
+ DEBUG_INFO
237
+ end
238
+
239
+ # Prepare delivery. It depends on that is SMTP or API.
240
+ def prepare_delivery
241
+ if send_via_smtp?
242
+ @message.delivery_method :smtp, settings[:smtp_settings]
243
+ @message
244
+ else
245
+ client.new(delivery_data)
246
+ end
247
+ end
248
+
249
+ # Check that it should return with the delivery data.
250
+ def return_delivery_data?
251
+ !send_via_smtp? &&
252
+ @response.is_a?(Hash) &&
253
+ @response[:return_delivery_data]
254
+ end
255
+
256
+ # Return with a response which depends on the conditions.
257
+ def return_with_response
258
+ return prepare_delivery if @response.nil?
259
+
260
+ return delivery_data if return_delivery_data?
261
+
262
+ @response
263
+ end
264
+
265
+ # Show debug information from variables and methods.
266
+ def show_debug_info
267
+ send_via_smtp? ? debug_info_for_smtp : debug_info_for_api
268
+ end
269
+
173
270
  # Show raw message for debug purpose.
174
271
  def show_raw_message
175
272
  puts <<~RAW_MESSAGE
@@ -182,5 +279,18 @@ module FakePlugger
182
279
 
183
280
  RAW_MESSAGE
184
281
  end
282
+
283
+ # Extract settings values and update attributes.
284
+ def update_settings
285
+ # rubocop:disable Naming/MemoizedInstanceVariableName
286
+ @debug ||= settings[:fake_plugger_debug] || false
287
+
288
+ @raw_message ||= settings[:fake_plugger_raw_message] || false
289
+
290
+ @response ||= settings[:fake_plugger_response]
291
+
292
+ @use_mail_grabber ||= settings[:fake_plugger_use_mail_grabber] || false
293
+ # rubocop:enable Naming/MemoizedInstanceVariableName
294
+ end
185
295
  end
186
296
  end
@@ -30,24 +30,73 @@ module MailPlugger
30
30
  @delivery_settings = options[:delivery_settings] ||
31
31
  MailPlugger.delivery_settings
32
32
 
33
+ @delivery_systems = MailPlugger.delivery_systems
34
+
33
35
  @default_delivery_system = options[:default_delivery_system] ||
34
36
  default_delivery_system_get
35
37
 
36
38
  @message = nil
37
39
  end
38
40
 
41
+ # Using SMTP:
42
+ # Send message via SMTP protocol if the 'delivery_settings' contains a
43
+ # 'smtp_settings' key and the value is a hash with the settings.
44
+ #
45
+ # Using API:
39
46
  # Send message with the given client if the message parameter is a
40
- # Mail::Message object. Before doing that extract those information from the
47
+ # Mail::Message object. Before doing that, extract this information from the
41
48
  # Mail::Message object which was provided in the 'delivery_options'. After
42
49
  # that it generates a hash with these data and sends the message with the
43
50
  # provided client class which has a 'deliver' method.
44
51
  #
45
52
  # @param [Mail::Message] message what we would like to send
46
53
  #
47
- # @return [Mail::Message/Hash] depend on delivery_settings and method calls
54
+ # @return [Mail::Message/Hash] depends on delivery_settings and method calls
48
55
  #
49
56
  # @example
50
57
  #
58
+ # # Using SMTP:
59
+ #
60
+ # MailPlugger.plug_in('test_smtp_client') do |smtp|
61
+ # smtp.delivery_settings = {
62
+ # smtp_settings: {
63
+ # address: 'smtp.server.com',
64
+ # port: 587,
65
+ # domain: 'test.domain.com',
66
+ # enable_starttls_auto: true,
67
+ # user_name: 'test_user',
68
+ # password: '1234',
69
+ # authentication: :plain
70
+ # }
71
+ # }
72
+ # end
73
+ #
74
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
75
+ # subject: 'Test email', body: 'Test email body')
76
+ #
77
+ # MailPlugger::DeliveryMethod.new.deliver!(message)
78
+ #
79
+ # # or
80
+ #
81
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
82
+ # subject: 'Test email', body: 'Test email body')
83
+ #
84
+ # MailPlugger::DeliveryMethod.new(
85
+ # delivery_settings: {
86
+ # smtp_settings: {
87
+ # address: 'smtp.server.com',
88
+ # port: 587,
89
+ # domain: 'test.domain.com',
90
+ # enable_starttls_auto: true,
91
+ # user_name: 'test_user',
92
+ # password: '1234',
93
+ # authentication: :plain
94
+ # }
95
+ # }
96
+ # ).deliver!(message)
97
+ #
98
+ # # Using API:
99
+ #
51
100
  # MailPlugger.plug_in('test_api_client') do |api|
52
101
  # api.delivery_options = %i[from to subject body]
53
102
  # api.client = DefinedApiClientClass
@@ -76,7 +125,12 @@ module MailPlugger
76
125
 
77
126
  @message = message
78
127
 
79
- client.new(delivery_data).deliver
128
+ if send_via_smtp?
129
+ message.delivery_method :smtp, settings[:smtp_settings]
130
+ message.deliver!
131
+ else
132
+ client.new(delivery_data).deliver
133
+ end
80
134
  end
81
135
  end
82
136
  end
@@ -2,24 +2,25 @@
2
2
 
3
3
  module MailPlugger
4
4
  class Error < StandardError
5
- # Specific error class for errors if client is not given or has a wrong type
5
+ # Specific error class for errors if client is not given or has a wrong
6
+ # type.
6
7
  class WrongApiClient < Error; end
7
8
 
8
- # Specific error class for errors if delivery options is not given or
9
- # has a wrong type
9
+ # Specific error class for errors if delivery options are not given or
10
+ # has a wrong type.
10
11
  class WrongDeliveryOptions < Error; end
11
12
 
12
- # Specific error class for errors if delivery settings has a wrong type
13
+ # Specific error class for errors if delivery settings has a wrong type.
13
14
  class WrongDeliverySettings < Error; end
14
15
 
15
- # Specific error class for errors if delivery system is not given
16
+ # Specific error class for errors if delivery system is not given.
16
17
  class WrongDeliverySystem < Error; end
17
18
 
18
- # Specific error class for errors if parameter is not given
19
+ # Specific error class for errors if parameter is not given.
19
20
  class WrongParameter < Error; end
20
21
 
21
- # Specific error class for errors if tries to add undelclared option
22
- # in plug_in block
22
+ # Specific error class for errors if tries to add undeclared option
23
+ # in plug_in block.
23
24
  class WrongPlugInOption < Error; end
24
25
  end
25
26
  end
@@ -5,6 +5,15 @@ require 'mail/indifferent_hash'
5
5
 
6
6
  module MailPlugger
7
7
  module MailHelper
8
+ DELIVERY_SETTINGS_KEYS = %i[
9
+ fake_plugger_debug
10
+ fake_plugger_raw_message
11
+ fake_plugger_response
12
+ fake_plugger_use_mail_grabber
13
+ return_response
14
+ smtp_settings
15
+ ].freeze
16
+
8
17
  # Check the version of a gem.
9
18
  #
10
19
  # @param [String] gem_name the name of the gem
@@ -18,9 +27,9 @@ module MailPlugger
18
27
  requirement.satisfied_by?(current_version)
19
28
  end
20
29
 
21
- # Extract 'client'. If it's a hash then it'll return the right
22
- # client belongs to the delivery system. If it's not a hash it'll return
23
- # the given value. But if the value doesn't a class it'll raise an error.
30
+ # Extract 'client'. If it's a hash, then it'll return the right
31
+ # client belongs to the delivery system. If it's not a hash, it'll return
32
+ # the given value. But if the value doesn't a class, it'll raise an error.
24
33
  #
25
34
  # @return [Class] the defined API class
26
35
  def client
@@ -64,18 +73,22 @@ module MailPlugger
64
73
  end
65
74
 
66
75
  # Tries to set up a default delivery system, if the 'delivery_system'
67
- # wasn't defined in the Mail::Message object and 'delivery_options' and/or
68
- # 'client' is a hash. Which means the MailPlugger.plugin method was used,
69
- # probably.
76
+ # wasn't defined in the Mail::Message object, and we're using
77
+ # MailPlugger.plug_in then it returns with first key of the stored
78
+ # 'delivery_systems'.
79
+ # When we aren't using MailPlugger.plug_in method and 'delivery_options',
80
+ # 'client' and/or 'delivery_settings' is a hash, then it tries to get the
81
+ # 'delivery_system' from the hashes.
82
+ # Otherwise, it returns with nil.
70
83
  #
71
- # @return [Stirng] the first key of the 'delivery_options' or 'client'
84
+ # @return [Stirng/NilClass] the first key from the extracted keys or nil
72
85
  def default_delivery_system_get
73
86
  extract_keys&.first
74
87
  end
75
88
 
76
- # Extract 'delivery_options'. If it's a hash then it'll return the right
77
- # options belongs to the delivery system. If it's not a hash it'll return
78
- # the given value. But if the value doesn't an array it'll raise an error.
89
+ # Extract 'delivery_options'. If it's a hash, then it'll return the right
90
+ # options, belongs to the delivery system. If it's not a hash, it'll return
91
+ # the given value. But if the value doesn't an array, it'll raise an error.
79
92
  #
80
93
  # @return [Array] the options it'll collect from the Mail::Message object
81
94
  def delivery_options
@@ -90,8 +103,9 @@ module MailPlugger
90
103
  end
91
104
 
92
105
  # Extract 'delivery_system' from the Mail::Message object or if it's not
93
- # defined then use the default one. If it's still nil and one of the
94
- # 'delivery_options' or 'client' is a hash then raise error.
106
+ # defined, then use the default one. If it's still nil and one of the
107
+ # 'delivery_options', 'client' and/or 'delivery_settings' is a hash and
108
+ # 'delivery_settings' doesn't contain 'delivery_system' then raise error.
95
109
  #
96
110
  # @return [String] with the name of the delivery system
97
111
  def delivery_system
@@ -104,12 +118,13 @@ module MailPlugger
104
118
  @delivery_system
105
119
  end
106
120
 
107
- # Check the given 'delivery_options' or 'client' are hashes and
108
- # if one of that does then check the 'delivery_system' is valid or not.
109
- # If the given 'delivery_system' is nil or doesn't match with extracted keys
110
- # then it will raise error.
121
+ # Check the given 'delivery_options', 'client' and 'delivery_settings' are
122
+ # hashes and if one of that does then check the 'delivery_system' is valid
123
+ # or not.
124
+ # If the given 'delivery_system' is nil or doesn't match with extracted
125
+ # keys, then it will raise error.
111
126
  def delivery_system_value_check
112
- return unless @delivery_options.is_a?(Hash) || @client.is_a?(Hash)
127
+ return unless need_delivery_system?
113
128
 
114
129
  if @delivery_system.nil?
115
130
  raise Error::WrongDeliverySystem,
@@ -122,6 +137,17 @@ module MailPlugger
122
137
  "\"delivery_system\" '#{@delivery_system}' does not exist"
123
138
  end
124
139
 
140
+ # Check that 'delivery_settings' has 'delivery_system' key or not.
141
+ # If 'delivery_settings' contains 'DELIVERY_SETTINGS_KEYS' then it returns
142
+ # false, else true.
143
+ #
144
+ # @return [Boolean] true/false
145
+ def exclude_delivey_settings_keys?
146
+ @delivery_settings.keys.none? do |key|
147
+ DELIVERY_SETTINGS_KEYS.include?(key.to_sym)
148
+ end
149
+ end
150
+
125
151
  # Extract attachments.
126
152
  #
127
153
  # @return [Array] with extracted attachment hashes
@@ -137,19 +163,32 @@ module MailPlugger
137
163
  end
138
164
  end
139
165
 
140
- # Extract keys from 'delivery_options' or 'client', depends on which is a
141
- # hash. If none of these are hashes then returns nil.
166
+ # Return 'delivery_systems' array if it's exist. If not, then extract keys
167
+ # from 'delivery_options', 'client' or 'delivery_settings',
168
+ # depends on which is a hash. If none of these are hashes, then returns nil.
142
169
  #
143
- # @return [Array/NilClass] with the keys of 'delivery_options' or 'client'
170
+ # @return [Array/NilClass] with the keys or nil
144
171
  def extract_keys
172
+ return @delivery_systems unless @delivery_systems.nil?
173
+
174
+ extract_keys_from_other_variables
175
+ end
176
+
177
+ # Extract keys from 'delivery_options', 'client' or 'delivery_settings',
178
+ # depends on which is a hash. If none of these are hashes, then returns nil.
179
+ #
180
+ # @return [Array/NilClass] with the keys from one of the hash or nil
181
+ def extract_keys_from_other_variables
145
182
  if @delivery_options.is_a?(Hash)
146
183
  @delivery_options
147
184
  elsif @client.is_a?(Hash)
148
185
  @client
186
+ elsif @delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys?
187
+ @delivery_settings
149
188
  end&.keys
150
189
  end
151
190
 
152
- # How to Extract the (unparsed) value of the mail message fields.
191
+ # How to extract the (unparsed) value of the mail message fields.
153
192
  #
154
193
  # @return [String] version dependent method call
155
194
  def mail_field_value
@@ -174,6 +213,17 @@ module MailPlugger
174
213
  message_field.public_send(*mail_field_value)
175
214
  end
176
215
 
216
+ # Check if either 'deliviery_options' or 'client' is a hash.
217
+ # Or delivery_settings is a hash but not contains 'DELIVERY_SETTINGS_KEYS'
218
+ # in first level.
219
+ #
220
+ # @return [Boolean] true/false
221
+ def need_delivery_system?
222
+ @delivery_options.is_a?(Hash) ||
223
+ @client.is_a?(Hash) ||
224
+ (@delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys?)
225
+ end
226
+
177
227
  # Extract the value from the given options.
178
228
  #
179
229
  # @param [Hash/Array/Class] option
@@ -187,11 +237,22 @@ module MailPlugger
187
237
  end
188
238
  end
189
239
 
190
- # Extract 'settings'. If it's a hash then it'll return the right
191
- # settings belongs to the delivery system. If it's not a hash it'll return
192
- # the given value. But if the value doesn't a hash it'll raise an error.
240
+ # Check that settings contains any SMTP related settings.
241
+ #
242
+ # @return [Boolean] true/false
243
+ def send_via_smtp?
244
+ return true if settings[:smtp_settings].is_a?(Hash) &&
245
+ settings[:smtp_settings].any?
246
+
247
+ false
248
+ end
249
+
250
+ # Extract 'settings'. If it's a hash, then it'll return the right
251
+ # settings, belongs to the delivery system. If 'delivery_settings' is nil,
252
+ # it'll return an empty hash. But if the value doesn't a hash, it'll raise
253
+ # an error.
193
254
  #
194
- # @return [Hash] settings for Mail delivery_method
255
+ # @return [Hash] settings for Mail delivery_method and/or FakePlugger
195
256
  def settings
196
257
  @settings ||= option_value_from(@delivery_settings)
197
258
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MailPlugger
4
- VERSION = '1.3.0'
4
+ VERSION = '1.6.0'
5
5
  end
data/lib/mail_plugger.rb CHANGED
@@ -3,24 +3,45 @@
3
3
  require 'mail_plugger/error'
4
4
  require 'mail_plugger/mail_helper'
5
5
  require 'mail_plugger/delivery_method'
6
- # If we are using this gem outside of Rails then do not load this code.
6
+ # If we are using this gem outside of Rails, then do not load this code.
7
7
  require 'mail_plugger/railtie' if defined?(Rails)
8
8
  require 'mail_plugger/version'
9
9
 
10
10
  require 'fake_plugger/delivery_method'
11
- # If we are using this gem outside of Rails then do not load this code.
11
+ # If we are using this gem outside of Rails, then do not load this code.
12
12
  require 'fake_plugger/railtie' if defined?(Rails)
13
13
 
14
14
  module MailPlugger
15
15
  class << self
16
- attr_reader :client, :delivery_options, :delivery_settings
16
+ attr_reader :client,
17
+ :delivery_options,
18
+ :delivery_settings,
19
+ :delivery_systems
17
20
 
18
- # Plug in defined API(s) class.
21
+ # Plug in SMTP(s) or defined API(s) class.
19
22
  #
20
- # @param [String/Symbol] delivery_system the name of the API
23
+ # @param [String/Symbol] delivery_system the name of the SMTP/API
21
24
  #
22
25
  # @example using Rails `config/initializers/mail_plugger.rb`
23
26
  #
27
+ # # Using SMTP:
28
+ #
29
+ # MailPlugger.plug_in('smtp_client') do |smtp|
30
+ # smtp.delivery_settings = {
31
+ # smtp_settings: {
32
+ # address: 'smtp.server.com',
33
+ # port: 587,
34
+ # domain: 'test.domain.com',
35
+ # enable_starttls_auto: true,
36
+ # user_name: 'test_user',
37
+ # password: '1234',
38
+ # authentication: :plain
39
+ # }
40
+ # }
41
+ # end
42
+ #
43
+ # # Using API:
44
+ #
24
45
  # # The defined API class should have an 'initialize' and a 'deliver'
25
46
  # # method.
26
47
  # class DefinedApiClientClass
@@ -75,6 +96,7 @@ module MailPlugger
75
96
  check_value(delivery_system)
76
97
 
77
98
  @delivery_system = delivery_system
99
+ (@delivery_systems ||= []) << delivery_system
78
100
 
79
101
  yield self
80
102
  rescue NoMethodError => e
@@ -82,8 +104,8 @@ module MailPlugger
82
104
  end
83
105
 
84
106
  # Define 'client', 'delivery_options' and 'delivery_settings' setter
85
- # methods. These methods are generating a hash where the key is the
86
- # 'delivery_system'. This let us to set/use more than one API.
107
+ # methods. These methods are generating a hash, where the key is the
108
+ # 'delivery_system'. This let us set/use more than one STMP/API.
87
109
  %w[client delivery_options delivery_settings].each do |method|
88
110
  define_method "#{method}=" do |value|
89
111
  variable = instance_variable_get("@#{method}")
@@ -94,10 +116,10 @@ module MailPlugger
94
116
 
95
117
  private
96
118
 
97
- # Check 'delivery_system' is valid or not. If it's not valid then
119
+ # Check 'delivery_system' is valid or not. If it's not valid, then
98
120
  # it will raise an error.
99
121
  #
100
- # @param [String/Symbol] delivery_system the name of the API
122
+ # @param [String/Symbol] delivery_system the name of the SMTP/API
101
123
  def check_value(delivery_system)
102
124
  if delivery_system.nil?
103
125
  raise Error::WrongDeliverySystem, '"delivery_system" is nil'
@@ -110,7 +132,7 @@ module MailPlugger
110
132
  return if delivery_system.is_a?(String) || delivery_system.is_a?(Symbol)
111
133
 
112
134
  raise Error::WrongDeliverySystem, '"delivery_system" does not a ' \
113
- 'String or Symbol'
135
+ 'String or Symbol'
114
136
  end
115
137
  end
116
138
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_plugger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Norbert Szivós
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-14 00:00:00.000000000 Z
11
+ date: 2022-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -24,8 +24,22 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.5'
27
- description: Delivery Method to send emails via API(s). We can use this Delivery Method
28
- with Ruby on Rails ActionMailer or other solutions.
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-smtp
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ description: Delivery Method to send emails via SMTP(s) and API(s). We can use this
42
+ Delivery Method with Ruby on Rails ActionMailer or other solutions.
29
43
  email:
30
44
  - sysqa@yahoo.com
31
45
  executables: []
@@ -54,6 +68,7 @@ metadata:
54
68
  changelog_uri: https://github.com/MailToolbox/mail_plugger/blob/main/CHANGELOG.md
55
69
  bug_tracker_uri: https://github.com/MailToolbox/mail_plugger/issues
56
70
  documentation_uri: https://rubydoc.info/gems/mail_plugger
71
+ rubygems_mfa_required: 'true'
57
72
  post_install_message:
58
73
  rdoc_options: []
59
74
  require_paths:
@@ -62,15 +77,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
77
  requirements:
63
78
  - - ">="
64
79
  - !ruby/object:Gem::Version
65
- version: 2.5.0
80
+ version: 2.7.0
66
81
  required_rubygems_version: !ruby/object:Gem::Requirement
67
82
  requirements:
68
83
  - - ">="
69
84
  - !ruby/object:Gem::Version
70
85
  version: '0'
71
86
  requirements: []
72
- rubygems_version: 3.1.4
87
+ rubygems_version: 3.3.3
73
88
  signing_key:
74
89
  specification_version: 4
75
- summary: Plug in required mailer API(s) with MailPlugger.
90
+ summary: Plug in required mailer SMTP(s) and API(s) with MailPlugger.
76
91
  test_files: []