mail_plugger 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/lib/mail_plugger/version.rb +1 -1
- metadata +6 -41
- 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,182 +0,0 @@
|
|
1
|
-
# How to use MailPlugger in Ruby on Rails
|
2
|
-
|
3
|
-
After to add `mail_plugger` gem and the gem of API of the mail provider, create `config/initializers/mail_plugger.rb` file and add something similar.
|
4
|
-
|
5
|
-
```ruby
|
6
|
-
# NOTE: This is just an example for testing...
|
7
|
-
class TestApiClientClass
|
8
|
-
def initialize(options = {})
|
9
|
-
@settings = { api_key: '12345' }
|
10
|
-
@options = options
|
11
|
-
end
|
12
|
-
|
13
|
-
def deliver
|
14
|
-
# e.g. API.new(@settings).client.post(generate_mail_hash)
|
15
|
-
puts " >>> settings: #{@settings.inspect}"
|
16
|
-
puts " >>> options: #{@options.inspect}"
|
17
|
-
puts " >>> generate_mail_hash: #{generate_mail_hash.inspect}"
|
18
|
-
{ response: 'OK' }
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def generate_mail_hash
|
24
|
-
{
|
25
|
-
to: generate_recipients,
|
26
|
-
from: {
|
27
|
-
email: @options[:from].first
|
28
|
-
},
|
29
|
-
subject: @options[:subject],
|
30
|
-
content: [
|
31
|
-
{
|
32
|
-
type: 'text/plain',
|
33
|
-
value: @options[:text_part]
|
34
|
-
},
|
35
|
-
{
|
36
|
-
type: 'text/html; charset=UTF-8',
|
37
|
-
value: @options[:html_part]
|
38
|
-
}
|
39
|
-
]
|
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('test_api_client') do |api|
|
53
|
-
api.delivery_options = %i[from to subject text_part html_part]
|
54
|
-
api.client = TestApiClientClass
|
55
|
-
end
|
56
|
-
```
|
57
|
-
|
58
|
-
Then change `config/application.rb` file.
|
59
|
-
|
60
|
-
```ruby
|
61
|
-
config.action_mailer.delivery_method = :mail_plugger
|
62
|
-
```
|
63
|
-
|
64
|
-
So now we should add a mailer method. Let's create `app/mailers/test_mailer.rb` file.
|
65
|
-
|
66
|
-
```ruby
|
67
|
-
class TestMailer < ApplicationMailer
|
68
|
-
default from: 'from@example.com'
|
69
|
-
|
70
|
-
def send_test
|
71
|
-
mail subject: 'Test email', to: 'to@example.com'
|
72
|
-
end
|
73
|
-
end
|
74
|
-
```
|
75
|
-
|
76
|
-
Then we should add views (the body) of this email, so create `app/views/test_mailer/send_test.html.erb`
|
77
|
-
|
78
|
-
```erb
|
79
|
-
<p>Test email body</p>
|
80
|
-
```
|
81
|
-
|
82
|
-
and `app/views/test_mailer/send_test.text.erb`.
|
83
|
-
|
84
|
-
```erb
|
85
|
-
Test email body
|
86
|
-
```
|
87
|
-
|
88
|
-
In the `rails console` we can try it out.
|
89
|
-
|
90
|
-
```ruby
|
91
|
-
TestMailer.send_test.deliver_now
|
92
|
-
# Rendering test_mailer/send_test.html.erb within layouts/mailer
|
93
|
-
# Rendered test_mailer/send_test.html.erb within layouts/mailer (1.3ms)
|
94
|
-
# Rendering test_mailer/send_test.text.erb within layouts/mailer
|
95
|
-
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.5ms)
|
96
|
-
#TestMailer#send_test: processed outbound mail in 62.2ms
|
97
|
-
# >>> settings: {:api_key=>"12345"}
|
98
|
-
# >>> 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"}
|
99
|
-
# >>> 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"}]}
|
100
|
-
#Sent mail to to@example.com (12.2ms)
|
101
|
-
#Date: Sat, 02 Jan 2021 15:08:53 +0100
|
102
|
-
#From: from@example.com
|
103
|
-
#To: to@example.com
|
104
|
-
#Message-ID: <5ff07e7597b40_104cfebb4988d3@100S-Mac.local.mail>
|
105
|
-
#Subject: Test email
|
106
|
-
#Mime-Version: 1.0
|
107
|
-
#Content-Type: multipart/alternative;
|
108
|
-
# boundary="--==_mimepart_5ff07e75956a7_104cfebb498739";
|
109
|
-
# charset=UTF-8
|
110
|
-
#Content-Transfer-Encoding: 7bit
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#----==_mimepart_5ff07e75956a7_104cfebb498739
|
114
|
-
#Content-Type: text/plain;
|
115
|
-
# charset=UTF-8
|
116
|
-
#Content-Transfer-Encoding: 7bit
|
117
|
-
#
|
118
|
-
#Test email body
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#----==_mimepart_5ff07e75956a7_104cfebb498739
|
122
|
-
#Content-Type: text/html;
|
123
|
-
# charset=UTF-8
|
124
|
-
#Content-Transfer-Encoding: 7bit
|
125
|
-
#
|
126
|
-
#<!DOCTYPE html>
|
127
|
-
#<html>
|
128
|
-
# <head>
|
129
|
-
# <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
130
|
-
# <style>
|
131
|
-
# /* Email styles need to be inline */
|
132
|
-
# </style>
|
133
|
-
# </head>
|
134
|
-
#
|
135
|
-
# <body>
|
136
|
-
# <p>Test email body</p>
|
137
|
-
#
|
138
|
-
# </body>
|
139
|
-
#</html>
|
140
|
-
#
|
141
|
-
#----==_mimepart_5ff07e75956a7_104cfebb498739--
|
142
|
-
|
143
|
-
#=> #<Mail::Message:61100, Multipart: true, Headers: <Date: Sat, 02 Jan 2021 15:08:53 +0100>, <From: from@example.com>, <To: to@example.com>, <Message-ID: <5ff07e7597b40_104cfebb4988d3@100S-Mac.local.mail>>, <Subject: Test email>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_5ff07e75956a7_104cfebb498739"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
|
144
|
-
|
145
|
-
# or use ! to not render mail
|
146
|
-
|
147
|
-
TestMailer.send_test.deliver_now!
|
148
|
-
# Rendering test_mailer/send_test.html.erb within layouts/mailer
|
149
|
-
# Rendered test_mailer/send_test.html.erb within layouts/mailer (0.1ms)
|
150
|
-
# Rendering test_mailer/send_test.text.erb within layouts/mailer
|
151
|
-
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.0ms)
|
152
|
-
#TestMailer#send_test: processed outbound mail in 20.9ms
|
153
|
-
# >>> settings: {:api_key=>"12345"}
|
154
|
-
# >>> 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"}
|
155
|
-
# >>> 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"}]}
|
156
|
-
#=> #<Mail::Message:61140, Multipart: true, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_5ff082bd7aab5_10afcebb4503a4"; charset=UTF-8>>
|
157
|
-
```
|
158
|
-
|
159
|
-
Let's add delivery settings as well in `config/initializers/mail_plugger.rb`.
|
160
|
-
|
161
|
-
```ruby
|
162
|
-
MailPlugger.plug_in('test_api_client') do |api|
|
163
|
-
api.delivery_options = %i[from to subject text_part html_part]
|
164
|
-
api.delivery_settings = { return_response: true }
|
165
|
-
api.client = TestApiClientClass
|
166
|
-
end
|
167
|
-
```
|
168
|
-
|
169
|
-
Then in the `rails console`.
|
170
|
-
|
171
|
-
```ruby
|
172
|
-
TestMailer.send_test.deliver_now!
|
173
|
-
# Rendering test_mailer/send_test.html.erb within layouts/mailer
|
174
|
-
# Rendered test_mailer/send_test.html.erb within layouts/mailer (0.8ms)
|
175
|
-
# Rendering test_mailer/send_test.text.erb within layouts/mailer
|
176
|
-
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.4ms)
|
177
|
-
#TestMailer#send_test: processed outbound mail in 37.0ms
|
178
|
-
# >>> settings: {:api_key=>"12345"}
|
179
|
-
# >>> 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"}
|
180
|
-
# >>> 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"}]}
|
181
|
-
#=> {:response=>"OK"}
|
182
|
-
```
|
@@ -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
|
-
```
|