mailtrap 1.0.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab906395e1a92aac49d71c10d3e8935982997a51b05b52efd6ceee3c847e49a8
4
- data.tar.gz: a86a0f2d5a265742d8801533c8c1566f2dd96af21af3d22197aea1dd7b788d4e
3
+ metadata.gz: 23acecf40de87c9ec6c68cee1a63244548f699819ce122b9f37877e6429a25e9
4
+ data.tar.gz: 5e2009cefd12428d8274ac79a57644f3b5bfcd6b4e266afc62ee5d274b22daa2
5
5
  SHA512:
6
- metadata.gz: e2d3a90dabf85ebc515b5213a087c65eb6ca444c3848b913d550a4ebf937b5e318cecc8307261a2ae21f033e367bd5f3113d3191d7a0a4f70acd5e4ede738d20
7
- data.tar.gz: 4d30beda4f75470bf4f3d72145d0ff1f0ed784261df3954de17fa56d3c54a9a77ebddead2d468e7b4f53172bf5df498fac773e92198ad033e474b5e139a53d09
6
+ metadata.gz: '00157801f3ae86ee16ffb51850c4af77f0213d736371fffacd3799c045f60955455f27c03283d8df16b41c5f0c635cfc22c1808c7d268c7b40c3d2ccd672e92c'
7
+ data.tar.gz: f8b80546c0dd317a64882cfb61ddbc8e646057da495508913fbefc9f26cd75a71129d807752a3cd41aa4df5ad38888e7c4a06247d2bbce8233af52d6507aa3e3
data/.rubocop.yml CHANGED
@@ -13,6 +13,9 @@ Naming/MethodParameterName:
13
13
  RSpec/MultipleExpectations:
14
14
  Max: 4
15
15
 
16
+ RSpec/NestedGroups:
17
+ Max: 4
18
+
16
19
  Style/Documentation:
17
20
  Enabled: false
18
21
 
data/CHANGELOG.md CHANGED
@@ -5,3 +5,12 @@
5
5
  ## [1.0.1] - 2022-06-20
6
6
 
7
7
  - Update packed files list
8
+
9
+
10
+ ## [1.1.0] - 2022-07-22
11
+
12
+ - Add ActionMailer support
13
+
14
+ ## [1.1.1] - 2022-10-14
15
+
16
+ - Fix custom port and host usage
data/Gemfile CHANGED
@@ -4,6 +4,8 @@ source 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
6
 
7
+ gem 'mail'
8
+ gem 'net-smtp'
7
9
  gem 'rake', '~> 13.0'
8
10
  gem 'rspec', '~> 3.0'
9
11
  gem 'rspec-its'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mailtrap (1.0.1)
4
+ mailtrap (1.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -13,6 +13,10 @@ GEM
13
13
  rexml
14
14
  diff-lcs (1.5.0)
15
15
  hashdiff (1.0.1)
16
+ mail (2.7.1)
17
+ mini_mime (>= 0.1.1)
18
+ mini_mime (1.1.2)
19
+ net-smtp (0.1.0)
16
20
  parallel (1.22.1)
17
21
  parser (3.1.2.0)
18
22
  ast (~> 2.4.1)
@@ -62,7 +66,9 @@ PLATFORMS
62
66
  ruby
63
67
 
64
68
  DEPENDENCIES
69
+ mail
65
70
  mailtrap!
71
+ net-smtp
66
72
  rake (~> 13.0)
67
73
  rspec (~> 3.0)
68
74
  rspec-its
data/README.md CHANGED
@@ -87,6 +87,27 @@ client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
87
87
  client.send(mail)
88
88
  ```
89
89
 
90
+ ### ActionMailer
91
+
92
+ This gem also adds ActionMailer delivery method. To configure it, add following to your ActionMailer configuration (in Rails projects located in `config/$ENVIRONMENT.rb`):
93
+ ```ruby
94
+ config.action_mailer.delivery_method = :mailtrap
95
+ config.action_mailer.mailtrap_settings = {
96
+ api_key: ENV.fetch('MAILTRAP_API_KEY')
97
+ }
98
+ ```
99
+ And continue to use ActionMailer as usual.
100
+
101
+ To add `category` and `custom_variables`, add them to the mail generation:
102
+ ```ruby
103
+ mail(
104
+ to: 'your@email.com',
105
+ subject: 'You are awesome!',
106
+ category: 'Test category',
107
+ custom_variables: { test_variable: 'abc' }
108
+ )
109
+ ```
110
+
90
111
  ## Development
91
112
 
92
113
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailtrap
4
+ module ActionMailer
5
+ class DeliveryMethod
6
+ attr_accessor :settings
7
+
8
+ def initialize(settings)
9
+ self.settings = settings
10
+ end
11
+
12
+ def deliver!(message)
13
+ mail = Mailtrap::Sending::Convert.from_message(message)
14
+
15
+ client.send(mail)
16
+ end
17
+
18
+ private
19
+
20
+ def client
21
+ @client ||= Mailtrap::Sending::Client.new(**settings.slice(:api_key, :api_host, :api_port))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailtrap
4
+ module ActionMailer
5
+ class Railtie < Rails::Railtie
6
+ initializer 'mailtrap_action_mailer.add_delivery_method', before: 'action_mailer.set_configs' do
7
+ ActiveSupport.on_load(:action_mailer) do
8
+ ::ActionMailer::Base.add_delivery_method(:mailtrap, Mailtrap::ActionMailer::DeliveryMethod)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'action_mailer/delivery_method'
4
+ require_relative 'action_mailer/railtie' if defined? Rails
5
+
6
+ module Mailtrap
7
+ module ActionMailer; end
8
+ end
@@ -30,7 +30,7 @@ module Mailtrap
30
30
  private
31
31
 
32
32
  def http_client
33
- @http_client ||= Net::HTTP.new(API_HOST, API_PORT).tap { |client| client.use_ssl = true }
33
+ @http_client ||= Net::HTTP.new(api_host, api_port).tap { |client| client.use_ssl = true }
34
34
  end
35
35
 
36
36
  def post_request(path, body)
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+
5
+ module Mailtrap
6
+ module Sending
7
+ module Convert
8
+ class << self
9
+ def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
10
+ Mailtrap::Sending::Mail.new(
11
+ from: prepare_address(message['from']&.address_list&.addresses&.first),
12
+ to: prepare_addresses(message['to']&.address_list&.addresses),
13
+ cc: prepare_addresses(message['cc']&.address_list&.addresses),
14
+ bcc: prepare_addresses(message['bcc']&.address_list&.addresses),
15
+ subject: message.subject,
16
+ text: prepare_text_part(message),
17
+ html: prepare_html_part(message),
18
+ headers: prepare_headers(message),
19
+ attachments: prepare_attachments(message.attachments),
20
+ category: message['category']&.unparsed_value,
21
+ custom_variables: message['custom_variables']&.unparsed_value
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ PROCESSED_HEADERS = %w[
28
+ from
29
+ to
30
+ cc
31
+ bcc
32
+ subject
33
+ category
34
+ customvariables
35
+ contenttype
36
+ ].freeze
37
+
38
+ def prepare_addresses(addresses)
39
+ Array(addresses).map { |address| prepare_address(address) }
40
+ end
41
+
42
+ def prepare_headers(message)
43
+ message
44
+ .header_fields
45
+ .reject { |header| PROCESSED_HEADERS.include?(header.name.downcase.delete('-')) }
46
+ .to_h { |header| [header.name, header.value] }
47
+ .compact
48
+ end
49
+
50
+ def prepare_address(address)
51
+ {
52
+ email: address.address,
53
+ name: address.display_name
54
+ }.compact
55
+ end
56
+
57
+ def prepare_attachments(attachments_list = [])
58
+ attachments_list.map do |attachment|
59
+ {
60
+ content: Base64.strict_encode64(attachment.body.decoded),
61
+ type: attachment.mime_type,
62
+ filename: attachment.filename,
63
+ disposition: attachment.header[:content_disposition]&.disposition_type,
64
+ content_id: attachment.header[:content_id]&.field&.content_id
65
+ }.compact
66
+ end
67
+ end
68
+
69
+ def prepare_html_part(message)
70
+ return message.body.decoded if message.mime_type == 'text/html'
71
+
72
+ message.html_part&.decoded
73
+ end
74
+
75
+ def prepare_text_part(message)
76
+ return message.body.decoded if message.mime_type == 'text/plain' || message.mime_type.nil?
77
+
78
+ message.text_part&.decoded
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'sending/attachment'
4
4
  require_relative 'sending/client'
5
+ require_relative 'sending/convert'
5
6
  require_relative 'sending/mail'
6
7
 
7
8
  module Mailtrap
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mailtrap
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1.1'
5
5
  end
data/lib/mailtrap.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'mailtrap/action_mailer' if defined? ActionMailer
3
4
  require_relative 'mailtrap/sending'
4
5
  require_relative 'mailtrap/version'
5
6
 
data/mailtrap.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/mailtrap/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mailtrap'
7
+ spec.version = Mailtrap::VERSION
8
+ spec.authors = ['Railsware Products Studio, Inc.']
9
+ spec.email = ['support@mailtrap.io']
10
+
11
+ spec.summary = 'Official mailtrap.io API client'
12
+ spec.description = 'Official mailtrap.io API client'
13
+ spec.homepage = 'https://github.com/railsware/mailtrap-ruby'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.7.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/railsware/mailtrap-ruby'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/railsware/mailtrap-ruby/blob/main/CHANGELOG.md'
20
+ spec.metadata['rubygems_mfa_required'] = 'true'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|github|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.require_paths = ['lib']
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailtrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Railsware Products Studio, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-20 00:00:00.000000000 Z
11
+ date: 2022-10-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Official mailtrap.io API client
14
14
  email:
@@ -28,11 +28,16 @@ files:
28
28
  - README.md
29
29
  - Rakefile
30
30
  - lib/mailtrap.rb
31
+ - lib/mailtrap/action_mailer.rb
32
+ - lib/mailtrap/action_mailer/delivery_method.rb
33
+ - lib/mailtrap/action_mailer/railtie.rb
31
34
  - lib/mailtrap/sending.rb
32
35
  - lib/mailtrap/sending/attachment.rb
33
36
  - lib/mailtrap/sending/client.rb
37
+ - lib/mailtrap/sending/convert.rb
34
38
  - lib/mailtrap/sending/mail.rb
35
39
  - lib/mailtrap/version.rb
40
+ - mailtrap.gemspec
36
41
  homepage: https://github.com/railsware/mailtrap-ruby
37
42
  licenses:
38
43
  - MIT