Skip to content

Commit e15d638

Browse files
authored
Merge pull request #81 from mailtrap/MT-19854-sending-domains
Add Sending domains API
2 parents 6e18102 + 72c3e00 commit e15d638

19 files changed

Lines changed: 1129 additions & 3 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ jobs:
99
build:
1010
runs-on: ubuntu-latest
1111
name: Ruby ${{ matrix.ruby }}
12+
env:
13+
BUNDLE_PATH: vendor/bundle
1214
strategy:
1315
matrix:
1416
ruby:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Add Sending Domains API
2+
13
## [2.6.0] - 2026-01-27
24
- Add Inboxes API
35
- Add Projects API

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ mail(delivery_method: :mailtrap_bulk)
173173
Refer to the [`examples`](examples) folder for more examples:
174174

175175
Email API:
176-
- Full email sending – [`full.rb`](examples/full.rb)
177176

178-
- Batch sending – [`batch.rb`](examples/batch.rb)
177+
- Full Email Sending – [`full.rb`](examples/full.rb)
178+
- Batch Sending – [`batch.rb`](examples/batch.rb)
179+
- Sending Domains API – [`sending_domains_api.rb`](examples/sending_domains_api.rb)
179180

180181
Email Sandbox (Testing):
181182

@@ -184,7 +185,7 @@ Email Sandbox (Testing):
184185

185186
Contact management:
186187

187-
- Contacts CRUD & listing[`contacts_api.rb`](examples/contacts_api.rb)
188+
- Contacts CRUD & Listing[`contacts_api.rb`](examples/contacts_api.rb)
188189

189190
General API:
190191

examples/sending_domains_api.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'mailtrap'
2+
3+
account_id = 3229
4+
client = Mailtrap::Client.new(api_key: 'your-api-key')
5+
sending_domains = Mailtrap::SendingDomainsAPI.new(account_id, client)
6+
7+
# Create new Sending Domain
8+
sending_domain = sending_domains.create(domain_name: 'example.com')
9+
# => #<struct Mailtrap::SendingDomain id=1, domain_name="example.com">
10+
11+
# Get all Sending Domains
12+
sending_domains.list
13+
# => [#<struct Mailtrap::SendingDomain id=1, domain_name="example.com">]
14+
15+
# Get sending domain
16+
sending_domain = sending_domains.get(sending_domain.id)
17+
# => #<struct Mailtrap::SendingDomain id=1, domain_name="example.com">
18+
19+
# Send setup email
20+
sending_domains.send_setup_instructions(sending_domain.id, 'jonathan@mail.com')
21+
# => nil
22+
23+
# Delete sending domain
24+
sending_domains.delete(sending_domain.id)
25+
# => nil

lib/mailtrap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require_relative 'mailtrap/suppressions_api'
1313
require_relative 'mailtrap/projects_api'
1414
require_relative 'mailtrap/inboxes_api'
15+
require_relative 'mailtrap/sending_domains_api'
1516

1617
module Mailtrap
1718
# @!macro api_errors

lib/mailtrap/sending_domain.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
module Mailtrap
4+
# Data Transfer Object for Sending Domain
5+
# @see https://docs.mailtrap.io/developers/management/sending-domains
6+
# @attr_reader id [Integer] The sending domain ID
7+
# @attr_reader domain_name [String] The sending domain name
8+
# @attr_reader demo [Boolean] Whether the sending domain is a demo domain
9+
# @attr_reader compliance_status [String] The compliance status of the sending domain
10+
# @attr_reader dns_verified [Boolean] Whether the DNS records are verified
11+
# @attr_reader dns_verified_at [String, nil] The timestamp when DNS was verified
12+
# @attr_reader dns_records [Array] The DNS records for the sending domain
13+
# @attr_reader open_tracking_enabled [Boolean] Whether open tracking is enabled
14+
# @attr_reader click_tracking_enabled [Boolean] Whether click tracking is enabled
15+
# @attr_reader auto_unsubscribe_link_enabled [Boolean] Whether auto unsubscribe link is enabled
16+
# @attr_reader custom_domain_tracking_enabled [Boolean] Whether custom domain tracking is enabled
17+
# @attr_reader health_alerts_enabled [Boolean] Whether health alerts are enabled
18+
# @attr_reader critical_alerts_enabled [Boolean] Whether critical alerts are enabled
19+
# @attr_reader alert_recipient_email [String, nil] The email address for alert recipients
20+
# @attr_reader permissions [Hash] The permissions for the sending domain
21+
#
22+
SendingDomain = Struct.new(
23+
:id,
24+
:domain_name,
25+
:demo,
26+
:compliance_status,
27+
:dns_verified,
28+
:dns_verified_at,
29+
:dns_records,
30+
:open_tracking_enabled,
31+
:click_tracking_enabled,
32+
:auto_unsubscribe_link_enabled,
33+
:custom_domain_tracking_enabled,
34+
:health_alerts_enabled,
35+
:critical_alerts_enabled,
36+
:alert_recipient_email,
37+
:permissions,
38+
:created_at,
39+
:updated_at,
40+
keyword_init: true
41+
)
42+
end
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'base_api'
4+
require_relative 'sending_domain'
5+
6+
module Mailtrap
7+
class SendingDomainsAPI
8+
include BaseAPI
9+
10+
self.supported_options = %i[domain_name]
11+
12+
self.response_class = SendingDomain
13+
14+
# Lists all sending domains for the account
15+
# @return [Array<SendingDomain>] Array of sending domains
16+
# @!macro api_errors
17+
def list
18+
response = client.get(base_path)
19+
response[:data].map { |item| handle_response(item) }
20+
end
21+
22+
# Retrieves a specific sending domain
23+
# @param domain_id [Integer] The sending domain ID
24+
# @return [SendingDomain] Sending domain object
25+
# @!macro api_errors
26+
def get(domain_id)
27+
base_get(domain_id)
28+
end
29+
30+
# Creates a new sending domain
31+
# @param [Hash] options The parameters to create
32+
# @option options [String] :domain_name The sending domain name
33+
# @return [SendingDomain] Created sending domain
34+
# @!macro api_errors
35+
# @raise [ArgumentError] If invalid options are provided
36+
def create(options)
37+
base_create(options)
38+
end
39+
40+
# Deletes a sending domain
41+
# @param domain_id [Integer] The sending domain ID
42+
# @return nil
43+
# @!macro api_errors
44+
def delete(domain_id)
45+
base_delete(domain_id)
46+
end
47+
48+
# Email DNS configuration instructions for the sending domain
49+
# @param domain_id [Integer] The sending domain ID
50+
# @param email [String] The email for instructions
51+
# @return nil
52+
# @!macro api_errors
53+
def send_setup_instructions(domain_id:, email:)
54+
client.post("#{base_path}/#{domain_id}/send_setup_instructions", email:)
55+
end
56+
57+
private
58+
59+
def base_path
60+
"/api/accounts/#{account_id}/sending_domains"
61+
end
62+
63+
def wrap_request(options)
64+
{ sending_domain: options }
65+
end
66+
end
67+
end

spec/fixtures/vcr_cassettes/Mailtrap_SendingDomainsAPI/_create/maps_response_data_to_sending_domain_object.yml

Lines changed: 80 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_SendingDomainsAPI/_create/when_API_returns_an_error/raises_a_Mailtrap_Error.yml

Lines changed: 78 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)