mailtrap 1.0.1 → 1.1.0

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: dd604b5702f48dbaa0cf3fb294c3d000af11891ca3e0d6453af0763c1aa0abd4
4
+ data.tar.gz: 66baa71b1a5cb26cbd4012f6b6a4ab10fd585850f93792ffc9b2776bde9019ae
5
5
  SHA512:
6
- metadata.gz: e2d3a90dabf85ebc515b5213a087c65eb6ca444c3848b913d550a4ebf937b5e318cecc8307261a2ae21f033e367bd5f3113d3191d7a0a4f70acd5e4ede738d20
7
- data.tar.gz: 4d30beda4f75470bf4f3d72145d0ff1f0ed784261df3954de17fa56d3c54a9a77ebddead2d468e7b4f53172bf5df498fac773e92198ad033e474b5e139a53d09
6
+ metadata.gz: 853e6421d2d5d3c02bd0b090b4810383b5b30e6b8bfc178379ea602e56eb44ba1b298a2bb8e87575abfd0a37bc110a5bb8c8d14ca78d3c75729974cf389b2fd6
7
+ data.tar.gz: b9ca3ed482f8077a35151237a6078ef88ce2cdc58f1f097f72730f6fe71eb7367235559ebbbeba24b1622a06af10f31c728deb93ee6ef4207d6091543e5944bb
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,8 @@
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
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.0)
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
@@ -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.0'
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Railsware Products Studio, Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-20 00:00:00.000000000 Z
11
+ date: 2022-08-01 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
@@ -41,7 +46,7 @@ metadata:
41
46
  source_code_uri: https://github.com/railsware/mailtrap-ruby
42
47
  changelog_uri: https://github.com/railsware/mailtrap-ruby/blob/main/CHANGELOG.md
43
48
  rubygems_mfa_required: 'true'
44
- post_install_message:
49
+ post_install_message:
45
50
  rdoc_options: []
46
51
  require_paths:
47
52
  - lib
@@ -57,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
62
  version: '0'
58
63
  requirements: []
59
64
  rubygems_version: 3.1.6
60
- signing_key:
65
+ signing_key:
61
66
  specification_version: 4
62
67
  summary: Official mailtrap.io API client
63
68
  test_files: []