Skip to content

Commit bc076ee

Browse files
authored
Merge pull request #83 from mailtrap/MT-19858-sandbox-attachments
Add Sandbox Attachments API
2 parents c773033 + 93b6f29 commit bc076ee

File tree

14 files changed

+539
-1
lines changed

14 files changed

+539
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
- Add Sandbox Messages API
22
- Add Sending Domains API
3+
- Add Sandbox Attachments API
34

45
## [2.6.0] - 2026-01-27
56
- Add Inboxes API

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Email Sandbox (Testing):
183183
- Projects CRUD – [`projects_api.rb`](examples/projects_api.rb)
184184
- Inboxes CRUD - [`inboxes_api.rb`](examples/inboxes_api.rb)
185185
- Sandbox Messages CRUD - [`sandbox_messages_api.rb`](examples/sandbox_messages_api.rb)
186+
- Sandbox Attachments API - [`sandbox_attachments_api.rb`](examples/sandbox_attachments_api.rb)
186187

187188
Contact management:
188189

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'mailtrap'
2+
3+
account_id = 3229
4+
inbox_id = 12
5+
message_id = 123
6+
client = Mailtrap::Client.new(api_key: 'your-api-key')
7+
sandbox_attachments = Mailtrap::SandboxAttachmentsAPI.new(account_id, inbox_id, message_id, client)
8+
9+
# List all attachments for a sandbox message (up to 30)
10+
sandbox_attachments.list
11+
# => [#<struct Mailtrap::SandboxAttachment id=1, filename="document.pdf", ...>, ...]
12+
13+
# Get a specific attachment by ID
14+
attachment_id = 456
15+
sandbox_attachments.get(attachment_id)
16+
# => #<struct Mailtrap::SandboxAttachment id=456, message_id=123, filename="document.pdf",
17+
# attachment_type="attachment", content_type="application/pdf", content_id=nil,
18+
# transfer_encoding="base64", attachment_size=1024, created_at="...", updated_at="...",
19+
# attachment_human_size="1 KB", download_path="/path/to/download", ...>

lib/mailtrap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require_relative 'mailtrap/projects_api'
1515
require_relative 'mailtrap/inboxes_api'
1616
require_relative 'mailtrap/sandbox_messages_api'
17+
require_relative 'mailtrap/sandbox_attachments_api'
1718

1819
module Mailtrap
1920
# @!macro api_errors

lib/mailtrap/sandbox_attachment.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module Mailtrap
4+
# Data Transfer Object for SandboxAttachment
5+
# @see https://docs.mailtrap.io/developers/email-sandbox/email-sandbox-api/attachments
6+
# @attr_reader id [Integer] The attachment ID
7+
# @attr_reader message_id [Integer] The message ID
8+
# @attr_reader filename [String] The attachment filename
9+
# @attr_reader attachment_type [String] The attachment type
10+
# @attr_reader content_type [String] The attachment content type
11+
# @attr_reader content_id [String] The attachment content ID
12+
# @attr_reader transfer_encoding [String] The attachment transfer encoding
13+
# @attr_reader attachment_size [Integer] The attachment size in bytes
14+
# @attr_reader created_at [String] The attachment creation timestamp
15+
# @attr_reader updated_at [String] The attachment update timestamp
16+
# @attr_reader attachment_human_size [String] The attachment size in human-readable format
17+
# @attr_reader download_path [String] The attachment download path
18+
#
19+
SandboxAttachment = Struct.new(
20+
:id,
21+
:message_id,
22+
:filename,
23+
:attachment_type,
24+
:content_type,
25+
:content_id,
26+
:transfer_encoding,
27+
:attachment_size,
28+
:created_at,
29+
:updated_at,
30+
:attachment_human_size,
31+
:download_path,
32+
keyword_init: true
33+
)
34+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'base_api'
4+
require_relative 'sandbox_attachment'
5+
6+
module Mailtrap
7+
class SandboxAttachmentsAPI
8+
include BaseAPI
9+
10+
attr_reader :account_id, :inbox_id, :sandbox_message_id, :client
11+
12+
self.response_class = SandboxAttachment
13+
14+
# @param account_id [Integer] The account ID
15+
# @param inbox_id [Integer] The inbox ID
16+
# @param sandbox_message_id [Integer] The message ID
17+
# @param client [Mailtrap::Client] The client instance
18+
# @raise [ArgumentError] If account_id is nil
19+
# @raise [ArgumentError] If inbox_id is nil
20+
def initialize(account_id, inbox_id, sandbox_message_id, client = Mailtrap::Client.new)
21+
raise ArgumentError, 'inbox_id is required' if inbox_id.nil?
22+
raise ArgumentError, 'sandbox_message_id is required' if sandbox_message_id.nil?
23+
24+
@inbox_id = inbox_id
25+
@sandbox_message_id = sandbox_message_id
26+
27+
super(account_id, client)
28+
end
29+
30+
# Retrieves a specific sandbox attachment
31+
# @param sandbox_attachment_id [Integer] The sandbox attachment ID
32+
# @return [SandboxAttachment] Sandbox attachment object
33+
# @!macro api_errors
34+
def get(sandbox_attachment_id)
35+
base_get(sandbox_attachment_id)
36+
end
37+
38+
# Lists all sandbox attachments for a message, limited up to 30 at once
39+
# @return [Array<SandboxAttachment>] Array of sandbox attachment objects
40+
# @!macro api_errors
41+
def list
42+
base_list
43+
end
44+
45+
private
46+
47+
def base_path
48+
"/api/accounts/#{account_id}/inboxes/#{inbox_id}/messages/#{sandbox_message_id}/attachments"
49+
end
50+
end
51+
end

lib/mailtrap/sandbox_message.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,10 @@ module Mailtrap
5050
:blacklists_report_info,
5151
:smtp_information,
5252
keyword_init: true
53-
)
53+
) do
54+
# @return [Boolean] Whether the message has been read
55+
def read?
56+
is_read
57+
end
58+
end
5459
end

spec/fixtures/vcr_cassettes/Mailtrap_SandboxAttachmentsAPI/_get/maps_response_data_to_SandboxAttachment_object.yml

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/Mailtrap_SandboxAttachmentsAPI/_get/when_attachment_does_not_exist/raises_not_found_error.yml

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/Mailtrap_SandboxAttachmentsAPI/_list/maps_response_data_to_SandboxAttachment_objects.yml

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)