docs/usage_of_more_api_delivery_systems_in_ruby_on_rails.md
# How to use more API delivery systems in Ruby on Rails
Let's modify the configuration which was defined [here](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md#api).
Add a new API Class in `config/initializers/mail_plugger.rb` (you can define it in another place, just now it is easier to do this).
```ruby
# NOTE: This is just an example for testing...
class TestApiClientClass
def initialize(options = {})
@settings = { api_key: '12345' }
@options = options
end
def deliver
# e.g. API.new(@settings).client.post(generate_mail_hash)
puts " >>> settings: #{@settings.inspect}"
puts " >>> options: #{@options.inspect}"
puts " >>> generate_mail_hash: #{generate_mail_hash.inspect}"
{ response: 'Message sent via API' }
end
private
def generate_mail_hash
{
to: generate_recipients,
from: {
email: @options[:from].first
},
subject: @options[:subject],
content: [
{
type: 'text/plain',
value: @options[:text_part]
},
{
type: 'text/html; charset=UTF-8',
value: @options[:html_part]
}
]
}
end
def generate_recipients
@options[:to].map do |to|
{
email: to
}
end
end
end
class TestApi2ClientClass
def initialize(options = {})
@settings = { api_key: '54321' }
@options = options
end
def deliver
# e.g. API.new(@settings).client.post(generate_mail_hash)
puts " >>> settings: #{@settings.inspect}"
puts " >>> options: #{@options.inspect}"
puts " >>> generate_mail_hash: #{generate_mail_hash.inspect}"
{ response: 'Message sent via API2' }
end
private
def generate_mail_hash
{
to: generate_recipients,
from: {
email: @options[:from].first
},
subject: @options[:subject],
content: [
{
type: 'text/plain',
value: @options[:text_part]
},
{
type: 'text/html; charset=UTF-8',
value: @options[:html_part]
}
]
}
end
def generate_recipients
@options[:to].map do |to|
{
email: to
}
end
end
end
MailPlugger.plug_in('test_api_client') do |api|
api.client = TestApiClientClass
api.delivery_options = %i[from to subject text_part html_part]
api.delivery_settings = { return_response: true }
end
MailPlugger.plug_in('test_api2_client') do |api|
api.client = TestApi2ClientClass
api.delivery_options = %i[from to subject text_part html_part]
api.delivery_settings = { return_response: true }
end
```
Then change `app/mailers/test_mailer.rb` file.
```ruby
class TestMailer < ApplicationMailer
default from: 'from@example.com'
def send_test
mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api_client'
end
def send_test2
mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api2_client'
end
end
```
Then we should add views of the second mailer method, so create `app/views/test_mailer/send_test2.html.erb`
```erb
<p>Test email body</p>
```
and `app/views/test_mailer/send_test2.text.erb`.
```erb
Test email body
```
In the `rails console` we can try it out.
```ruby
TestMailer.send_test.deliver_now!
# Rendering test_mailer/send_test.html.erb within layouts/mailer
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
# Rendering test_mailer/send_test.text.erb within layouts/mailer
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
#TestMailer#send_test: processed outbound mail in 46.1ms
# >>> settings: {:api_key=>"12345"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API"}
TestMailer.send_test2.deliver_now!
# Rendering test_mailer/send_test2.html.erb within layouts/mailer
# Rendered test_mailer/send_test2.html.erb within layouts/mailer (2.6ms)
# Rendering test_mailer/send_test2.text.erb within layouts/mailer
# Rendered test_mailer/send_test2.text.erb within layouts/mailer (0.4ms)
#TestMailer#send_test2: processed outbound mail in 33.9ms
# >>> settings: {:api_key=>"54321"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API2"}
```
In the `app/mailers/test_mailer.rb` file we can use the Rails default option as well to define `delivery_system`.
```ruby
class TestMailer < ApplicationMailer
default from: 'from@example.com', delivery_system: 'test_api_client'
def send_test
mail subject: 'Test email', to: 'to@example.com'
end
def send_test2
mail subject: 'Test email', to: 'to@example.com'
end
end
```
In the `rails console` we can try it out.
```ruby
TestMailer.send_test.deliver_now!
# Rendering test_mailer/send_test.html.erb within layouts/mailer
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
# Rendering test_mailer/send_test.text.erb within layouts/mailer
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
#TestMailer#send_test: processed outbound mail in 46.1ms
# >>> settings: {:api_key=>"12345"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API"}
TestMailer.send_test2.deliver_now!
# Rendering test_mailer/send_test.html.erb within layouts/mailer
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
# Rendering test_mailer/send_test.text.erb within layouts/mailer
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
#TestMailer#send_test: processed outbound mail in 46.1ms
# >>> settings: {:api_key=>"12345"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API"}
```
Or we can use default, but override it in the method.
```ruby
class TestMailer < ApplicationMailer
default from: 'from@example.com', delivery_system: 'test_api_client'
def send_test
mail subject: 'Test email', to: 'to@example.com'
end
def send_test2
mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api2_client'
end
end
```
In the `rails console` we can try it out.
```ruby
TestMailer.send_test.deliver_now!
# Rendering test_mailer/send_test.html.erb within layouts/mailer
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
# Rendering test_mailer/send_test.text.erb within layouts/mailer
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
#TestMailer#send_test: processed outbound mail in 46.1ms
# >>> settings: {:api_key=>"12345"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API"}
TestMailer.send_test2.deliver_now!
# Rendering test_mailer/send_test2.html.erb within layouts/mailer
# Rendered test_mailer/send_test2.html.erb within layouts/mailer (2.6ms)
# Rendering test_mailer/send_test2.text.erb within layouts/mailer
# Rendered test_mailer/send_test2.text.erb within layouts/mailer (0.4ms)
#TestMailer#send_test2: processed outbound mail in 33.9ms
# >>> settings: {:api_key=>"54321"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API2"}
```
Or if we are not define any `delivey_system` then it will use the first defined one with `MailPlugger.plug_in` method.
```ruby
class TestMailer < ApplicationMailer
default from: 'from@example.com'
def send_test
mail subject: 'Test email', to: 'to@example.com'
end
def send_test2
mail subject: 'Test email', to: 'to@example.com'
end
end
```
In the `rails console` we can try it out.
```ruby
TestMailer.send_test.deliver_now!
# Rendering test_mailer/send_test.html.erb within layouts/mailer
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
# Rendering test_mailer/send_test.text.erb within layouts/mailer
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
#TestMailer#send_test: processed outbound mail in 46.1ms
# >>> settings: {:api_key=>"12345"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API"}
TestMailer.send_test2.deliver_now!
# Rendering test_mailer/send_test.html.erb within layouts/mailer
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
# Rendering test_mailer/send_test.text.erb within layouts/mailer
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
#TestMailer#send_test: processed outbound mail in 46.1ms
# >>> settings: {:api_key=>"12345"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API"}
```
Or we can just define `delivery_system` where we would like to use the other one.
```ruby
class TestMailer < ApplicationMailer
default from: 'from@example.com'
def send_test
mail subject: 'Test email', to: 'to@example.com'
end
def send_test2
mail subject: 'Test email', to: 'to@example.com', delivery_system: 'test_api2_client'
end
end
```
In the `rails console` we can try it out.
```ruby
TestMailer.send_test.deliver_now!
# Rendering test_mailer/send_test.html.erb within layouts/mailer
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.0ms)
# Rendering test_mailer/send_test.text.erb within layouts/mailer
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
#TestMailer#send_test: processed outbound mail in 46.1ms
# >>> settings: {:api_key=>"12345"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API"}
TestMailer.send_test2.deliver_now!
# Rendering test_mailer/send_test2.html.erb within layouts/mailer
# Rendered test_mailer/send_test2.html.erb within layouts/mailer (2.6ms)
# Rendering test_mailer/send_test2.text.erb within layouts/mailer
# Rendered test_mailer/send_test2.text.erb within layouts/mailer (0.4ms)
#TestMailer#send_test2: processed outbound mail in 33.9ms
# >>> settings: {:api_key=>"54321"}
# >>> 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"}
# >>> 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"}]}
#=> {:response=>"Message sent via API2"}
```