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.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +8 -8
- data/README.md +16 -3
- data/docs/usage_in_ruby_on_rails.md +4 -4
- data/docs/usage_in_script_or_console.md +9 -9
- data/docs/usage_of_attachments_in_ruby_on_rails.md +110 -0
- data/docs/usage_of_aws_ses_in_ruby_on_rails.md +136 -0
- data/docs/usage_of_delivery_method.md +104 -0
- data/docs/usage_of_mailgun_in_ruby_on_rails.md +58 -0
- data/docs/usage_of_mandrill_in_ruby_on_rails.md +104 -0
- data/docs/usage_of_more_delivery_system_in_ruby_on_rails.md +332 -0
- data/docs/usage_of_one_delivery_system_with_more_send_methods_in_ruby_on_rails.md +119 -0
- data/docs/usage_of_plug_in_method.md +62 -0
- data/docs/usage_of_postmark_in_ruby_on_rails.md +59 -0
- data/docs/usage_of_secial_options_in_ruby_on_rails.md +90 -0
- data/docs/usage_of_sendgrid_in_ruby_on_rails.md +82 -0
- data/docs/usage_of_sparkpost_in_ruby_on_rails.md +142 -0
- data/gemfiles/mail_2.6.gemfile.lock +8 -8
- data/gemfiles/mail_2.7.0.gemfile.lock +8 -8
- data/gemfiles/mail_2.7.gemfile.lock +8 -8
- data/lib/mail_plugger.rb +36 -26
- data/lib/mail_plugger/delivery_method.rb +42 -5
- data/lib/mail_plugger/mail_helper.rb +41 -18
- data/lib/mail_plugger/version.rb +1 -1
- data/mail_plugger.gemspec +4 -3
- metadata +18 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c47f868eab003c41513627ce740e0d61ab9628a97ba4fd896181254ce957ad92
|
4
|
+
data.tar.gz: 5212e247ab3e9372e10c403edd9d680109e67aa07cf07d2cb2e2d92a3a2d8b59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d79e80294fffb6b85ad0cb7838b03595d1c695d058feea40640d677e93609095a3b3162d95a2918798c8afc07b8e7d828f79b03019410fd59136d20fce904de8
|
7
|
+
data.tar.gz: 401581be7d293bf54d5e133a65d8b793dbf31c36823a96c7080b5f6f3e965f376b9e84c7296272241f245832d665b6c7ca437a86fac9ea55ee7e150bb087cad4
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## 1.0.0.rc1 (2021-01-13)
|
4
|
+
|
5
|
+
* Change description and fix changelog_uri in gemspec file.
|
6
|
+
* Add/Change documentations.
|
7
|
+
* Update gems.
|
8
|
+
* Change `MailPlugger.plug_in` and `MailPlugger::MailHelper.delivery_options` methods to accept strings and symbols.
|
9
|
+
* Change `MailPlugger::MailHelper.delivery_data` method that we can retrieve massage object as well.
|
10
|
+
* Check hash keys in `MailPlugger::MailHelper.delivery_system` method, so now if we are add wrong `delivey_system` then we are getting a meaningful error message.
|
11
|
+
* Change `MailPlugger::MailHelper.extract_attachments` method. If an attachment is inline then the hash contains the `filename` and `cid` as well.
|
12
|
+
* Add/Change tests.
|
13
|
+
* Change `MailPlugger::MailHelper.delivery_option` that returns with indifferent hash
|
14
|
+
|
3
15
|
## 1.0.0.beta1 (2021-01-02)
|
4
16
|
|
5
17
|
* Implement MailPlugger methods and functionality. See [README.md](https://github.com/norbertszivos/mail_plugger/blob/main/README.md)
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mail_plugger (1.0.0.
|
4
|
+
mail_plugger (1.0.0.rc1)
|
5
5
|
mail (~> 2.5)
|
6
6
|
|
7
7
|
GEM
|
@@ -43,16 +43,16 @@ GEM
|
|
43
43
|
diff-lcs (>= 1.2.0, < 2.0)
|
44
44
|
rspec-support (~> 3.10.0)
|
45
45
|
rspec-support (3.10.1)
|
46
|
-
rubocop (1.
|
46
|
+
rubocop (1.8.0)
|
47
47
|
parallel (~> 1.10)
|
48
|
-
parser (>=
|
48
|
+
parser (>= 3.0.0.0)
|
49
49
|
rainbow (>= 2.2.2, < 4.0)
|
50
50
|
regexp_parser (>= 1.8, < 3.0)
|
51
51
|
rexml
|
52
52
|
rubocop-ast (>= 1.2.0, < 2.0)
|
53
53
|
ruby-progressbar (~> 1.7)
|
54
|
-
unicode-display_width (>= 1.4.0, <
|
55
|
-
rubocop-ast (1.
|
54
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
55
|
+
rubocop-ast (1.4.0)
|
56
56
|
parser (>= 2.7.1.5)
|
57
57
|
rubocop-performance (1.9.2)
|
58
58
|
rubocop (>= 0.90.0, < 2.0)
|
@@ -61,14 +61,14 @@ GEM
|
|
61
61
|
rubocop (~> 1.0)
|
62
62
|
rubocop-ast (>= 1.1.0)
|
63
63
|
ruby-progressbar (1.11.0)
|
64
|
-
simplecov (0.
|
64
|
+
simplecov (0.21.2)
|
65
65
|
docile (~> 1.1)
|
66
66
|
simplecov-html (~> 0.11)
|
67
67
|
simplecov_json_formatter (~> 0.1)
|
68
68
|
simplecov-html (0.12.3)
|
69
69
|
simplecov_json_formatter (0.1.2)
|
70
70
|
thor (1.0.1)
|
71
|
-
unicode-display_width (
|
71
|
+
unicode-display_width (2.0.0)
|
72
72
|
webmock (3.11.0)
|
73
73
|
addressable (>= 2.3.6)
|
74
74
|
crack (>= 0.3.2)
|
@@ -89,4 +89,4 @@ DEPENDENCIES
|
|
89
89
|
webmock (~> 3.0)
|
90
90
|
|
91
91
|
BUNDLED WITH
|
92
|
-
2.2.
|
92
|
+
2.2.4
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# M<img src="https://github.com/norbertszivos/mail_plugger/blob/main/images/mail_plugger.png" height="25" />ilPlugger
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/mail_plugger.svg)](https://badge.fury.io/rb/mail_plugger)
|
4
|
+
[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE.txt)
|
3
5
|
[![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
|
4
6
|
[![Build Status](https://travis-ci.com/norbertszivos/mail_plugger.svg?branch=main)](https://travis-ci.com/norbertszivos/mail_plugger)
|
5
7
|
[![Maintainability](https://api.codeclimate.com/v1/badges/bd2cda43214c111d8d16/maintainability)](https://codeclimate.com/github/norbertszivos/mail_plugger/maintainability)
|
@@ -25,9 +27,20 @@ Or install it yourself as:
|
|
25
27
|
|
26
28
|
## Usage
|
27
29
|
|
28
|
-
[
|
29
|
-
|
30
|
-
[
|
30
|
+
- [How to use MailPlugger.plug_in method](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_plug_in_method.md)
|
31
|
+
- [How to use MailPlugger::DeliveryMethod class](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_delivery_method.md)
|
32
|
+
- [How to use MailPlugger in a Ruby script or IRB console](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_script_or_console.md)
|
33
|
+
- [How to use MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md)
|
34
|
+
- [How to add API specific options to the mailer method in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_secial_options_in_ruby_on_rails.md)
|
35
|
+
- [How to add attachments to the mailer method in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_attachments_in_ruby_on_rails.md)
|
36
|
+
- [How to use more delivey systems in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_more_delivery_system_in_ruby_on_rails.md)
|
37
|
+
- [How to use one delivey system with more send methods in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_one_delivery_system_with_more_send_methods_in_ruby_on_rails.md)
|
38
|
+
- [How to use AWS SES with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_aws_ses_in_ruby_on_rails.md)
|
39
|
+
- [How to use SparkPost with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_sparkpost_in_ruby_on_rails.md)
|
40
|
+
- [How to use SendGrid with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_sendgrid_in_ruby_on_rails.md)
|
41
|
+
- [How to use Mandrill with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_mandrill_in_ruby_on_rails.md)
|
42
|
+
- [How to use Postmark with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_postmark_in_ruby_on_rails.md)
|
43
|
+
- [How to use Mailgun with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_mailgun_in_ruby_on_rails.md)
|
31
44
|
|
32
45
|
## Development
|
33
46
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# How to use MailPlugger in Ruby on Rails
|
2
2
|
|
3
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
4
|
|
@@ -95,7 +95,7 @@ TestMailer.send_test.deliver_now
|
|
95
95
|
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.5ms)
|
96
96
|
#TestMailer#send_test: processed outbound mail in 62.2ms
|
97
97
|
# >>> settings: {:api_key=>"12345"}
|
98
|
-
# >>> options: {
|
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
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
100
|
#Sent mail to to@example.com (12.2ms)
|
101
101
|
#Date: Sat, 02 Jan 2021 15:08:53 +0100
|
@@ -151,7 +151,7 @@ TestMailer.send_test.deliver_now!
|
|
151
151
|
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.0ms)
|
152
152
|
#TestMailer#send_test: processed outbound mail in 20.9ms
|
153
153
|
# >>> settings: {:api_key=>"12345"}
|
154
|
-
# >>> options: {
|
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
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
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
157
|
```
|
@@ -176,7 +176,7 @@ TestMailer.send_test.deliver_now!
|
|
176
176
|
# Rendered test_mailer/send_test.text.erb within layouts/mailer (0.4ms)
|
177
177
|
#TestMailer#send_test: processed outbound mail in 37.0ms
|
178
178
|
# >>> settings: {:api_key=>"12345"}
|
179
|
-
# >>> options: {
|
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
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
181
|
#=> {:response=>"OK"}
|
182
182
|
```
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# How to use MailPlugger in a Ruby script or IRB console
|
2
2
|
|
3
3
|
First you should be able to `require 'mail'` and `require 'mail_plugger'` to get started.
|
4
4
|
|
@@ -61,7 +61,7 @@ message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Tes
|
|
61
61
|
|
62
62
|
MailPlugger::DeliveryMethod.new.deliver!(message)
|
63
63
|
# >>> settings: {:api_key=>"12345"}
|
64
|
-
# >>> options: {
|
64
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
65
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
66
|
# => {:response=>"OK"}
|
67
67
|
```
|
@@ -74,7 +74,7 @@ message = Mail.new(from: 'from@example.com', to: 'to@example.com', subject: 'Tes
|
|
74
74
|
|
75
75
|
MailPlugger::DeliveryMethod.new(delivery_options: %i[from to subject body], client: TestApiClientClass).deliver!(message)
|
76
76
|
# >>> settings: {:api_key=>"12345"}
|
77
|
-
# >>> options: {
|
77
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
78
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
79
|
# => {:response=>"OK"}
|
80
80
|
```
|
@@ -90,7 +90,7 @@ mail.delivery_method MailPlugger::DeliveryMethod, { delivery_options: %i[from to
|
|
90
90
|
|
91
91
|
mail.deliver
|
92
92
|
# >>> settings: {:api_key=>"12345"}
|
93
|
-
# >>> options: {
|
93
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
94
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
95
|
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
96
96
|
|
@@ -98,7 +98,7 @@ mail.deliver
|
|
98
98
|
|
99
99
|
mail.deliver!
|
100
100
|
# >>> settings: {:api_key=>"12345"}
|
101
|
-
# >>> options: {
|
101
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
102
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
103
|
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
104
104
|
```
|
@@ -114,7 +114,7 @@ mail.delivery_method MailPlugger::DeliveryMethod, { delivery_options: %i[from to
|
|
114
114
|
|
115
115
|
mail.deliver
|
116
116
|
# >>> settings: {:api_key=>"12345"}
|
117
|
-
# >>> options: {
|
117
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
118
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
119
|
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
120
120
|
|
@@ -122,7 +122,7 @@ mail.deliver
|
|
122
122
|
|
123
123
|
mail.deliver!
|
124
124
|
# >>> settings: {:api_key=>"12345"}
|
125
|
-
# >>> options: {
|
125
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
126
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
127
|
# => {:response=>"OK"}
|
128
128
|
```
|
@@ -144,7 +144,7 @@ mail.delivery_method MailPlugger::DeliveryMethod
|
|
144
144
|
|
145
145
|
mail.deliver
|
146
146
|
# >>> settings: {:api_key=>"12345"}
|
147
|
-
# >>> options: {
|
147
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
148
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
149
|
# => #<Mail::Message:1960, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: Test email>>
|
150
150
|
|
@@ -152,7 +152,7 @@ mail.deliver
|
|
152
152
|
|
153
153
|
mail.deliver!
|
154
154
|
# >>> settings: {:api_key=>"12345"}
|
155
|
-
# >>> options: {
|
155
|
+
# >>> options: {"from"=>["from@example.com"], "to"=>["to@example.com"], "subject"=>"Test email", "body"=>"Test email body"}
|
156
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
157
|
# => {:response=>"OK"}
|
158
158
|
```
|
@@ -0,0 +1,110 @@
|
|
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
|
+
```
|
@@ -0,0 +1,136 @@
|
|
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
|
+
```
|