mailset 0.7.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6c238ddea05e923d9c95583e40d59d836ad014fc5e034d283d97fc7f20c417b3
4
+ data.tar.gz: 19d3cb34071f58e3488a7dd80a8dd2515af2bb99ca3b4c949ef13741ae420b09
5
+ SHA512:
6
+ metadata.gz: 5b25702d83098ae036027b27bfc1de0212bcb1a30d89acbb09564c6161dba3210a6e0125e9e529046d201b96d3d15cb5cbb9c13905b29a0f313e4c8991ffa24d
7
+ data.tar.gz: afdfdfd311927a7941277cc9d6528a5b578134a2e927406c447e6522e77bce8f0e11b8a850926cb7e4417666b013e44414f8fa198c4cce4cf038e98922d99405
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Mailset SDK License
2
+
3
+ Copyright (c) 2026 Intenseloop. All rights reserved.
4
+
5
+ You may use this software, and change it, in your own applications and
6
+ services, including in their builds, images and bundles, to call Mailset's
7
+ API (mailset.app). Nothing else is granted.
8
+
9
+ You may not publish, sell, sublicense or redistribute this software, changed
10
+ or not, on its own or as part of another library, SDK or package, and you may
11
+ not use it with any service other than Mailset.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED. IN NO EVENT SHALL INTENSELOOP BE LIABLE FOR ANY CLAIM, DAMAGES OR
15
+ OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
16
+ ITS USE.
17
+
18
+ Questions and issues: mailset.app/support
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # mailset
2
+
3
+ Mailset's official Ruby SDK, for Ruby 3.0 and later. Standard library only.
4
+
5
+ ```sh
6
+ gem install mailset # or: bundle add mailset
7
+ ```
8
+
9
+ ```ruby
10
+ require 'mailset'
11
+
12
+ mailset = Mailset::Client.new # reads MAILSET_KEY
13
+ mailset.send_email(
14
+ from: 'Acme <hello@example.com>',
15
+ to: 'ana@example.com',
16
+ subject: 'Your Acme code',
17
+ text: 'Your code is 123456.'
18
+ )
19
+ ```
20
+
21
+ Every public command is a method too: `mailset.domains.list`. Ruby calls send `send_email` and `send_emails`, and the `messages.send` command `messages.send_message`, since `send` is a method every Ruby object has.
22
+
23
+ - Docs: https://mailset.app/docs/sdk
24
+ - API reference: https://mailset.app/docs/api
25
+
26
+ Questions and issues: mailset.app/support
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailset
4
+ # An approval waiting for a passkey: open {#url} on a device with the
5
+ # passkey (or send it to whoever approves), then pass {#id} as `approval:`.
6
+ class Approval < Model
7
+ # @!attribute [r] id
8
+ # Pass it as `approval:` once approved.
9
+ # @return [String]
10
+ field :id, 'id', :string, required: true
11
+
12
+ # @!attribute [r] url
13
+ # Where whoever approves opens it, on a device with their passkey.
14
+ # @return [String]
15
+ field :url, 'url', :string, required: true
16
+
17
+ # @!attribute [r] expires_at
18
+ # When it stops waiting, in ms since the epoch.
19
+ # @return [Integer]
20
+ field :expires_at, 'expiresAt', :integer, required: true
21
+
22
+ # @!attribute [r] purpose
23
+ # What it approves.
24
+ # @return [Mailset::StepUpPurpose]
25
+ field :purpose, 'purpose', :StepUpPurpose, required: true
26
+ end
27
+
28
+ # Approvals for commands that need a passkey: `mailset.approvals`. Only an
29
+ # API token can ask for one, since a person approves it. A client made with
30
+ # `on_approval:` does all of this itself.
31
+ #
32
+ # rescue Mailset::PasskeyRequiredError => e
33
+ # approval = mailset.approvals.request(e.purpose)
34
+ # puts "Approve it: #{approval.url}"
35
+ # id = mailset.approvals.wait(approval)
36
+ # mailset.tokens.create(name: 'CI', request_options: { approval: id })
37
+ class Approvals
38
+ # @api private
39
+ def initialize(transport)
40
+ @transport = transport
41
+ end
42
+
43
+ # Opens an approval for what a `step_up_required` error named.
44
+ # @param purpose [Mailset::StepUpPurpose, Hash] {Error#purpose}
45
+ # @return [Mailset::Approval]
46
+ def request(purpose)
47
+ if @transport.credential.kind == :key
48
+ raise PermissionError.new(
49
+ 'An approval needs an API token (MAILSET_TOKEN): a person approves it with their passkey, ' \
50
+ 'and an app key is no person.',
51
+ needs: 'MAILSET_TOKEN'
52
+ )
53
+ end
54
+ body = Types.dump(:StepUpPurpose, purpose)
55
+ started = @transport.raw('POST', '/v1/approvals', body)
56
+ unless started.is_a?(Hash) && started['id'].is_a?(String) && started['approveUrl'].is_a?(String) &&
57
+ started['expiresAt'].is_a?(Integer)
58
+ raise ServerError.new('The Mailset API can’t be reached.', code: 'unavailable')
59
+ end
60
+
61
+ Approval.new(id: started['id'], url: started['approveUrl'], expires_at: started['expiresAt'],
62
+ purpose: StepUpPurpose.from_json(body))
63
+ end
64
+
65
+ # Waits until it's approved, checking every `every` seconds, and answers
66
+ # the id to pass as `approval:`. Raises {PermissionError} when it's
67
+ # refused or expires, and {ExpiredError} after `timeout` seconds.
68
+ # @param approval [Mailset::Approval, String]
69
+ # @return [String]
70
+ def wait(approval, timeout: 600, every: 2)
71
+ id = approval.is_a?(Approval) ? approval.id : approval.to_s
72
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
73
+ while Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
74
+ view = @transport.raw('GET', "/v1/approvals/#{escape(id)}", nil)
75
+ state = view.is_a?(Hash) ? view['state'] : nil
76
+ return id if state == 'approved'
77
+ raise PermissionError, 'That approval was refused.' if state == 'denied'
78
+ raise PermissionError, 'That approval expired before anyone approved it.' if state == 'expired'
79
+
80
+ @transport.pause(every)
81
+ end
82
+ raise ExpiredError, 'Nobody approved it in time.'
83
+ end
84
+
85
+ def inspect
86
+ '#<Mailset::Approvals>'
87
+ end
88
+
89
+ private
90
+
91
+ # As JavaScript's encodeURIComponent writes it.
92
+ def escape(text)
93
+ text.b.gsub(%r{[^A-Za-z0-9\-_.!~*'()]}n) { |char| format('%%%02X', char.ord) }
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailset
4
+ # Base64 as Mailset writes it, with Array#pack instead of the base64 gem
5
+ # (a bundled gem from Ruby 3.4, so it would need a Gemfile line).
6
+ #
7
+ # @api private
8
+ module Base64url
9
+ module_function
10
+
11
+ # Standard base64 on one line, as `messages.submit` takes `data`.
12
+ def strict_encode(bytes)
13
+ [bytes].pack('m0')
14
+ end
15
+
16
+ # Standard base64, or nil when the text isn't.
17
+ def strict_decode(text)
18
+ text.unpack1('m0')
19
+ rescue ArgumentError
20
+ nil
21
+ end
22
+
23
+ # base64url with no padding: a key, a proof, a digest.
24
+ def encode(bytes)
25
+ strict_encode(bytes).tr('+/', '-_').delete('=')
26
+ end
27
+
28
+ # base64url with or without padding, or nil when the text isn't.
29
+ def decode(text)
30
+ return nil unless text.is_a?(String) && text.match?(/\A[A-Za-z0-9_-]*={0,2}\z/)
31
+
32
+ body = text.delete('=').tr('-_', '+/')
33
+ return nil if body.length % 4 == 1
34
+
35
+ strict_decode(body + ('=' * ((4 - (body.length % 4)) % 4)))
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailset
4
+ module Resources
5
+ # `mailset.campaigns`: campaign mail, from a campaigns key
6
+ # (`mailset keys create --campaigns`).
7
+ class Campaigns < Base
8
+ # The most messages one `campaigns.submit` takes.
9
+ MAX_MESSAGES = 100
10
+
11
+ # Sends a campaign's messages, one per recipient, from a sending domain
12
+ # (`campaigns.submit`). Each message is a Hash of {Client#send_email}'s
13
+ # fields (`from:`, `to:`, `subject:`, `text:`, `html:`, `attachments:`,
14
+ # `headers:`…), built into the message here, never base64 from you; or
15
+ # `raw:`, a whole message you built yourself, as text. Beside them:
16
+ #
17
+ # - `campaign:` your own id for the campaign: 1 to 64 letters, digits, - or _.
18
+ # - `recipient:` your own id for the recipient, in the same form.
19
+ # - `track:` `{ opens:, clicks: }`, both false unless told. Only a domain
20
+ # with a tracking domain can track.
21
+ #
22
+ # Both ids come back on every event. Any number of messages: past 100
23
+ # they go in several calls, and the results stay in order.
24
+ #
25
+ # @param domain [String] the sending domain the messages go from.
26
+ # @param messages [Array<Hash>]
27
+ # @param request_options [Hash] `org:`, `approval:`, `timeout:` for each call.
28
+ # @return [Mailset::CampaignSubmitted] one result per message, in order:
29
+ # {Mailset::CampaignResultQueued} or {Mailset::CampaignResultRefused} with why.
30
+ def submit(domain:, messages:, request_options: {})
31
+ now = transport.now_ms
32
+ prepared = messages.map do |message|
33
+ fields = Mime.fields_of(message, %i[raw campaign recipient track])
34
+ {
35
+ 'raw' => Mime.base64_of(fields, now), 'campaign' => fields[:campaign], 'recipient' => fields[:recipient],
36
+ 'track' => track_of(fields[:track])
37
+ }
38
+ end
39
+ results = []
40
+ prepared.each_slice(MAX_MESSAGES) do |slice|
41
+ answer = transport.command('campaigns.submit', { 'domain' => domain, 'messages' => slice },
42
+ **call_options(request_options))
43
+ unless answer.is_a?(Hash) && answer['results'].is_a?(Array)
44
+ raise ServerError.new('The Mailset API can’t be reached.', code: 'unavailable', command: 'campaigns.submit')
45
+ end
46
+
47
+ results.concat(answer['results'])
48
+ end
49
+ CampaignSubmitted.from_json('results' => results)
50
+ end
51
+
52
+ private
53
+
54
+ def track_of(track)
55
+ return { 'opens' => false, 'clicks' => false } if track.nil?
56
+
57
+ given = track.to_h.transform_keys(&:to_sym)
58
+ { 'opens' => given[:opens] ? true : false, 'clicks' => given[:clicks] ? true : false }
59
+ end
60
+ end
61
+
62
+ # `mailset.sending`: mail from a sending domain.
63
+ class Sending < Base
64
+ # Sends one message from a sending domain, the way a reply goes, never
65
+ # tracked (`sending.submit`). Give its fields as {Client#send_email}
66
+ # takes them, built into the message here; or `raw:`, a whole message
67
+ # you built yourself, as text. Answers each envelope address: `queued`,
68
+ # or refused with why.
69
+ #
70
+ # @param domain [String] the sending domain the message goes from.
71
+ # @param ref [String] your own id for the message: 1 to 64 letters,
72
+ # digits, - or _. It comes back on its events.
73
+ # @param recipients [Array<String>, nil] who it goes to. Left out,
74
+ # everyone it names in To, Cc and Bcc.
75
+ # @param raw [String, nil] a whole message built elsewhere, instead of its fields.
76
+ # @param request_options [Hash] `org:`, `approval:`, `timeout:` for this call.
77
+ # @return [Mailset::SendingSubmitted]
78
+ def submit(domain:, ref:, recipients: nil, raw: nil, from: nil, to: nil, cc: nil, bcc: nil, reply_to: nil,
79
+ subject: nil, text: nil, html: nil, attachments: nil, headers: nil, request_options: {})
80
+ fields = { raw: raw, from: from, to: to, cc: cc, bcc: bcc, reply_to: reply_to, subject: subject, text: text,
81
+ html: html, attachments: attachments, headers: headers }.compact
82
+ input = { 'domain' => domain, 'ref' => ref, 'raw' => Mime.base64_of(fields, transport.now_ms) }
83
+ input['recipients'] = recipients.map(&:to_s) unless recipients.nil?
84
+ SendingSubmitted.from_json(transport.command('sending.submit', input, **call_options(request_options)))
85
+ end
86
+ end
87
+ end
88
+ end