mailtrap 0.2.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dd604b5702f48dbaa0cf3fb294c3d000af11891ca3e0d6453af0763c1aa0abd4
4
+ data.tar.gz: 66baa71b1a5cb26cbd4012f6b6a4ab10fd585850f93792ffc9b2776bde9019ae
5
+ SHA512:
6
+ metadata.gz: 853e6421d2d5d3c02bd0b090b4810383b5b30e6b8bfc178379ea602e56eb44ba1b298a2bb8e87575abfd0a37bc110a5bb8c8d14ca78d3c75729974cf389b2fd6
7
+ data.tar.gz: b9ca3ed482f8077a35151237a6078ef88ce2cdc58f1f097f72730f6fe71eb7367235559ebbbeba24b1622a06af10f31c728deb93ee6ef4207d6091543e5944bb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.7
5
+ NewCops: enable
6
+
7
+ Layout/LineLength:
8
+ Max: 120
9
+
10
+ Naming/MethodParameterName:
11
+ MinNameLength: 2
12
+
13
+ RSpec/MultipleExpectations:
14
+ Max: 4
15
+
16
+ RSpec/NestedGroups:
17
+ Max: 4
18
+
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ Style/StringLiterals:
23
+ Enabled: true
24
+ EnforcedStyle: single_quotes
25
+
26
+ Style/StringLiteralsInInterpolation:
27
+ Enabled: true
28
+ EnforcedStyle: double_quotes
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ ## [1.0.0] - 2022-06-14
2
+
3
+ - Initial release
4
+
5
+ ## [1.0.1] - 2022-06-20
6
+
7
+ - Update packed files list
8
+
9
+
10
+ ## [1.1.0] - 2022-07-22
11
+
12
+ - Add ActionMailer support
@@ -0,0 +1,81 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or advances of any kind
22
+ * Trolling, insulting or derogatory comments, and personal or political attacks
23
+ * Public or private harassment
24
+ * Publishing others' private information, such as a physical or email address, without their explicit permission
25
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
26
+
27
+ ## Enforcement Responsibilities
28
+
29
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
32
+
33
+ ## Scope
34
+
35
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
36
+
37
+ ## Enforcement
38
+
39
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at support@mailtrap.io. All complaints will be reviewed and investigated promptly and fairly.
40
+
41
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
42
+
43
+ ## Enforcement Guidelines
44
+
45
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
46
+
47
+ ### 1. Correction
48
+
49
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
50
+
51
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
52
+
53
+ ### 2. Warning
54
+
55
+ **Community Impact**: A violation through a single incident or series of actions.
56
+
57
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
58
+
59
+ ### 3. Temporary Ban
60
+
61
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
62
+
63
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
64
+
65
+ ### 4. Permanent Ban
66
+
67
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
68
+
69
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
70
+
71
+ ## Attribution
72
+
73
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
74
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
75
+
76
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
77
+
78
+ [homepage]: https://www.contributor-covenant.org
79
+
80
+ For answers to common questions about this code of conduct, see the FAQ at
81
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'mail'
8
+ gem 'net-smtp'
9
+ gem 'rake', '~> 13.0'
10
+ gem 'rspec', '~> 3.0'
11
+ gem 'rspec-its'
12
+ gem 'rubocop', '~> 1.21'
13
+ gem 'rubocop-rspec', require: false
14
+ gem 'vcr'
15
+ gem 'webmock'
data/Gemfile.lock ADDED
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mailtrap (1.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ ast (2.4.2)
12
+ crack (0.4.5)
13
+ rexml
14
+ diff-lcs (1.5.0)
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)
20
+ parallel (1.22.1)
21
+ parser (3.1.2.0)
22
+ ast (~> 2.4.1)
23
+ public_suffix (4.0.7)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.4.0)
27
+ rexml (3.2.5)
28
+ rspec (3.11.0)
29
+ rspec-core (~> 3.11.0)
30
+ rspec-expectations (~> 3.11.0)
31
+ rspec-mocks (~> 3.11.0)
32
+ rspec-core (3.11.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-expectations (3.11.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.11.0)
37
+ rspec-its (1.3.0)
38
+ rspec-core (>= 3.0.0)
39
+ rspec-expectations (>= 3.0.0)
40
+ rspec-mocks (3.11.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.11.0)
43
+ rspec-support (3.11.0)
44
+ rubocop (1.29.1)
45
+ parallel (~> 1.10)
46
+ parser (>= 3.1.0.0)
47
+ rainbow (>= 2.2.2, < 4.0)
48
+ regexp_parser (>= 1.8, < 3.0)
49
+ rexml (>= 3.2.5, < 4.0)
50
+ rubocop-ast (>= 1.17.0, < 2.0)
51
+ ruby-progressbar (~> 1.7)
52
+ unicode-display_width (>= 1.4.0, < 3.0)
53
+ rubocop-ast (1.18.0)
54
+ parser (>= 3.1.1.0)
55
+ rubocop-rspec (2.11.1)
56
+ rubocop (~> 1.19)
57
+ ruby-progressbar (1.11.0)
58
+ unicode-display_width (2.1.0)
59
+ vcr (6.1.0)
60
+ webmock (3.14.0)
61
+ addressable (>= 2.8.0)
62
+ crack (>= 0.3.2)
63
+ hashdiff (>= 0.4.0, < 2.0.0)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ mail
70
+ mailtrap!
71
+ net-smtp
72
+ rake (~> 13.0)
73
+ rspec (~> 3.0)
74
+ rspec-its
75
+ rubocop (~> 1.21)
76
+ rubocop-rspec
77
+ vcr
78
+ webmock
79
+
80
+ BUNDLED WITH
81
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Railsware Products Studio, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,127 @@
1
+ [![test](https://github.com/railsware/mailtrap-ruby/actions/workflows/main.yml/badge.svg)](https://github.com/railsware/mailtrap-ruby/actions/workflows/main.yml)
2
+
3
+ # Official Mailtrap Ruby client
4
+
5
+ This Ruby gem offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io).
6
+
7
+ Quickly add email sending functionality to your Ruby application with Mailtrap.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'mailtrap'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install mailtrap
24
+
25
+ ## Usage
26
+
27
+ ### Minimal
28
+
29
+ ```ruby
30
+ require 'mailtrap'
31
+
32
+ # create mail object
33
+ mail = Mailtrap::Sending::Mail.new(
34
+ from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
35
+ to: [
36
+ { email: 'your@email.com' }
37
+ ],
38
+ subject: 'You are awesome!',
39
+ text: "Congrats for sending test email with Mailtrap!"
40
+ )
41
+
42
+ # create client and send
43
+ client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
44
+ client.send(mail)
45
+ ```
46
+
47
+ ### Full
48
+
49
+ ```ruby
50
+ require 'mailtrap'
51
+ require 'base64'
52
+
53
+ mail = Mailtrap::Sending::Mail.new(
54
+ from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
55
+ to: [
56
+ { email: 'your@email.com', name: 'Your name' }
57
+ ],
58
+ cc: [
59
+ { email: 'cc@email.com', name: 'Copy To' }
60
+ ],
61
+ bcc: [
62
+ { email: 'bcc@email.com', name: 'Hidden Recipient' }
63
+ ],
64
+ subject: 'You are awesome!',
65
+ text: "Congrats for sending test email with Mailtrap!",
66
+ category: "Integration Test",
67
+ attachments: [
68
+ {
69
+ content: Base64.encode64('Attachment content'), # base64 encoded content or IO string
70
+ filename: 'attachment.txt'
71
+ }
72
+ ],
73
+ headers: {
74
+ 'X-MT-Header': 'Custom header'
75
+ },
76
+ custom_variables: {
77
+ year: 2022
78
+ }
79
+ )
80
+
81
+ data = File.open('/path/to/image.jpg').read
82
+ encoded = Base64.encode64(data).gsub(/\n/,"")
83
+
84
+ mail.add_attachment(content: encoded, filename: 'image.png')
85
+
86
+ client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
87
+ client.send(mail)
88
+ ```
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
+
111
+ ## Development
112
+
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.
114
+
115
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
116
+
117
+ ## Contributing
118
+
119
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/railsware/mailtrap-ruby). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
124
+
125
+ ## Code of Conduct
126
+
127
+ Everyone interacting in the Mailtrap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,20 +1,12 @@
1
- # -*- ruby -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'rubygems'
4
- require 'hoe'
5
- require './lib/mailtrap.rb'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
6
5
 
7
- Hoe.new('mailtrap', Mailtrap::VERSION ) do |p|
8
- p.rubyforge_name = 'rubymatt'
9
- p.author = 'Matt Mower'
10
- p.email = 'self@mattmower.com'
11
- p.summary = 'Mailtrap is a mock SMTP server for use in Rails development'
12
- p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
- p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
- p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
- p.remote_rdoc_dir = 'mailtrap'
16
- p.extra_deps << ['daemons','>= 1.0.8']
17
- p.extra_deps << ['trollop','>= 1.7']
18
- end
6
+ RSpec::Core::RakeTask.new(:spec)
19
7
 
20
- # vim: syntax=Ruby
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -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,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'json'
5
+
6
+ module Mailtrap
7
+ module Sending
8
+ class Attachment
9
+ attr_accessor :type, :filename, :disposition, :content_id
10
+ attr_reader :content
11
+
12
+ def initialize(content:, filename:, type: nil, disposition: nil, content_id: nil)
13
+ self.content = content
14
+ @type = type
15
+ @filename = filename
16
+ @disposition = disposition
17
+ @content_id = content_id
18
+ end
19
+
20
+ def as_json
21
+ {
22
+ 'content' => content,
23
+ 'type' => type,
24
+ 'filename' => filename,
25
+ 'disposition' => disposition,
26
+ 'content_id' => content_id
27
+ }.compact
28
+ end
29
+
30
+ def content=(content)
31
+ if content.respond_to?(:read)
32
+ @content = encode(content)
33
+ else
34
+ raise AttachmentContentError unless base64?(content)
35
+
36
+ @content = content
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def encode(io)
43
+ string = io.read.encode('UTF-8') unless io.respond_to?(:binmode?) && io.binmode?
44
+ Base64.encode64(string).gsub(/\n/, '')
45
+ end
46
+
47
+ def base64?(string)
48
+ string.is_a?(String) && Base64.strict_encode64(Base64.decode64(string)) == string
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ module Mailtrap
8
+ module Sending
9
+ class Client
10
+ API_HOST = 'send.api.mailtrap.io'
11
+ API_PORT = 443
12
+
13
+ attr_reader :api_key, :api_host, :api_port
14
+
15
+ def initialize(api_key: ENV.fetch('MAILTRAP_API_KEY'), api_host: API_HOST, api_port: API_PORT)
16
+ @api_key = api_key
17
+ @api_host = api_host
18
+ @api_port = api_port
19
+ end
20
+
21
+ def send(mail)
22
+ raise ArgumentError, 'should be Mailtrap::Sending::Mail object' unless mail.is_a? Mailtrap::Sending::Mail
23
+
24
+ request = post_request('/api/send', mail.to_json)
25
+ response = http_client.request(request)
26
+
27
+ handle_response(response)
28
+ end
29
+
30
+ private
31
+
32
+ def http_client
33
+ @http_client ||= Net::HTTP.new(API_HOST, API_PORT).tap { |client| client.use_ssl = true }
34
+ end
35
+
36
+ def post_request(path, body)
37
+ request = Net::HTTP::Post.new(path)
38
+ request.body = body
39
+ request['Authorization'] = "Bearer #{api_key}"
40
+ request['Content-Type'] = 'application/json'
41
+
42
+ request
43
+ end
44
+
45
+ def handle_response(response)
46
+ case response.code
47
+ when '200'
48
+ json_response(response.body)
49
+ when '400'
50
+ raise Mailtrap::Sending::Error, json_response(response.body)[:errors]
51
+ when '401'
52
+ raise Mailtrap::Sending::AuthorizationError, json_response(response.body)[:errors]
53
+ else
54
+ raise Mailtrap::Sending::Error, ['server error']
55
+ end
56
+ end
57
+
58
+ def json_response(body)
59
+ JSON.parse(body, symbolize_names: true)
60
+ end
61
+ end
62
+ end
63
+ 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
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Mailtrap
6
+ module Sending
7
+ class Mail
8
+ attr_accessor :from, :to, :cc, :bcc, :subject, :text, :html, :headers, :category, :custom_variables
9
+ attr_reader :attachments
10
+
11
+ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
12
+ from: nil,
13
+ to: [],
14
+ cc: [],
15
+ bcc: [],
16
+ subject: nil,
17
+ text: nil,
18
+ html: nil,
19
+ attachments: [],
20
+ headers: {},
21
+ category: nil,
22
+ custom_variables: {}
23
+ )
24
+ @from = from
25
+ @to = to
26
+ @cc = cc
27
+ @bcc = bcc
28
+ @subject = subject
29
+ @text = text
30
+ @html = html
31
+ self.attachments = attachments
32
+ @headers = headers
33
+ @category = category
34
+ @custom_variables = custom_variables
35
+ end
36
+
37
+ def as_json # rubocop:disable Metrics/MethodLength
38
+ {
39
+ 'to' => to,
40
+ 'from' => from,
41
+ 'cc' => cc,
42
+ 'bcc' => bcc,
43
+ 'subject' => subject,
44
+ 'html' => html,
45
+ 'text' => text,
46
+ 'attachments' => attachments.map(&:as_json),
47
+ # TODO: update headers and custom_variables with as_json method
48
+ 'headers' => headers,
49
+ 'category' => category,
50
+ 'custom_variables' => custom_variables
51
+ }.compact
52
+ end
53
+
54
+ def to_json(*args)
55
+ JSON.generate(
56
+ as_json,
57
+ *args
58
+ )
59
+ end
60
+
61
+ def attachments=(attachments)
62
+ @attachments = attachments.map { |attachment| Mailtrap::Sending::Attachment.new(**attachment) }
63
+ end
64
+
65
+ def add_attachment(content:, filename:, type: nil, disposition: nil, content_id: nil)
66
+ attachment = Mailtrap::Sending::Attachment.new(
67
+ content: content,
68
+ filename: filename,
69
+ type: type,
70
+ disposition: disposition,
71
+ content_id: content_id
72
+ )
73
+ attachments << attachment
74
+
75
+ attachment
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sending/attachment'
4
+ require_relative 'sending/client'
5
+ require_relative 'sending/convert'
6
+ require_relative 'sending/mail'
7
+
8
+ module Mailtrap
9
+ module Sending
10
+ class AttachmentContentError < StandardError; end
11
+
12
+ class Error < StandardError
13
+ attr_reader :messages
14
+
15
+ def initialize(messages)
16
+ @messages = messages
17
+
18
+ super(messages.join(', '))
19
+ end
20
+ end
21
+
22
+ class AuthorizationError < Error; end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailtrap
4
+ VERSION = '1.1.0'
5
+ end
data/lib/mailtrap.rb CHANGED
@@ -1,130 +1,7 @@
1
+ # frozen_string_literal: true
1
2
 
2
- require 'rubygems'
3
- require 'daemons'
4
- require 'socket'
5
- require 'trollop'
3
+ require_relative 'mailtrap/action_mailer' if defined? ActionMailer
4
+ require_relative 'mailtrap/sending'
5
+ require_relative 'mailtrap/version'
6
6
 
7
- #
8
- # Mailtrap creates a TCP server that listens on a specified port for SMTP
9
- # clients. Accepts the connection and talks just enough of the SMTP protocol
10
- # for them to deliver a message which it writes to disk.
11
- #
12
- class Mailtrap
13
- VERSION = '0.2.1'
14
-
15
- # Create a new Mailtrap on the specified host:port. If once it true it
16
- # will listen for one message then exit. Specify the msgdir where messages
17
- # are written.
18
- def initialize( host, port, once, msgfile )
19
- @host = host
20
- @port = port
21
- @once = once
22
- @msgfile = msgfile
23
-
24
- File.open( @msgfile, "a" ) do |file|
25
- file.puts "\n* Mailtrap started at #{@host}:#{port}\n"
26
- end
27
-
28
- service = TCPServer.new( @host, @port )
29
- accept( service )
30
- end
31
-
32
- # Service one or more SMTP client connections
33
- def accept( service )
34
- while session = service.accept
35
-
36
- class << session
37
- def get_line
38
- line = gets
39
- line.chomp! unless line.nil?
40
- line
41
- end
42
- end
43
-
44
- begin
45
- serve( session )
46
- rescue Exception => e
47
- puts "Erk! #{e.message}"
48
- end
49
-
50
- break if @once
51
- end
52
- end
53
-
54
- # Write a plain text dump of the incoming email to a text
55
- # file. The file will be in the @msgdir folder and will
56
- # be called smtp0001.msg, smtp0002.msg, and so on.
57
- def write( from, to_list, message )
58
-
59
- # Strip SMTP commands from To: and From:
60
- from.gsub!( /MAIL FROM:\s*/, "" )
61
- to_list = to_list.map { |to| to.gsub( /RCPT TO:\s*/, "" ) }
62
-
63
- # Append to the end of the messages file
64
- File.open( @msgfile, "a" ) do |file|
65
- file.puts "* Message begins"
66
- file.puts "From: #{from}"
67
- file.puts "To: #{to_list.join(", ")}"
68
- file.puts "Body:"
69
- file.puts message
70
- file.puts "\n* Message ends"
71
- end
72
-
73
- end
74
-
75
- # Talk pidgeon-SMTP to the client to get them to hand over the message
76
- # and go away.
77
- def serve( connection )
78
- connection.puts( "220 #{@host} MailTrap ready ESTMP" )
79
- helo = connection.get_line # whoever they are
80
- puts "Helo: #{helo}"
81
-
82
- if helo =~ /^EHLO\s+/
83
- puts "Seen an EHLO"
84
- connection.puts "250-#{@host} offers just ONE extension my pretty"
85
- connection.puts "250 HELP"
86
- end
87
-
88
- # Accept MAIL FROM:
89
- from = connection.get_line
90
- connection.puts( "250 OK" )
91
- puts "From: #{from}"
92
-
93
- to_list = []
94
-
95
- # Accept RCPT TO: until we see DATA
96
- loop do
97
- to = connection.get_line
98
- break if to.nil?
99
-
100
- if to =~ /^DATA/
101
- connection.puts( "354 Start your message" )
102
- break
103
- else
104
- puts "To: #{to}"
105
- to_list << to
106
- connection.puts( "250 OK" )
107
- end
108
- end
109
-
110
- # Capture the message body terminated by <CR>.<CR>
111
- lines = []
112
- loop do
113
- line = connection.get_line
114
- break if line.nil? || line == "."
115
- lines << line
116
- puts "+ #{line}"
117
- end
118
-
119
- # We expect the client will go away now
120
- connection.puts( "250 OK" )
121
- connection.gets # Quit
122
- connection.puts "221 Seeya"
123
- connection.close
124
- puts "And we're done with that bozo!"
125
-
126
- write( from, to_list, lines.join( "\n" ) )
127
-
128
- end
129
-
130
- end
7
+ module Mailtrap; end
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,102 +1,68 @@
1
- --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
1
+ --- !ruby/object:Gem::Specification
4
2
  name: mailtrap
5
- version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2007-10-12 00:00:00 +01:00
8
- summary: Mailtrap is a mock SMTP server for use in Rails development
9
- require_paths:
10
- - lib
11
- email: self@mattmower.com
12
- homepage: " by Matt Mower <self@mattmower.com>"
13
- rubyforge_project: rubymatt
14
- description: "Mailtrap is a mock SMTP server for use in Rails development. Mailtrap waits on your choosen port for a client to connect and talks _just enough_ SMTP protocol for ActionMailer to successfully deliver its message. Mailtrap makes *no* attempt to actually deliver messages and, instead, writes them into a file (hence the name \tMail_trap_). Handy tip: use tail -f to see emails being received. You can configure the hostname (default: localhost) and port (default: 2525) for the server and also where the messages get written (default: /var/tmp/mailtrap.log). == FEATURES/PROBLEMS:"
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Railsware Products Studio, Inc.
15
8
  autorequire:
16
- default_executable:
17
9
  bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
25
- platform: ruby
26
- signing_key:
27
- cert_chain:
28
- - |
29
- -----BEGIN CERTIFICATE-----
30
- MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MQ0wCwYDVQQDDARzZWxm
31
- MRkwFwYKCZImiZPyLGQBGRYJbWF0dG1vd2VyMRMwEQYKCZImiZPyLGQBGRYDY29t
32
- MB4XDTA3MTAwMzIzMjkwN1oXDTA4MTAwMjIzMjkwN1owPzENMAsGA1UEAwwEc2Vs
33
- ZjEZMBcGCgmSJomT8ixkARkWCW1hdHRtb3dlcjETMBEGCgmSJomT8ixkARkWA2Nv
34
- bTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMVQejbmmtJD6FfTwc9+
35
- QsWEqNFgNL8+tbrpxUuT8J2j0O0Pvkhw3wGT3tzMdXlnaz+OwUz8qU6HGwDAiK/3
36
- zWAGuBRApkIAooTryRWLYmkZfnZidG8A2msmRwQd84Rkzn6Vgad+46o6jQfJSgy1
37
- pDsZVfsRcUZ0dWetp+ll7Lq/4HyecdNgRfmrQh6pmKwIUmh5cj91iBX3NEqx5TN6
38
- AKkYIOBMuOnBMNrXvApH10ruvus1d/PiJIjAavvbgSih+vyB8NTXTF4kOhKXnF0w
39
- Zx3CF5asJejopRGNUcY9Vmjr7WiRHhUnFjc8hs385MlQNKEdAirVwIIgWfkQwJO+
40
- GQ0CAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFL99
41
- Fuw4VZwExQog/QSr8zIFfj1WMA0GCSqGSIb3DQEBBQUAA4IBAQATHmiqgSFKeJQM
42
- QZjefTiRH6q1TgnJEt0/ylb9gNUCBpIFC1QOtuf68NAt90n6q1CZ51W3EreOPmi2
43
- k2UxzfnPH6av2HQxmC1Xwv+X/eKF65shy/D75XWeCKLwynlXbA0cSjkV8/jSDB9I
44
- pPXX1PjTNmzS2oNGSBroG567Uod41eAjlFA2FQTSWu/lDaYvLBjupGzz59/lZfkl
45
- oCvK+OORDtfJ/a8CdYuD7PltGIEGV9srqdsEsmcrXGHaZt+jGRLYi60h5ZEd7lOm
46
- 1WEvMXOXF3ofp87Yw1MSpG7yQyr0i5DW6VM3v6AP0ABxoM4p80JDHhwAmKWRWWQk
47
- yMQ/kUco
48
- -----END CERTIFICATE-----
49
-
50
- post_install_message:
51
- authors:
52
- - Matt Mower
53
- files:
54
- - History.txt
55
- - Manifest.txt
56
- - README.txt
10
+ cert_chain: []
11
+ date: 2022-08-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Official mailtrap.io API client
14
+ email:
15
+ - support@mailtrap.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - ".ruby-version"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
57
29
  - Rakefile
58
- - bin/mailtrap
59
30
  - lib/mailtrap.rb
60
- test_files:
61
- - test/test_mailtrap.rb
62
- rdoc_options:
63
- - --main
64
- - README.txt
65
- extra_rdoc_files:
66
- - History.txt
67
- - Manifest.txt
68
- - README.txt
69
- executables:
70
- - mailtrap
71
- extensions: []
72
-
31
+ - lib/mailtrap/action_mailer.rb
32
+ - lib/mailtrap/action_mailer/delivery_method.rb
33
+ - lib/mailtrap/action_mailer/railtie.rb
34
+ - lib/mailtrap/sending.rb
35
+ - lib/mailtrap/sending/attachment.rb
36
+ - lib/mailtrap/sending/client.rb
37
+ - lib/mailtrap/sending/convert.rb
38
+ - lib/mailtrap/sending/mail.rb
39
+ - lib/mailtrap/version.rb
40
+ - mailtrap.gemspec
41
+ homepage: https://github.com/railsware/mailtrap-ruby
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/railsware/mailtrap-ruby
46
+ source_code_uri: https://github.com/railsware/mailtrap-ruby
47
+ changelog_uri: https://github.com/railsware/mailtrap-ruby/blob/main/CHANGELOG.md
48
+ rubygems_mfa_required: 'true'
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.7.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
73
63
  requirements: []
74
-
75
- dependencies:
76
- - !ruby/object:Gem::Dependency
77
- name: daemons
78
- version_requirement:
79
- version_requirements: !ruby/object:Gem::Version::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: 1.0.8
84
- version:
85
- - !ruby/object:Gem::Dependency
86
- name: trollop
87
- version_requirement:
88
- version_requirements: !ruby/object:Gem::Version::Requirement
89
- requirements:
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- version: "1.7"
93
- version:
94
- - !ruby/object:Gem::Dependency
95
- name: hoe
96
- version_requirement:
97
- version_requirements: !ruby/object:Gem::Version::Requirement
98
- requirements:
99
- - - ">="
100
- - !ruby/object:Gem::Version
101
- version: 1.3.0
102
- version:
64
+ rubygems_version: 3.1.6
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Official mailtrap.io API client
68
+ test_files: []
data/History.txt DELETED
@@ -1,5 +0,0 @@
1
- == 1.0.0 / 2007-10-03
2
-
3
- * 1 major enhancement
4
- * Birthday!
5
-
data/Manifest.txt DELETED
@@ -1,6 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- bin/mailtrap
6
- lib/mailtrap.rb
data/README.txt DELETED
@@ -1,75 +0,0 @@
1
- mailtrap
2
- by Matt Mower <self@mattmower.com>
3
- http://matt.blogs.it/
4
-
5
- == DESCRIPTION:
6
-
7
- Mailtrap is a mock SMTP server for use in Rails development.
8
-
9
- Mailtrap waits on your choosen port for a client to connect and talks _just enough_ SMTP protocol for ActionMailer to successfully deliver its message.
10
-
11
- Mailtrap makes *no* attempt to actually deliver messages and, instead, writes them into a file (hence the name Mail_trap_). Handy tip: use tail -f to see emails being received.
12
-
13
- You can configure the hostname (default: localhost) and port (default: 2525) for the server and also where the messages get written (default: /var/tmp/mailtrap.log).
14
-
15
- == FEATURES/PROBLEMS:
16
-
17
- * Lightweight, no setup
18
- * Runs as a daemon with start, stop, etc..
19
- * Tested with ActionMailer's SMTP delivery method
20
- * Very, very, dumb ... might not work with an arbitrary SMTP client
21
-
22
- == SYNOPSIS:
23
-
24
- To use the defaults host:localhost, port:2525, file:/var/log/mailtrap.log
25
-
26
- * mailtrap start
27
-
28
- Customise startup:
29
-
30
- * sudo mailtrap start --host my.host --port 25 --once --file=/var/log/messages.txt
31
-
32
- (sudo because you want to use restricted port 25)
33
-
34
- For more info:
35
-
36
- * mailtrap --help (to see Daemonization options)
37
- * mailtrap start --help (to see Mailtrap options)
38
-
39
- == REQUIREMENTS:
40
-
41
- * Hoe rubygem
42
- * Rubygems rubygem
43
- * Daemons rubygem
44
- * Trollop rubygem
45
-
46
- All these are automatically installed if you use gem install -y
47
-
48
- == INSTALL:
49
-
50
- * sudo gem install -y mailtrap
51
-
52
- == LICENSE:
53
-
54
- (The MIT License)
55
-
56
- Copyright (c) 2007 Matt Mower, Software Heretics
57
-
58
- Permission is hereby granted, free of charge, to any person obtaining
59
- a copy of this software and associated documentation files (the
60
- 'Software'), to deal in the Software without restriction, including
61
- without limitation the rights to use, copy, modify, merge, publish,
62
- distribute, sublicense, and/or sell copies of the Software, and to
63
- permit persons to whom the Software is furnished to do so, subject to
64
- the following conditions:
65
-
66
- The above copyright notice and this permission notice shall be
67
- included in all copies or substantial portions of the Software.
68
-
69
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
70
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
71
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
72
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
73
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
74
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
75
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/mailtrap DELETED
@@ -1,28 +0,0 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # Run the mailtrap server
4
- #
5
-
6
- require 'rubygems'
7
- require 'daemons'
8
- require 'mailtrap'
9
-
10
- opts = Trollop::options do
11
- opt :host, "The host SMTP clients will connect to", :default => 'localhost'
12
- opt :port, "The port SMTP clients will connect to", :default => 2525
13
- opt :once, "Whether to terminate after serving the first client", :default => false
14
- opt :file, "File where messages get written", :default => "/var/tmp/mailtrap.log"
15
- end
16
-
17
- options = {
18
- :dir_mode => :normal,
19
- :dir => '/var/tmp',
20
- :multiple => true,
21
- :mode => :exec,
22
- :backtrace => true,
23
- :log_output => true
24
- }
25
-
26
- Daemons.run_proc( 'mailtrap', options ) do
27
- Mailtrap.new( opts[:host], opts[:port], opts[:once], opts[:file] )
28
- end
File without changes
data.tar.gz.sig DELETED
Binary file
metadata.gz.sig DELETED
Binary file