mail_plugger 1.0.0.beta1 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ # How to use MailPlugger::DeliveryMethod class
2
+
3
+ With this class it can extract data from the Mail::Message object and send message based on the given configurations. We can add these options directly in the `new` method or we can use `MailPlugger.plug_in` method as well.
4
+
5
+ The `new` method parameter is a Hash where the keys are Symbols.
6
+
7
+ Hash parameters:
8
+ - `client` which should be a Class (It can be a Hash with this Class as well. In this case the key of the Hash is the `delivery_system` from the `Mail::Message` object or the `default_delivery_system`). This Class is a special class which generates the data and calls the API to send the message.
9
+ - `delivery_options` which should be an Array with Symbols or Strings (It can be a Hash with this Array as well. In this case the key of the Hash is the `delivery_system` from the `Mail::Message` object or the `default_delivery_system`). It will search these options in the `Mail::Message` object like `from`, `to`, `cc`, `bcc`, `subject`, `body`, `text_part`, `html_part`, `attachments` or anything what we will add to this object. Also we can retrieve the `Mail::Message` object with `message_obj`.
10
+ - `delivery_settings` which should be a Hash. The Mail gem can use these settings like `{ return_response: true }` (The keys are should be Symbols).
11
+ - `default_delivery_system` which should be a String or Symbol. This option is needed when `delivery_options` and `client` are Hashes. When `delivery_system` in the `Mail::Message` object is not defined then the `default_delivery_system` value is the key of those Hashes. When `default_delivery_system` is not defined then `default_delivery_system_get` will return with the first key of `delivery_options` or `client` Hash.
12
+
13
+ Examples:
14
+
15
+ ```ruby
16
+ # NOTE: This is just an example for testing...
17
+ class TestApiClientClass
18
+ def initialize(options = {})
19
+ @settings = { api_key: '12345' }
20
+ @options = options
21
+ end
22
+
23
+ def deliver
24
+ # e.g. API.new(@settings).client.post(generate_mail_hash)
25
+ puts " >>> settings: #{@settings.inspect}"
26
+ puts " >>> options: #{@options.inspect}"
27
+ puts " >>> generate_mail_hash: #{generate_mail_hash.inspect}"
28
+ { response: 'OK' }
29
+ end
30
+
31
+ private
32
+
33
+ def generate_mail_hash
34
+ {
35
+ to: generate_recipients,
36
+ from: {
37
+ email: @options[:from].first
38
+ },
39
+ subject: @options[:subject],
40
+ content: [
41
+ {
42
+ type: 'text/plain',
43
+ value: @options[:body]
44
+ }
45
+ ]
46
+ }
47
+ end
48
+
49
+ def generate_recipients
50
+ @options[:to].map do |to|
51
+ {
52
+ email: to
53
+ }
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ We can add simple options to `MailPlugger::DeliveryMethod`.
60
+
61
+ ```ruby
62
+ message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
63
+
64
+ MailPlugger::DeliveryMethod.new(delivery_options: %i[from to subject body], client: TestApiClientClass).deliver!(message)
65
+ ```
66
+
67
+ Or we can add these options in Hash and set `default_delivery_system`.
68
+
69
+ ```ruby
70
+ message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
71
+
72
+ MailPlugger::DeliveryMethod.new(delivery_options: { 'test_api_client' => %i[from to subject body] }, client: { 'test_api_client' => TestApiClientClass }, default_delivery_system: 'test_api_client').deliver!(message)
73
+ ```
74
+
75
+ Add `delivery_system` in the `Mail::Message` object (it will search this value in the given Hash). The `delivery_system` type in the `Mail::Message` object should match with the given key type of the Hash (if `delivery_system` is String then Hash key should String as well).
76
+
77
+ ```ruby
78
+ message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body', delivery_system: 'test_api_client')
79
+
80
+ MailPlugger::DeliveryMethod.new(delivery_options: { 'test_api_client' => %i[from to subject body] }, client: { 'test_api_client' => TestApiClientClass }).deliver!(message)
81
+ ```
82
+
83
+ If we are not adding `delivery_system` anywhere then it will use the first key of the Hash.
84
+
85
+ ```ruby
86
+ message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
87
+
88
+ MailPlugger::DeliveryMethod.new(delivery_options: { 'test_api_client' => %i[from to subject body] }, client: { 'test_api_client' => TestApiClientClass }).deliver!(message)
89
+ ```
90
+
91
+ We can use `MailPlugger.plug_in` to add our configurations.
92
+
93
+ ```ruby
94
+ MailPlugger.plug_in('test_api_client') do |api|
95
+ api.delivery_options = %i[from to subject body]
96
+ api.client = TestApiClientClass
97
+ end
98
+
99
+ message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
100
+
101
+ MailPlugger::DeliveryMethod.new.deliver!(message)
102
+ ```
103
+
104
+ If we are using `mail_plugger` gem in Ruby on Rails we don't have to do anything with this class. Rails will load this method automatically. Basically we should use `MailPlugger.plug_in` method to configure this delivery method.
@@ -0,0 +1,58 @@
1
+ # How to use Mailgun with MailPlugger in Ruby on Rails
2
+
3
+ **Please note that these examples were not tested, but I believe it should work.**
4
+
5
+ Let's use mailer method which was defined [here](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md).
6
+
7
+ Add `mailgun-ruby` to the `Gemfile`.
8
+
9
+ ```ruby
10
+ gem 'mailgun-ruby'
11
+ ```
12
+
13
+ Then run `bundle install` command to deploy the gem.
14
+
15
+ Change the API and `MailPlugger.plug_in` method in `config/initializers/mail_plugger.rb`.
16
+
17
+ ```ruby
18
+ class MailgunApiClient
19
+ def initialize(options = {})
20
+ @settings = { api_key: ENV['MAILGUN_API_KEY'] }
21
+ @options = options
22
+ end
23
+
24
+ def deliver
25
+ Mailgun::Client.new(@settings[:api_key]).send_message('sending_domain.com', generate_mail_hash)
26
+ end
27
+
28
+ private
29
+
30
+ def generate_mail_hash
31
+ {
32
+ from: @options[:from].first,
33
+ to: @options[:to].join(','),
34
+ subject: @options[:subject],
35
+ text: @options[:text_part],
36
+ html: @options[:html_part]
37
+ }
38
+ end
39
+ end
40
+
41
+ MailPlugger.plug_in('mailgun') do |api|
42
+ api.delivery_options = %i[from to subject text_part html_part]
43
+ api.delivery_settings = { return_response: true }
44
+ api.client = MailgunApiClient
45
+ end
46
+ ```
47
+
48
+ Then modify the mailer method a little bit.
49
+
50
+ ```ruby
51
+ class TestMailer < ApplicationMailer
52
+ default from: 'from@example.com'
53
+
54
+ def send_test
55
+ mail subject: 'Test email', to: 'to@example.com', delivery_system: 'mailgun'
56
+ end
57
+ end
58
+ ```
@@ -0,0 +1,104 @@
1
+ # How to use Mandrill with MailPlugger in Ruby on Rails
2
+
3
+ **Please note that these examples were not tested, but I believe it should work.**
4
+
5
+ Let's use mailer method which was defined [here](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md).
6
+
7
+ Add `mandrill-api-json` to the `Gemfile`.
8
+
9
+ ```ruby
10
+ gem 'mandrill-api-json'
11
+ ```
12
+
13
+ Then run `bundle install` command to deploy the gem.
14
+
15
+ ## Send
16
+
17
+ Change the API and `MailPlugger.plug_in` method in `config/initializers/mail_plugger.rb`.
18
+
19
+ ```ruby
20
+ class MandrillApiClient
21
+ def initialize(options = {})
22
+ @settings = { api_key: ENV['MANDRILL_API_KEY'] }
23
+ @options = options
24
+ end
25
+
26
+ def deliver
27
+ Mandrill::API.new(@settings[:api_key]).messages.send(generate_mail_hash)
28
+ end
29
+
30
+ private
31
+
32
+ def generate_mail_hash
33
+ {
34
+ from_email: @options[:from].first,
35
+ to: generate_recipients,
36
+ subject: @options[:subject],
37
+ text: @options[:text_part],
38
+ html: @options[:html_part],
39
+ tags: [@options[:tag]]
40
+ }
41
+ end
42
+
43
+ def generate_recipients
44
+ @options[:to].map do |to|
45
+ {
46
+ email: to
47
+ }
48
+ end
49
+ end
50
+ end
51
+
52
+ MailPlugger.plug_in('mandrill') do |api|
53
+ api.delivery_options = %i[from to subject text_part html_part tag]
54
+ api.delivery_settings = { return_response: true }
55
+ api.client = MandrillApiClient
56
+ end
57
+ ```
58
+
59
+ Then modify the mailer method a little bit.
60
+
61
+ ```ruby
62
+ class TestMailer < ApplicationMailer
63
+ default from: 'from@example.com'
64
+
65
+ def send_test
66
+ mail subject: 'Test email', to: 'to@example.com', delivery_system: 'mandrill', tag: 'send_test'
67
+ end
68
+ end
69
+ ```
70
+
71
+ ## Send Raw
72
+
73
+ Change the API and `MailPlugger.plug_in` method in `config/initializers/mail_plugger.rb`.
74
+
75
+ ```ruby
76
+ class MandrillApiClient
77
+ def initialize(options = {})
78
+ @settings = { api_key: ENV['MANDRILL_API_KEY'] }
79
+ @options = options
80
+ end
81
+
82
+ def deliver
83
+ Mandrill::API.new(@settings[:api_key]).messages.send_raw(@options[:message_obj].to_s)
84
+ end
85
+ end
86
+
87
+ MailPlugger.plug_in('mandrill') do |api|
88
+ api.delivery_options = %i[message_obj]
89
+ api.delivery_settings = { return_response: true }
90
+ api.client = MandrillApiClient
91
+ end
92
+ ```
93
+
94
+ Then modify the mailer method a little bit.
95
+
96
+ ```ruby
97
+ class TestMailer < ApplicationMailer
98
+ default from: 'from@example.com'
99
+
100
+ def send_test
101
+ mail subject: 'Test email', to: 'to@example.com', delivery_system: 'mandrill'
102
+ end
103
+ end
104
+ ```
@@ -0,0 +1,332 @@
1
+ # How to use more delivey systems in Ruby on Rails
2
+
3
+ Let's modify the configuration which was defined [here](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md).
4
+
5
+ Add a new API Class in `config/initializers/mail_plugger.rb` (you can define it in another place just now it is esasier to do this).
6
+
7
+ ```ruby
8
+ # NOTE: This is just an example for testing...
9
+ class TestApiClientClass
10
+ def initialize(options = {})
11
+ @settings = { api_key: '12345' }
12
+ @options = options
13
+ end
14
+
15
+ def deliver
16
+ # e.g. API.new(@settings).client.post(generate_mail_hash)
17
+ puts " >>> settings: #{@settings.inspect}"
18
+ puts " >>> options: #{@options.inspect}"
19
+ puts " >>> generate_mail_hash: #{generate_mail_hash.inspect}"
20
+ { response: 'Message sent via API' }
21
+ end
22
+
23
+ private
24
+
25
+ def generate_mail_hash
26
+ {
27
+ to: generate_recipients,
28
+ from: {
29
+ email: @options[:from].first
30
+ },
31
+ subject: @options[:subject],
32
+ content: [
33
+ {
34
+ type: 'text/plain',
35
+ value: @options[:text_part]
36
+ },
37
+ {
38
+ type: 'text/html; charset=UTF-8',
39
+ value: @options[:html_part]
40
+ }
41
+ ]
42
+ }
43
+ end
44
+
45
+ def generate_recipients
46
+ @options[:to].map do |to|
47
+ {
48
+ email: to
49
+ }
50
+ end
51
+ end
52
+ end
53
+
54
+ class TestApi2ClientClass
55
+ def initialize(options = {})
56
+ @settings = { api_key: '54321' }
57
+ @options = options
58
+ end
59
+
60
+ def deliver
61
+ # e.g. API.new(@settings).client.post(generate_mail_hash)
62
+ puts " >>> settings: #{@settings.inspect}"
63
+ puts " >>> options: #{@options.inspect}"
64
+ puts " >>> generate_mail_hash: #{generate_mail_hash.inspect}"
65
+ { response: 'Message sent via API2' }
66
+ end
67
+
68
+ private
69
+
70
+ def generate_mail_hash
71
+ {
72
+ to: generate_recipients,
73
+ from: {
74
+ email: @options[:from].first
75
+ },
76
+ subject: @options[:subject],
77
+ content: [
78
+ {
79
+ type: 'text/plain',
80
+ value: @options[:text_part]
81
+ },
82
+ {
83
+ type: 'text/html; charset=UTF-8',
84
+ value: @options[:html_part]
85
+ }
86
+ ]
87
+ }
88
+ end
89
+
90
+ def generate_recipients
91
+ @options[:to].map do |to|
92
+ {
93
+ email: to
94
+ }
95
+ end
96
+ end
97
+ end
98
+
99
+ MailPlugger.plug_in('test_api_client') do |api|
100
+ api.delivery_options = %i[from to subject text_part html_part]
101
+ api.delivery_settings = { return_response: true }
102
+ api.client = TestApiClientClass
103
+ end
104
+
105
+ MailPlugger.plug_in('test_api2_client') do |api|
106
+ api.delivery_options = %i[from to subject text_part html_part]
107
+ api.delivery_settings = { return_response: true }
108
+ api.client = TestApi2ClientClass
109
+ end
110
+ ```
111
+
112
+ Then change `app/mailers/test_mailer.rb` file.
113
+
114
+ ```ruby
115
+ class TestMailer < ApplicationMailer
116
+ default from: 'from@example.com'
117
+
118
+ def send_test
119
+ mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api_client'
120
+ end
121
+
122
+ def send_test2
123
+ mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api2_client'
124
+ end
125
+ end
126
+ ```
127
+
128
+ Then we should add views of the second mailer method, so create `app/views/test_mailer/send_test2.html.erb`
129
+
130
+ ```erb
131
+ <p>Test email body</p>
132
+ ```
133
+
134
+ and `app/views/test_mailer/send_test2.text.erb`.
135
+
136
+ ```erb
137
+ Test email body
138
+ ```
139
+
140
+ In the `rails console` we can try it out.
141
+
142
+ ```ruby
143
+ TestMailer.send_test.deliver_now!
144
+ # Rendering test_mailer/send_test.html.erb within layouts/mailer
145
+ # Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
146
+ # Rendering test_mailer/send_test.text.erb within layouts/mailer
147
+ # Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
148
+ #TestMailer#send_test: processed outbound mail in 46.1ms
149
+ # >>> settings: {:api_key=>"12345"}
150
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
151
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
152
+ #=> {:response=>"Message sent via API"}
153
+
154
+ TestMailer.send_test2.deliver_now!
155
+ # Rendering test_mailer/send_test2.html.erb within layouts/mailer
156
+ # Rendered test_mailer/send_test2.html.erb within layouts/mailer (2.6ms)
157
+ # Rendering test_mailer/send_test2.text.erb within layouts/mailer
158
+ # Rendered test_mailer/send_test2.text.erb within layouts/mailer (0.4ms)
159
+ #TestMailer#send_test2: processed outbound mail in 33.9ms
160
+ # >>> settings: {:api_key=>"54321"}
161
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
162
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
163
+ #=> {:response=>"Message sent via API2"}
164
+ ```
165
+
166
+ In the `app/mailers/test_mailer.rb` file we can use the Rails default option as well to define `delivery_system`.
167
+
168
+ ```ruby
169
+ class TestMailer < ApplicationMailer
170
+ default from: 'from@example.com', delivery_system: 'test_api_client'
171
+
172
+ def send_test
173
+ mail subject: 'Test email', to: 'to@example.com'
174
+ end
175
+
176
+ def send_test2
177
+ mail subject: 'Test email', to: 'to@example.com'
178
+ end
179
+ end
180
+ ```
181
+
182
+ In the `rails console` we can try it out.
183
+
184
+ ```ruby
185
+ TestMailer.send_test.deliver_now!
186
+ # Rendering test_mailer/send_test.html.erb within layouts/mailer
187
+ # Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
188
+ # Rendering test_mailer/send_test.text.erb within layouts/mailer
189
+ # Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
190
+ #TestMailer#send_test: processed outbound mail in 46.1ms
191
+ # >>> settings: {:api_key=>"12345"}
192
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
193
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
194
+ #=> {:response=>"Message sent via API"}
195
+
196
+ TestMailer.send_test2.deliver_now!
197
+ # Rendering test_mailer/send_test.html.erb within layouts/mailer
198
+ # Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
199
+ # Rendering test_mailer/send_test.text.erb within layouts/mailer
200
+ # Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
201
+ #TestMailer#send_test: processed outbound mail in 46.1ms
202
+ # >>> settings: {:api_key=>"12345"}
203
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
204
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
205
+ #=> {:response=>"Message sent via API"}
206
+ ```
207
+
208
+ Or we can use default, but override it in the method.
209
+
210
+ ```ruby
211
+ class TestMailer < ApplicationMailer
212
+ default from: 'from@example.com', delivery_system: 'test_api_client'
213
+
214
+ def send_test
215
+ mail subject: 'Test email', to: 'to@example.com'
216
+ end
217
+
218
+ def send_test2
219
+ mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api2_client'
220
+ end
221
+ end
222
+ ```
223
+
224
+ In the `rails console` we can try it out.
225
+
226
+ ```ruby
227
+ TestMailer.send_test.deliver_now!
228
+ # Rendering test_mailer/send_test.html.erb within layouts/mailer
229
+ # Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
230
+ # Rendering test_mailer/send_test.text.erb within layouts/mailer
231
+ # Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
232
+ #TestMailer#send_test: processed outbound mail in 46.1ms
233
+ # >>> settings: {:api_key=>"12345"}
234
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
235
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
236
+ #=> {:response=>"Message sent via API"}
237
+
238
+ TestMailer.send_test2.deliver_now!
239
+ # Rendering test_mailer/send_test2.html.erb within layouts/mailer
240
+ # Rendered test_mailer/send_test2.html.erb within layouts/mailer (2.6ms)
241
+ # Rendering test_mailer/send_test2.text.erb within layouts/mailer
242
+ # Rendered test_mailer/send_test2.text.erb within layouts/mailer (0.4ms)
243
+ #TestMailer#send_test2: processed outbound mail in 33.9ms
244
+ # >>> settings: {:api_key=>"54321"}
245
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
246
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
247
+ #=> {:response=>"Message sent via API2"}
248
+ ```
249
+
250
+ Or if we are not define any `delivey_system` then it will use the first defined one with `MailPlugger.plug_in` method.
251
+
252
+ ```ruby
253
+ class TestMailer < ApplicationMailer
254
+ default from: 'from@example.com'
255
+
256
+ def send_test
257
+ mail subject: 'Test email', to: 'to@example.com'
258
+ end
259
+
260
+ def send_test2
261
+ mail subject: 'Test email', to: 'to@example.com'
262
+ end
263
+ end
264
+ ```
265
+
266
+ In the `rails console` we can try it out.
267
+
268
+ ```ruby
269
+ TestMailer.send_test.deliver_now!
270
+ # Rendering test_mailer/send_test.html.erb within layouts/mailer
271
+ # Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
272
+ # Rendering test_mailer/send_test.text.erb within layouts/mailer
273
+ # Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
274
+ #TestMailer#send_test: processed outbound mail in 46.1ms
275
+ # >>> settings: {:api_key=>"12345"}
276
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
277
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
278
+ #=> {:response=>"Message sent via API"}
279
+
280
+ TestMailer.send_test2.deliver_now!
281
+ # Rendering test_mailer/send_test.html.erb within layouts/mailer
282
+ # Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
283
+ # Rendering test_mailer/send_test.text.erb within layouts/mailer
284
+ # Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
285
+ #TestMailer#send_test: processed outbound mail in 46.1ms
286
+ # >>> settings: {:api_key=>"12345"}
287
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
288
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
289
+ #=> {:response=>"Message sent via API"}
290
+ ```
291
+
292
+ Or we can just define `delivery_system` where we would like to use the other one.
293
+
294
+ ```ruby
295
+ class TestMailer < ApplicationMailer
296
+ default from: 'from@example.com'
297
+
298
+ def send_test
299
+ mail subject: 'Test email', to: 'to@example.com'
300
+ end
301
+
302
+ def send_test2
303
+ mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api2_client'
304
+ end
305
+ end
306
+ ```
307
+
308
+ In the `rails console` we can try it out.
309
+
310
+ ```ruby
311
+ TestMailer.send_test.deliver_now!
312
+ # Rendering test_mailer/send_test.html.erb within layouts/mailer
313
+ # Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
314
+ # Rendering test_mailer/send_test.text.erb within layouts/mailer
315
+ # Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
316
+ #TestMailer#send_test: processed outbound mail in 46.1ms
317
+ # >>> settings: {:api_key=>"12345"}
318
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
319
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
320
+ #=> {:response=>"Message sent via API"}
321
+
322
+ TestMailer.send_test2.deliver_now!
323
+ # Rendering test_mailer/send_test2.html.erb within layouts/mailer
324
+ # Rendered test_mailer/send_test2.html.erb within layouts/mailer (2.6ms)
325
+ # Rendering test_mailer/send_test2.text.erb within layouts/mailer
326
+ # Rendered test_mailer/send_test2.text.erb within layouts/mailer (0.4ms)
327
+ #TestMailer#send_test2: processed outbound mail in 33.9ms
328
+ # >>> settings: {:api_key=>"54321"}
329
+ # >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "text_part"=>"Test email body\n\n", "html_part"=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}
330
+ # >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body\n\n"}, {:type=>"text/html; charset=UTF-8", :value=>"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n <style>\n /* Email styles need to be inline */\n </style>\n </head>\n\n <body>\n <p>Test email body</p>\n\n </body>\n</html>\n"}]}
331
+ #=> {:response=>"Message sent via API2"}
332
+ ```