mail_plugger 1.0.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +63 -3
- data/README.md +33 -22
- data/lib/fake_plugger/delivery_method.rb +186 -0
- data/lib/fake_plugger/railtie.rb +14 -0
- data/lib/mail_plugger.rb +9 -1
- data/lib/mail_plugger/version.rb +1 -1
- metadata +15 -46
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -22
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -16
- data/.gitignore +0 -11
- data/.rspec +0 -3
- data/.rubocop.yml +0 -53
- data/.travis.yml +0 -22
- data/Appraisals +0 -13
- data/CODE_OF_CONDUCT.md +0 -14
- data/CONTRIBUTING.md +0 -26
- data/Gemfile +0 -18
- data/Gemfile.lock +0 -81
- data/Rakefile +0 -11
- data/bin/console +0 -17
- data/bin/setup +0 -8
- data/docs/usage_in_ruby_on_rails.md +0 -182
- data/docs/usage_in_script_or_console.md +0 -158
- data/docs/usage_of_attachments_in_ruby_on_rails.md +0 -110
- data/docs/usage_of_aws_ses_in_ruby_on_rails.md +0 -136
- data/docs/usage_of_delivery_method.md +0 -104
- data/docs/usage_of_mailgun_in_ruby_on_rails.md +0 -58
- data/docs/usage_of_mandrill_in_ruby_on_rails.md +0 -104
- data/docs/usage_of_more_delivery_system_in_ruby_on_rails.md +0 -332
- data/docs/usage_of_one_delivery_system_with_more_send_methods_in_ruby_on_rails.md +0 -119
- data/docs/usage_of_plug_in_method.md +0 -62
- data/docs/usage_of_postmark_in_ruby_on_rails.md +0 -59
- data/docs/usage_of_secial_options_in_ruby_on_rails.md +0 -90
- data/docs/usage_of_sendgrid_in_ruby_on_rails.md +0 -82
- data/docs/usage_of_sparkpost_in_ruby_on_rails.md +0 -142
- data/gemfiles/.bundle/config +0 -2
- data/gemfiles/mail_2.6.gemfile +0 -14
- data/gemfiles/mail_2.6.gemfile.lock +0 -84
- data/gemfiles/mail_2.7.0.gemfile +0 -14
- data/gemfiles/mail_2.7.0.gemfile.lock +0 -82
- data/gemfiles/mail_2.7.gemfile +0 -14
- data/gemfiles/mail_2.7.gemfile.lock +0 -82
- data/images/mail_plugger.png +0 -0
- data/mail_plugger.gemspec +0 -40
@@ -1,158 +0,0 @@
|
|
1
|
-
# How to use MailPlugger in a Ruby script or IRB console
|
2
|
-
|
3
|
-
First you should be able to `require 'mail'` and `require 'mail_plugger'` to get started.
|
4
|
-
|
5
|
-
We need a class which will send the message in the right format via API.
|
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: 'OK' }
|
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[:body]
|
36
|
-
}
|
37
|
-
]
|
38
|
-
}
|
39
|
-
end
|
40
|
-
|
41
|
-
def generate_recipients
|
42
|
-
@options[:to].map do |to|
|
43
|
-
{
|
44
|
-
email: to
|
45
|
-
}
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
```
|
50
|
-
|
51
|
-
We can use `MailPlugger.plug_in` to add our configurations.
|
52
|
-
|
53
|
-
```ruby
|
54
|
-
MailPlugger.plug_in('test_api_client') do |api|
|
55
|
-
api.delivery_options = %i[from to subject body]
|
56
|
-
api.client = TestApiClientClass
|
57
|
-
end
|
58
|
-
|
59
|
-
message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
|
60
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Tes...
|
61
|
-
|
62
|
-
MailPlugger::DeliveryMethod.new.deliver!(message)
|
63
|
-
# >>> settings: {:api_key=>"12345"}
|
64
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
65
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
66
|
-
# => {:response=>"OK"}
|
67
|
-
```
|
68
|
-
|
69
|
-
Or we can use the `MailPlugger::DeliveryMethod` directly as well.
|
70
|
-
|
71
|
-
```ruby
|
72
|
-
message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
|
73
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Tes...
|
74
|
-
|
75
|
-
MailPlugger::DeliveryMethod.new(delivery_options: %i[from to subject body], client: TestApiClientClass).deliver!(message)
|
76
|
-
# >>> settings: {:api_key=>"12345"}
|
77
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
78
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
79
|
-
# => {:response=>"OK"}
|
80
|
-
```
|
81
|
-
|
82
|
-
Or add `MailPlugger::DeliveryMethod` to `mail.delivery_method`.
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
mail = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
|
86
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Tes...
|
87
|
-
|
88
|
-
mail.delivery_method MailPlugger::DeliveryMethod, { delivery_options: %i[from to subject body], client: TestApiClientClass }
|
89
|
-
# => #<MailPlugger::DeliveryMethod:0x00007fecbbb2ca00 @delivery_options=[:from, :to, :subject, :body], @client=TestApiClientClass, @default_delivery_system=nil, @delivery_settings=nil, @message=nil>
|
90
|
-
|
91
|
-
mail.deliver
|
92
|
-
# >>> settings: {:api_key=>"12345"}
|
93
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
94
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
95
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
96
|
-
|
97
|
-
# or
|
98
|
-
|
99
|
-
mail.deliver!
|
100
|
-
# >>> settings: {:api_key=>"12345"}
|
101
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
102
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
103
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
104
|
-
```
|
105
|
-
|
106
|
-
Let's add delivery settings to the delivery method.
|
107
|
-
|
108
|
-
```ruby
|
109
|
-
mail = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
|
110
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Tes...
|
111
|
-
|
112
|
-
mail.delivery_method MailPlugger::DeliveryMethod, { delivery_options: %i[from to subject body], client: TestApiClientClass, delivery_settings: { return_response: true } }
|
113
|
-
# => #<MailPlugger::DeliveryMethod:0x00007fecbb9e3630 @delivery_options=[:from, :to, :subject, :body], @client=TestApiClientClass, @default_delivery_system=nil, @delivery_settings={:return_response=>true}, @message=nil>
|
114
|
-
|
115
|
-
mail.deliver
|
116
|
-
# >>> settings: {:api_key=>"12345"}
|
117
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
118
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
119
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
120
|
-
|
121
|
-
# or
|
122
|
-
|
123
|
-
mail.deliver!
|
124
|
-
# >>> settings: {:api_key=>"12345"}
|
125
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
126
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
127
|
-
# => {:response=>"OK"}
|
128
|
-
```
|
129
|
-
|
130
|
-
Let's use `MailPlugger.plug_in` method.
|
131
|
-
|
132
|
-
```ruby
|
133
|
-
MailPlugger.plug_in('test_api_client') do |api|
|
134
|
-
api.delivery_options = %i[from to subject body]
|
135
|
-
api.delivery_settings = { return_response: true }
|
136
|
-
api.client = TestApiClientClass
|
137
|
-
end
|
138
|
-
|
139
|
-
mail = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Test email', body: 'Test email body')
|
140
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Tes...
|
141
|
-
|
142
|
-
mail.delivery_method MailPlugger::DeliveryMethod
|
143
|
-
# => #<MailPlugger::DeliveryMethod:0x00007ffed930b8f0 @delivery_options={"test_api_client"=>[:from, :to, :subject, :body]}, @client={"test_api_client"=>TestApiClientClass}, @default_delivery_system="test_api_client", @delivery_settings={"test_api_client"=>{:return_response=>true}}, @message=nil>
|
144
|
-
|
145
|
-
mail.deliver
|
146
|
-
# >>> settings: {:api_key=>"12345"}
|
147
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
148
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
149
|
-
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
150
|
-
|
151
|
-
# or
|
152
|
-
|
153
|
-
mail.deliver!
|
154
|
-
# >>> settings: {:api_key=>"12345"}
|
155
|
-
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
156
|
-
# >>> generate_mail_hash: {:to=>[{:email=>"to@example.com"}], :from=>{:email=>"from@example.com"}, :subject=>"Test email", :content=>[{:type=>"text/plain", :value=>"Test email body"}]}
|
157
|
-
# => {:response=>"OK"}
|
158
|
-
```
|
@@ -1,110 +0,0 @@
|
|
1
|
-
# How to add attachments to the mailer method in Ruby on Rails
|
2
|
-
|
3
|
-
Let's use mailer method which was defined [here](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md).
|
4
|
-
|
5
|
-
Change the API and `MailPlugger.plug_in` method in `config/initializers/mail_plugger.rb`.
|
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: 'OK' }
|
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
|
-
attachments: generate_attachments
|
43
|
-
}
|
44
|
-
end
|
45
|
-
|
46
|
-
def generate_recipients
|
47
|
-
@options[:to].map do |to|
|
48
|
-
{
|
49
|
-
email: to
|
50
|
-
}
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def generate_attachments
|
55
|
-
@options[:attachments].map do |attachment|
|
56
|
-
hash = {
|
57
|
-
filename: attachment[:filename],
|
58
|
-
type: attachment[:type],
|
59
|
-
content: attachment[:content]
|
60
|
-
}
|
61
|
-
hash.merge!(content_id: attachment[:cid]) if attachment.has_key?(:cid)
|
62
|
-
|
63
|
-
hash
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
MailPlugger.plug_in('test_api_client') do |api|
|
69
|
-
api.delivery_options = %i[from to subject text_part html_part attachments]
|
70
|
-
api.delivery_settings = { return_response: true }
|
71
|
-
api.client = TestApiClientClass
|
72
|
-
end
|
73
|
-
```
|
74
|
-
|
75
|
-
Let's change `app/mailers/test_mailer.rb` file.
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
class TestMailer < ApplicationMailer
|
79
|
-
default from: 'from@example.com'
|
80
|
-
|
81
|
-
def send_test
|
82
|
-
attachments['image1.png'] = File.read('/path/to/image1.png')
|
83
|
-
attachments.inline['image2.png'] = File.read('/path/to/image2.png')
|
84
|
-
|
85
|
-
mail subject: 'Test email', to: 'to@example.com'
|
86
|
-
end
|
87
|
-
end
|
88
|
-
```
|
89
|
-
|
90
|
-
Then change `app/views/test_mailer/send_test.html.erb` and add inline attachment.
|
91
|
-
|
92
|
-
```erb
|
93
|
-
<p>Test email body</p>
|
94
|
-
<%= image_tag attachments['image2.png'].url %>
|
95
|
-
```
|
96
|
-
|
97
|
-
In the `rails console` we can try it out.
|
98
|
-
|
99
|
-
```ruby
|
100
|
-
TestMailer.send_test.deliver_now!
|
101
|
-
# Rendering test_mailer/send_test.html.erb within layouts/mailer
|
102
|
-
# Rendered test_mailer/send_test.html.erb within layouts/mailer (4.6ms)
|
103
|
-
# Rendering test_mailer/send_test.text.erb within layouts/mailer
|
104
|
-
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.3ms)
|
105
|
-
#TestMailer#send_test: processed outbound mail in 47.5ms
|
106
|
-
# >>> settings: {:api_key=>"12345"}
|
107
|
-
# >>> 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<img src=\"cid:5ffdd449d282a_163e2ef9c62c0@server.local.mail\" />\n\n </body>\n</html>\n", "attachments"=>[{"filename"=>"image1.png", "type"=>"image/png", "content"=>"iVBORw0KGgoAAAANSUhEUgAAALQAAABdCAMAAAA7WLggg==\n"}, {"cid"=>"5ffdd449d282a_163e2ef9c62c0@server.local.mail", "filename"=>"image2.png", "type"=>"image/png", "content"=>"iVBORw0KGgoAAAANSUhEUgAAAAggg==\n"}]}
|
108
|
-
# >>> 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<img src=\"cid:5ffdd449d282a_163e2ef9c62c0@server.local.mail\" />\n\n </body>\n</html>\n"}], :attachments=>[{:filename=>"image1.png", :type=>"image/png", :content=>"iVBORw0KGgoAAAANSUhEUgAAALQAAABdCAMAAAA7WLggg==\n"}, {:filename=>"image2.png", :type=>"image/png", :content=>"iVBORw0KGgoAAAANSUhEUgAAAAggg==\n", :content_id=>"5ffdd449d282a_163e2ef9c62c0@server.local.mail"}]}
|
109
|
-
#=> {:response=>"OK"}
|
110
|
-
```
|
@@ -1,136 +0,0 @@
|
|
1
|
-
# How to use AWS SES 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 `aws-sdk-gem` to the `Gemfile`.
|
8
|
-
|
9
|
-
```ruby
|
10
|
-
gem 'aws-sdk-ses'
|
11
|
-
```
|
12
|
-
|
13
|
-
Then run `bundle install` command to deploy the gem.
|
14
|
-
|
15
|
-
## Send Email
|
16
|
-
|
17
|
-
Change the API and `MailPlugger.plug_in` method in `config/initializers/mail_plugger.rb`.
|
18
|
-
|
19
|
-
```ruby
|
20
|
-
class AwsSesApiClient
|
21
|
-
def initialize(options = {})
|
22
|
-
@credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
|
23
|
-
@region = ENV['AWS_DEFAULT_REGION']
|
24
|
-
@options = options
|
25
|
-
end
|
26
|
-
|
27
|
-
def deliver
|
28
|
-
Aws::SES::Client.new(credentials: @credentials, region: @region).send_email(generate_mail_hash)
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def generate_mail_hash
|
34
|
-
{
|
35
|
-
source: @options[:from].first,
|
36
|
-
destination: {
|
37
|
-
to_addresses: @options[:to]
|
38
|
-
},
|
39
|
-
message: {
|
40
|
-
subject: {
|
41
|
-
charset: 'UTF-8',
|
42
|
-
data: @options[:subject]
|
43
|
-
},
|
44
|
-
body: {
|
45
|
-
text: {
|
46
|
-
charset: 'UTF-8',
|
47
|
-
data: @options[:text_part]
|
48
|
-
},
|
49
|
-
html: {
|
50
|
-
charset: 'UTF-8',
|
51
|
-
data: @options[:html_part]
|
52
|
-
}
|
53
|
-
}
|
54
|
-
},
|
55
|
-
tags: [
|
56
|
-
{
|
57
|
-
name: @options[:message_obj].delivery_handler.to_s,
|
58
|
-
value: @options[:tag]
|
59
|
-
}
|
60
|
-
],
|
61
|
-
configuration_set_name: @options[:configuration_set_name]
|
62
|
-
}
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
MailPlugger.plug_in('aws_ses') do |api|
|
67
|
-
api.delivery_options = %i[from to subject text_part html_part message_obj tag configuration_set_name]
|
68
|
-
api.delivery_settings = { return_response: true }
|
69
|
-
api.client = AwsSesApiClient
|
70
|
-
end
|
71
|
-
```
|
72
|
-
|
73
|
-
Then modify the mailer method a little bit.
|
74
|
-
|
75
|
-
```ruby
|
76
|
-
class TestMailer < ApplicationMailer
|
77
|
-
default from: 'from@example.com'
|
78
|
-
|
79
|
-
def send_test
|
80
|
-
mail subject: 'Test email', to: 'to@example.com', delivery_system: 'aws_ses', tag: 'send_test', configuration_set_name: "#{Rails.env}_events_tracking"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
```
|
84
|
-
|
85
|
-
## Send Raw Email
|
86
|
-
|
87
|
-
Change the API and `MailPlugger.plug_in` method in `config/initializers/mail_plugger.rb`.
|
88
|
-
|
89
|
-
```ruby
|
90
|
-
class AwsSesApiClient
|
91
|
-
def initialize(options = {})
|
92
|
-
@credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
|
93
|
-
@region = ENV['AWS_DEFAULT_REGION']
|
94
|
-
@options = options
|
95
|
-
end
|
96
|
-
|
97
|
-
def deliver
|
98
|
-
Aws::SES::Client.new(credentials: @credentials, region: @region).send_raw_email(generate_mail_hash)
|
99
|
-
end
|
100
|
-
|
101
|
-
private
|
102
|
-
|
103
|
-
def generate_mail_hash
|
104
|
-
{
|
105
|
-
raw_message: {
|
106
|
-
data: @options[:message_obj].to_s
|
107
|
-
},
|
108
|
-
tags: [
|
109
|
-
{
|
110
|
-
name: @options[:message_obj].delivery_handler.to_s,
|
111
|
-
value: @options[:tag]
|
112
|
-
}
|
113
|
-
],
|
114
|
-
configuration_set_name: @options[:configuration_set_name]
|
115
|
-
}
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
MailPlugger.plug_in('aws_ses') do |api|
|
120
|
-
api.delivery_options = %i[message_obj tag configuration_set_name]
|
121
|
-
api.delivery_settings = { return_response: true }
|
122
|
-
api.client = AwsSesApiClient
|
123
|
-
end
|
124
|
-
```
|
125
|
-
|
126
|
-
Then modify the mailer method a little bit.
|
127
|
-
|
128
|
-
```ruby
|
129
|
-
class TestMailer < ApplicationMailer
|
130
|
-
default from: 'from@example.com'
|
131
|
-
|
132
|
-
def send_test
|
133
|
-
mail subject: 'Test email', to: 'to@example.com', delivery_system: 'aws_ses', tag: 'send_test', configuration_set_name: "#{Rails.env}_events_tracking"
|
134
|
-
end
|
135
|
-
end
|
136
|
-
```
|
@@ -1,104 +0,0 @@
|
|
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.
|