mail_plugger 1.0.1 → 1.4.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: b4dd3655061ce3563a5a47717b4450a24a85d402e9e1b87fc035584be6caf9fc
4
- data.tar.gz: 9dace2ebc237cef9590074e5b25b7a8878cbf3c7ba14af9bf612567fa370cb94
3
+ metadata.gz: a4e017acfdc5bd84be87de9bb9b213dbeaf5356436b896be22c2fdd8cf0f484e
4
+ data.tar.gz: 75dfedda8f0d97436469c5cbdb29b8316cb1e00a9a479168950b342870c4cf2b
5
5
  SHA512:
6
- metadata.gz: 21092888241af389665f0d5f03341cfed7d13bea5f3f753fedac3c3316e01ac6908bbebc262822f19b70fd14afd2bf485b3b45209ed8505ff1aa794f1473a526
7
- data.tar.gz: 713f8ba5dd65c40f965887dedb2e39618745aec161f69d1ca3a14f710a870d1f7517e617e75c700dcde7dca79ff71ac405ef6d26df328fef1432f0dac1de4485
6
+ metadata.gz: a7229b6a397f883683331bb341be7356061367362bfd8d8d2f3c82b46c9a774355c60ba85d643b19cf25284e6335c46f6bdcd06aa821613662bec7ff04fde833
7
+ data.tar.gz: dbe3dcd8e421074b5df9d1144672b1cebe852e3a06f46d6273505ca0833fe31f650bfe8e5d40ccc57cff17e698a75ac84af3821b2f5b07572bd559e70241eebb
data/CHANGELOG.md CHANGED
@@ -1,28 +1,94 @@
1
1
  # Change log
2
2
 
3
+ ## 1.4.0 (2021-06-08)
4
+
5
+ ### New features
6
+
7
+ * Add SMTP support. With this modifications we can use SMTP and API delivery methods as well.
8
+
9
+ ### Changes
10
+
11
+ * Update bundler and gems.
12
+
13
+
14
+ ## 1.3.0 (2021-04-14)
15
+
16
+ ### New features
17
+
18
+ * Add MailGrabber to FakePlugger. Now we can use FakePlugger and MailGrabber in same time.
19
+
20
+ ### Changes
21
+
22
+ * Remove unnecessary double quotes from `show_debug_info` method in `FakePlugger::DeliveryMethod` and fix documentations.
23
+ * Update bundler and gems.
24
+ * Update bug_report.md.
25
+
26
+
27
+ ## 1.2.0 (2021-03-18)
28
+
29
+ ### New organization
30
+
31
+ * Move mail_plugger repository into MailToolbox organization.
32
+
33
+ ### Changes
34
+
35
+ * Update gems.
36
+ * Fix some documentation issues.
37
+
38
+
39
+ ## 1.1.1 (2021-01-21)
40
+
41
+ ### Changes
42
+
43
+ * Change `FakePlugger::DeliveryMethod` to returns with the extracted delivery data instead of the message object.
44
+ * Update code documentations.
45
+ * Add missing FakePlugger description in the README.md.
46
+
47
+
48
+ ## 1.1.0 (2021-01-18)
49
+
50
+ ### New features
51
+
52
+ * Add FakePlugger to mock MailPlugger.
53
+
54
+ ### Changes
55
+
56
+ * Fix typos in the documentations.
57
+ * Add more metadata to gempspec file.
58
+
59
+
3
60
  ## 1.0.1 (2021-01-15)
4
61
 
62
+ ### Changes
63
+
5
64
  * Tidy up the gemspec file and change which files contains in the gem. Now the gem is much smaller.
6
65
  * Add a new mail plugger image which has a different canvas and use it in the README.md.
7
66
 
67
+
8
68
  ## 1.0.0 (2021-01-14)
9
69
 
70
+ ### Changes
71
+
10
72
  * Update gems.
11
73
  * Remove `webmock` gem to clean up unused things.
12
74
  * Update documentations.
13
75
 
76
+
14
77
  ## 1.0.0.rc1 (2021-01-13)
15
78
 
79
+ ### Changes
80
+
16
81
  * Change description and fix changelog_uri in gemspec file.
17
82
  * Add/Change documentations.
18
83
  * Update gems.
19
84
  * Change `MailPlugger.plug_in` and `MailPlugger::MailHelper.delivery_options` methods to accept strings and symbols.
20
- * Change `MailPlugger::MailHelper.delivery_data` method that we can retrieve massage object as well.
85
+ * Change `MailPlugger::MailHelper.delivery_data` method that we can retrieve message object as well.
21
86
  * Check hash keys in `MailPlugger::MailHelper.delivery_system` method, so now if we are add wrong `delivey_system` then we are getting a meaningful error message.
22
87
  * Change `MailPlugger::MailHelper.extract_attachments` method. If an attachment is inline then the hash contains the `filename` and `cid` as well.
23
88
  * Add/Change tests.
24
- * Change `MailPlugger::MailHelper.delivery_option` that returns with indifferent hash
89
+ * Change `MailPlugger::MailHelper.delivery_option` that returns with indifferent hash.
90
+
25
91
 
26
92
  ## 1.0.0.beta1 (2021-01-02)
27
93
 
28
- * Implement MailPlugger methods and functionality. See [README.md](https://github.com/norbertszivos/mail_plugger/blob/main/README.md)
94
+ * Implement MailPlugger methods and functionality. See [README.md](https://github.com/MailToolbox/mail_plugger/blob/main/README.md)
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
- # M<img src="https://raw.githubusercontent.com/norbertszivos/mail_plugger/master/images/mail_plugger800x500.png" height="22" />ilPlugger
1
+ # M<img src="https://raw.githubusercontent.com/MailToolbox/mail_plugger/main/images/mail_plugger800x500.png" height="22" />ilPlugger
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/mail_plugger.svg)](https://badge.fury.io/rb/mail_plugger)
4
- [![MIT license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE.txt)
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/norbertszivos/mail_plugger.svg?branch=main)](https://travis-ci.com/norbertszivos/mail_plugger)
7
- [![Maintainability](https://api.codeclimate.com/v1/badges/bd2cda43214c111d8d16/maintainability)](https://codeclimate.com/github/norbertszivos/mail_plugger/maintainability)
8
- [![Test Coverage](https://api.codeclimate.com/v1/badges/bd2cda43214c111d8d16/test_coverage)](https://codeclimate.com/github/norbertszivos/mail_plugger/test_coverage)
6
+ [![Build Status](https://travis-ci.com/MailToolbox/mail_plugger.svg?branch=main)](https://travis-ci.com/MailToolbox/mail_plugger)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/137881380fc475b4a836/maintainability)](https://codeclimate.com/github/MailToolbox/mail_plugger/maintainability)
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 balacing or cost management.
11
11
 
12
12
  ## Installation
13
13
 
@@ -27,20 +27,34 @@ Or install it yourself as:
27
27
 
28
28
  ## Usage
29
29
 
30
- - [How to use MailPlugger.plug_in method](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_plug_in_method.md)
31
- - [How to use MailPlugger::DeliveryMethod class](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_delivery_method.md)
32
- - [How to use MailPlugger in a Ruby script or IRB console](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_script_or_console.md)
33
- - [How to use MailPlugger in Ruby on Rails](https://github.com/norbertszivos/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/norbertszivos/mail_plugger/blob/main/docs/usage_of_secial_options_in_ruby_on_rails.md)
35
- - [How to add attachments to the mailer method in Ruby on Rails](https://github.com/norbertszivos/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/norbertszivos/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/norbertszivos/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/norbertszivos/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/norbertszivos/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/norbertszivos/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/norbertszivos/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/norbertszivos/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/norbertszivos/mail_plugger/blob/main/docs/usage_of_mailgun_in_ruby_on_rails.md)
30
+ - [How to use MailPlugger.plug_in method](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_plug_in_method.md)
31
+ - [How to use MailPlugger::DeliveryMethod class](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_delivery_method.md)
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
+ - [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 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)
46
+
47
+ # F<img src="https://raw.githubusercontent.com/MailToolbox/mail_plugger/main/images/fake_plugger800x500.png" height="22" />kePlugger
48
+
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 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`.
50
+
51
+ ## Usage
52
+
53
+ - [How to use FakePlugger::DeliveryMethod class](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_delivery_method.md)
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)
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)
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)
44
58
 
45
59
  ## Development
46
60
 
@@ -50,7 +64,7 @@ To install this gem onto your local machine, run `bundle exec rake install`.
50
64
 
51
65
  To release a new version:
52
66
 
53
- - Update CHANGELOG.md
67
+ - Update [CHANGELOG.md](https://github.com/MailToolbox/mail_plugger/blob/main/CHANGELOG.md)
54
68
  - Update the version number in `version.rb` manually or use `gem-release` gem and run `gem bump -v major|minor|patch|rc|beta`.
55
69
  - Build gem with `bundle exec rake build`.
56
70
  - Run `bundle install` and `bundle exec appraisal install` to update gemfiles and commit the changes.
@@ -58,7 +72,7 @@ To release a new version:
58
72
 
59
73
  ## Contributing
60
74
 
61
- Bug reports and pull requests are welcome. Please read [CONTRIBUTING.md](https://github.com/norbertszivos/mail_plugger/blob/main/CONTRIBUTING.md) if you would like to contribute to this project.
75
+ Bug reports and pull requests are welcome. Please read [CONTRIBUTING.md](https://github.com/MailToolbox/mail_plugger/blob/main/CONTRIBUTING.md) if you would like to contribute to this project.
62
76
 
63
77
  ## Inspiration
64
78
 
@@ -69,4 +83,4 @@ Bug reports and pull requests are welcome. Please read [CONTRIBUTING.md](https:/
69
83
 
70
84
  ## License
71
85
 
72
- The gem is available as open source under the terms of the [MIT License](https://github.com/norbertszivos/mail_plugger/blob/main/LICENSE.txt).
86
+ The gem is available as open source under the terms of the [MIT License](https://github.com/MailToolbox/mail_plugger/blob/main/LICENSE.txt).
@@ -0,0 +1,296 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mail_grabber' if Gem.loaded_specs.key?('mail_grabber')
4
+
5
+ module FakePlugger
6
+ class DeliveryMethod < MailPlugger::DeliveryMethod
7
+ # Initialize FakePlugger delivery method attributes. If we are using
8
+ # MailPlugger.plug_in method, then these attributes can be nil, if not then
9
+ # we should set these attributes.
10
+ #
11
+ # @param [Hash] options check options below
12
+ # @option options [Class/Hash] client
13
+ # e.g. DefinedApiClientClass or { 'key' => DefinedApiClientClass }
14
+ #
15
+ # @option options [Array/Hash] delivery_options
16
+ # e.g. [:to, :from, :subject, :body] or
17
+ # { 'key' => [:to, :from, :subject, :body] }
18
+ #
19
+ # @option options [Hash] delivery_settings
20
+ # e.g. { return_response: true }
21
+ #
22
+ # @option options [String/Symbol] default_delivery_system
23
+ # e.g. 'defined_api'
24
+ #
25
+ # @option options [Boolean] debug
26
+ # if true it will show debug informations
27
+ #
28
+ # @option options [Boolean] raw_message
29
+ # if true it will show raw message
30
+ #
31
+ # @option options [String/Symbol/Array/Hash] response
32
+ # the deliver! method will return with this value or if this value is nil
33
+ # then it will return with the client object
34
+ #
35
+ # @option options [Boolean] use_mail_grabber
36
+ # if true it will store the message in a database which MailGrabber can
37
+ # read
38
+ def initialize(options = {})
39
+ super
40
+
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
+ end
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:
55
+ # Mock send message with the given client if the message parameter is a
56
+ # Mail::Message object. If 'response' parameter is nil then it will extract
57
+ # those information from the Mail::Message object which was provided in the
58
+ # 'delivery_options'. After that it generates a hash with these data and
59
+ # returns with the provided client class which has a 'deliver' method, but
60
+ # it won't call the 'deliver' method.
61
+ # If the 'response' parameter is a hash with 'return_delivery_data: true'
62
+ # then it will retrun with the extracted delivery data.
63
+ #
64
+ #
65
+ # If the 'response' parameter is not nil then retruns with that given data
66
+ # without call any other methods.
67
+ # Except if 'debug' is true. In this case it will call those methods which
68
+ # is calling in normal operation as well.
69
+ # If 'debug' is true then it prints out some debug informations.
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.
72
+ #
73
+ # @param [Mail::Message] message what we would like to send
74
+ #
75
+ # @return [Mail::Message/Hash] depends on the given value
76
+ #
77
+ # @example
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
+ #
121
+ # MailPlugger.plug_in('test_api_client') do |api|
122
+ # api.delivery_options = %i[from to subject body]
123
+ # api.delivery_settings = {
124
+ # fake_plugger_debug: true,
125
+ # fake_plugger_raw_message: true,
126
+ # fake_plugger_use_mail_grabber: true,
127
+ # fake_plugger_response: { response: 'OK' }
128
+ # }
129
+ # api.client = DefinedApiClientClass
130
+ # end
131
+ #
132
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
133
+ # subject: 'Test email', body: 'Test email body')
134
+ #
135
+ # FakePlugger::DeliveryMethod.new.deliver!(message)
136
+ #
137
+ # # or
138
+ #
139
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
140
+ # subject: 'Test email', body: 'Test email body')
141
+ #
142
+ # FakePlugger::DeliveryMethod.new(
143
+ # delivery_options: %i[from to subject body],
144
+ # client: DefinedApiClientClass,
145
+ # debug: true,
146
+ # raw_message: true,
147
+ # use_mail_grabber: true,
148
+ # response: { response: 'OK' }
149
+ # ).deliver!(message)
150
+ #
151
+ def deliver!(message)
152
+ unless message.is_a?(Mail::Message)
153
+ raise MailPlugger::Error::WrongParameter,
154
+ 'The given parameter is not a Mail::Message'
155
+ end
156
+
157
+ @message = message
158
+
159
+ update_settings
160
+
161
+ call_extra_options
162
+
163
+ return_with_response
164
+ end
165
+
166
+ private
167
+
168
+ # Call extra options like show debug informations, show raw message,
169
+ # use mail grabber.
170
+ def call_extra_options
171
+ show_debug_info if @debug
172
+ show_raw_message if @raw_message
173
+
174
+ return unless Gem.loaded_specs.key?('mail_grabber') && @use_mail_grabber
175
+
176
+ MailGrabber::DeliveryMethod.new.deliver!(@message)
177
+ end
178
+
179
+ # Debug informations for API
180
+ def debug_info_for_api
181
+ puts <<~DEBUG_INFO
182
+
183
+ ===================== FakePlugger::DeliveryMethod =====================
184
+
185
+ ------------------------------ Variables ------------------------------
186
+
187
+ ==> @client: #{@client.inspect}
188
+
189
+ ==> @delivery_options: #{@delivery_options.inspect}
190
+
191
+ ==> @delivery_settings: #{@delivery_settings.inspect}
192
+
193
+ ==> @default_delivery_system: #{@default_delivery_system.inspect}
194
+
195
+ ==> @message: #{@message.inspect}
196
+
197
+ ------------------------------- Methods -------------------------------
198
+
199
+ ==> client: #{client.inspect}
200
+
201
+ ==> delivery_system: #{delivery_system.inspect}
202
+
203
+ ==> delivery_options: #{delivery_options.inspect}
204
+
205
+ ==> delivery_data: #{delivery_data.inspect}
206
+
207
+ ==> settings: #{settings.inspect}
208
+
209
+ =======================================================================
210
+
211
+ DEBUG_INFO
212
+ end
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 retrun 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 informations from variables and methods.
266
+ def show_debug_info
267
+ send_via_smtp? ? debug_info_for_smtp : debug_info_for_api
268
+ end
269
+
270
+ # Show raw message for debug purpose.
271
+ def show_raw_message
272
+ puts <<~RAW_MESSAGE
273
+
274
+ ============================ Mail::Message ============================
275
+
276
+ #{@message}
277
+
278
+ =======================================================================
279
+
280
+ RAW_MESSAGE
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
295
+ end
296
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FakePlugger
4
+ class Railtie < Rails::Railtie
5
+ initializer 'fake_plugger.add_delivery_method' do
6
+ ActiveSupport.on_load :action_mailer do
7
+ ActionMailer::Base.add_delivery_method(
8
+ :fake_plugger,
9
+ FakePlugger::DeliveryMethod
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/mail_plugger.rb CHANGED
@@ -7,17 +7,42 @@ require 'mail_plugger/delivery_method'
7
7
  require 'mail_plugger/railtie' if defined?(Rails)
8
8
  require 'mail_plugger/version'
9
9
 
10
+ require 'fake_plugger/delivery_method'
11
+ # If we are using this gem outside of Rails then do not load this code.
12
+ require 'fake_plugger/railtie' if defined?(Rails)
13
+
10
14
  module MailPlugger
11
15
  class << self
12
- attr_reader :client, :delivery_options, :delivery_settings
16
+ attr_reader :client,
17
+ :delivery_options,
18
+ :delivery_settings,
19
+ :delivery_systems
13
20
 
14
- # Plug in defined API(s) class.
21
+ # Plug in SMTP(s) or defined API(s) class.
15
22
  #
16
- # @param [String/Symbol] delivery_system the name of the API
23
+ # @param [String/Symbol] delivery_system the name of the SMTP/API
17
24
  #
18
25
  # @example using Rails `config/initializers/mail_plugger.rb`
19
26
  #
20
- # # The defined API class should have an 'initializer' and a 'deliver'
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
+ #
45
+ # # The defined API class should have an 'initialize' and a 'deliver'
21
46
  # # method.
22
47
  # class DefinedApiClientClass
23
48
  # def initialize(options = {})
@@ -71,6 +96,7 @@ module MailPlugger
71
96
  check_value(delivery_system)
72
97
 
73
98
  @delivery_system = delivery_system
99
+ (@delivery_systems ||= []) << delivery_system
74
100
 
75
101
  yield self
76
102
  rescue NoMethodError => e
@@ -79,7 +105,7 @@ module MailPlugger
79
105
 
80
106
  # Define 'client', 'delivery_options' and 'delivery_settings' setter
81
107
  # methods. These methods are generating a hash where the key is the
82
- # 'delivery_system'. This let us to set/use more than one API.
108
+ # 'delivery_system'. This let us to set/use more than one STMP/API.
83
109
  %w[client delivery_options delivery_settings].each do |method|
84
110
  define_method "#{method}=" do |value|
85
111
  variable = instance_variable_get("@#{method}")
@@ -90,6 +116,10 @@ module MailPlugger
90
116
 
91
117
  private
92
118
 
119
+ # Check 'delivery_system' is valid or not. If it's not valid then
120
+ # it will raise an error.
121
+ #
122
+ # @param [String/Symbol] delivery_system the name of the SMTP/API
93
123
  def check_value(delivery_system)
94
124
  if delivery_system.nil?
95
125
  raise Error::WrongDeliverySystem, '"delivery_system" is nil'
@@ -30,12 +30,19 @@ 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
47
  # Mail::Message object. Before doing that extract those information from the
41
48
  # Mail::Message object which was provided in the 'delivery_options'. After
@@ -44,10 +51,52 @@ module MailPlugger
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
@@ -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
@@ -64,11 +73,15 @@ 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 are'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
@@ -91,7 +104,8 @@ module MailPlugger
91
104
 
92
105
  # Extract 'delivery_system' from the Mail::Message object or if it's not
93
106
  # 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.
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.
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.
109
124
  # If the given 'delivery_system' is nil or doesn't match with extracted keys
110
125
  # 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 retruns
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,15 +163,28 @@ 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
 
@@ -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
 
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
+
190
250
  # 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.
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.0.1'
4
+ VERSION = '1.4.0'
5
5
  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.0.1
4
+ version: 1.4.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-01-15 00:00:00.000000000 Z
11
+ date: 2021-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -24,8 +24,8 @@ 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
+ description: Delivery Method to send emails via SMTP(s) and API(s). We can use this
28
+ Delivery Method with Ruby on Rails ActionMailer or other solutions.
29
29
  email:
30
30
  - sysqa@yahoo.com
31
31
  executables: []
@@ -37,19 +37,23 @@ files:
37
37
  - CHANGELOG.md
38
38
  - LICENSE.txt
39
39
  - README.md
40
+ - lib/fake_plugger/delivery_method.rb
41
+ - lib/fake_plugger/railtie.rb
40
42
  - lib/mail_plugger.rb
41
43
  - lib/mail_plugger/delivery_method.rb
42
44
  - lib/mail_plugger/error.rb
43
45
  - lib/mail_plugger/mail_helper.rb
44
46
  - lib/mail_plugger/railtie.rb
45
47
  - lib/mail_plugger/version.rb
46
- homepage: https://github.com/norbertszivos/mail_plugger
48
+ homepage: https://github.com/MailToolbox/mail_plugger
47
49
  licenses:
48
50
  - MIT
49
51
  metadata:
50
- homepage_uri: https://github.com/norbertszivos/mail_plugger
51
- source_code_uri: https://github.com/norbertszivos/mail_plugger
52
- changelog_uri: https://github.com/norbertszivos/mail_plugger/blob/main/CHANGELOG.md
52
+ homepage_uri: https://github.com/MailToolbox/mail_plugger
53
+ source_code_uri: https://github.com/MailToolbox/mail_plugger
54
+ changelog_uri: https://github.com/MailToolbox/mail_plugger/blob/main/CHANGELOG.md
55
+ bug_tracker_uri: https://github.com/MailToolbox/mail_plugger/issues
56
+ documentation_uri: https://rubydoc.info/gems/mail_plugger
53
57
  post_install_message:
54
58
  rdoc_options: []
55
59
  require_paths:
@@ -68,5 +72,5 @@ requirements: []
68
72
  rubygems_version: 3.1.4
69
73
  signing_key:
70
74
  specification_version: 4
71
- summary: Plug in the required mailer API(s) with MailPlugger.
75
+ summary: Plug in required mailer SMTP(s) and API(s) with MailPlugger.
72
76
  test_files: []