Skip to content

Commit 3f7027c

Browse files
drishlucasfcosta
andauthored
feat: Inbound API (#134)
Co-authored-by: Lucas da Costa <[email protected]>
1 parent a7dd876 commit 3f7027c

File tree

6 files changed

+590
-0
lines changed

6 files changed

+590
-0
lines changed

examples/receiving_emails.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../lib/resend"
4+
5+
raise if ENV["RESEND_API_KEY"].nil?
6+
7+
Resend.api_key = ENV["RESEND_API_KEY"]
8+
9+
puts "=== Listing Received Emails ==="
10+
11+
puts "\nListing all received emails:"
12+
emails = Resend::Emails::Receiving.list
13+
14+
puts "Total emails in response: #{emails[:data].length}"
15+
puts "Has more: #{emails[:has_more]}"
16+
17+
emails[:data].each do |e|
18+
puts " - #{e["id"]}: #{e["subject"]} from #{e["from"]}"
19+
puts " Created: #{e["created_at"]}"
20+
if e["attachments"] && !e["attachments"].empty?
21+
puts " Attachments: #{e["attachments"].length}"
22+
end
23+
end
24+
25+
puts "\n\nListing with limit of 5:"
26+
limited_emails = Resend::Emails::Receiving.list(limit: 5)
27+
28+
puts "Retrieved #{limited_emails[:data].length} emails"
29+
puts "Has more: #{limited_emails[:has_more]}"
30+
31+
# Example of pagination (if you have more emails)
32+
if limited_emails[:has_more] && limited_emails[:data].last
33+
last_id = limited_emails[:data].last["id"]
34+
puts "\n\nGetting next page after ID: #{last_id}"
35+
next_page = Resend::Emails::Receiving.list(limit: 5, after: last_id)
36+
puts "Next page has #{next_page[:data].length} emails"
37+
end
38+
39+
puts "\n\n=== Retrieving Single Received Email ==="
40+
41+
# Use the first email from the list, or specify a known ID
42+
if emails[:data] && emails[:data].first
43+
email_id = emails[:data].first["id"]
44+
else
45+
# Replace with an actual received email ID from your account
46+
email_id = "006e2796-ff6a-4436-91ad-0429e600bf8a"
47+
end
48+
49+
email = Resend::Emails::Receiving.get(email_id)
50+
51+
puts "\nEmail Details:"
52+
puts " ID: #{email[:id]}"
53+
puts " From: #{email[:from]}"
54+
puts " To: #{email[:to].join(', ')}"
55+
puts " Subject: #{email[:subject]}"
56+
puts " Created At: #{email[:created_at]}"
57+
puts " Message ID: #{email[:message_id]}"
58+
59+
if email[:cc] && !email[:cc].empty?
60+
puts " CC: #{email[:cc].join(', ')}"
61+
end
62+
63+
if email[:bcc] && !email[:bcc].empty?
64+
puts " BCC: #{email[:bcc].join(', ')}"
65+
end
66+
67+
if email[:attachments] && !email[:attachments].empty?
68+
puts "\n Attachments:"
69+
email[:attachments].each do |attachment|
70+
puts " - #{attachment["filename"]} (#{attachment["content_type"]})"
71+
puts " ID: #{attachment["id"]}"
72+
puts " Size: #{attachment["size"]} bytes" if attachment["size"]
73+
puts " Content ID: #{attachment["content_id"]}" if attachment["content_id"]
74+
end
75+
76+
puts "\n Listing all attachments for email: #{email[:id]}"
77+
attachments_list = Resend::Attachments::Receiving.list(
78+
email_id: email[:id]
79+
)
80+
81+
puts " Total attachments: #{attachments_list[:data].length}"
82+
puts " Has more: #{attachments_list[:has_more]}"
83+
84+
# Retrieve full attachment details for the first attachment
85+
if email[:attachments].first
86+
first_attachment_id = email[:attachments].first["id"]
87+
puts "\n Retrieving full attachment details for: #{first_attachment_id}"
88+
89+
attachment_details = Resend::Attachments::Receiving.get(
90+
id: first_attachment_id,
91+
email_id: email[:id]
92+
)
93+
94+
puts " Download URL: #{attachment_details[:download_url]}"
95+
puts " Expires At: #{attachment_details[:expires_at]}"
96+
end
97+
end
98+
99+
puts "\n HTML Content:"
100+
puts " #{email[:html][0..100]}..." if email[:html]
101+
102+
puts "\n Text Content:"
103+
puts " #{email[:text]}" if email[:text]

lib/resend.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
require "resend/contacts"
2121
require "resend/domains"
2222
require "resend/emails"
23+
require "resend/emails/receiving"
24+
require "resend/attachments/receiving"
2325
require "resend/topics"
2426

2527
# Rails
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
module Resend
4+
module Attachments
5+
# Module for receiving email attachments API operations
6+
module Receiving
7+
class << self
8+
# Retrieve a single attachment from a received email
9+
#
10+
# @param params [Hash] Parameters for retrieving the attachment
11+
# @option params [String] :id The attachment ID (required)
12+
# @option params [String] :email_id The email ID (required)
13+
# @return [Hash] The attachment object
14+
#
15+
# @example
16+
# Resend::Attachments::Receiving.get(
17+
# id: "2a0c9ce0-3112-4728-976e-47ddcd16a318",
18+
# email_id: "4ef9a417-02e9-4d39-ad75-9611e0fcc33c"
19+
# )
20+
def get(params = {})
21+
attachment_id = params[:id]
22+
email_id = params[:email_id]
23+
24+
path = "emails/receiving/#{email_id}/attachments/#{attachment_id}"
25+
Resend::Request.new(path, {}, "get").perform
26+
end
27+
28+
# List attachments from a received email with optional pagination
29+
#
30+
# @param params [Hash] Parameters for listing attachments
31+
# @option params [String] :email_id The email ID (required)
32+
# @option params [Integer] :limit Maximum number of attachments to return (1-100)
33+
# @option params [String] :after Cursor for pagination (newer attachments)
34+
# @option params [String] :before Cursor for pagination (older attachments)
35+
# @return [Hash] List of attachments with pagination info
36+
#
37+
# @example List all attachments
38+
# Resend::Attachments::Receiving.list(
39+
# email_id: "4ef9a417-02e9-4d39-ad75-9611e0fcc33c"
40+
# )
41+
#
42+
# @example List with custom limit
43+
# Resend::Attachments::Receiving.list(
44+
# email_id: "4ef9a417-02e9-4d39-ad75-9611e0fcc33c",
45+
# limit: 50
46+
# )
47+
#
48+
# @example List with pagination
49+
# Resend::Attachments::Receiving.list(
50+
# email_id: "4ef9a417-02e9-4d39-ad75-9611e0fcc33c",
51+
# limit: 20,
52+
# after: "attachment_id_123"
53+
# )
54+
def list(params = {})
55+
email_id = params[:email_id]
56+
path = "emails/receiving/#{email_id}/attachments"
57+
58+
# Build query parameters, filtering out nil values
59+
query_params = {}
60+
query_params[:limit] = params[:limit] if params[:limit]
61+
query_params[:after] = params[:after] if params[:after]
62+
query_params[:before] = params[:before] if params[:before]
63+
64+
Resend::Request.new(path, query_params, "get").perform
65+
end
66+
end
67+
end
68+
end
69+
end

lib/resend/emails/receiving.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
module Resend
4+
module Emails
5+
# Module for receiving emails API operations
6+
module Receiving
7+
class << self
8+
# Retrieve a single received email
9+
#
10+
# @param email_id [String] The ID of the received email
11+
# @return [Hash] The received email object
12+
#
13+
# @example
14+
# Resend::Emails::Receiving.get("4ef9a417-02e9-4d39-ad75-9611e0fcc33c")
15+
def get(email_id = "")
16+
path = "emails/receiving/#{email_id}"
17+
Resend::Request.new(path, {}, "get").perform
18+
end
19+
20+
# List received emails with optional pagination
21+
#
22+
# @param params [Hash] Optional parameters for pagination
23+
# @option params [Integer] :limit Maximum number of emails to return (1-100)
24+
# @option params [String] :after Cursor for pagination (newer emails)
25+
# @option params [String] :before Cursor for pagination (older emails)
26+
# @return [Hash] List of received emails with pagination info
27+
#
28+
# @example List all received emails
29+
# Resend::Emails::Receiving.list
30+
#
31+
# @example List with custom limit
32+
# Resend::Emails::Receiving.list(limit: 50)
33+
#
34+
# @example List with pagination
35+
# Resend::Emails::Receiving.list(limit: 20, after: "email_id_123")
36+
def list(params = {})
37+
path = Resend::PaginationHelper.build_paginated_path("emails/receiving", params)
38+
Resend::Request.new(path, {}, "get").perform
39+
end
40+
end
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)