-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathemail_logs_api.rb
More file actions
54 lines (45 loc) · 2.11 KB
/
email_logs_api.rb
File metadata and controls
54 lines (45 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'time'
require 'mailtrap'
account_id = 3229
client = Mailtrap::Client.new(api_key: 'your-api-key')
email_logs = Mailtrap::EmailLogsAPI.new(account_id, client)
# Set your API credentials as environment variables
# export MAILTRAP_API_KEY='your-api-key'
# export MAILTRAP_ACCOUNT_ID=your-account-id
#
# email_logs = Mailtrap::EmailLogsAPI.new
# List email logs (first page)
response = email_logs.list
# => #<struct Mailtrap::EmailLogsListResponse
# messages=[#<struct Mailtrap::EmailLogMessage message_id="...", status="delivered", ...>, ...],
# total_count=150,
# next_page_cursor="b2c3d4e5-f6a7-8901-bcde-f12345678901">
response.messages.each { |m| puts "#{m.message_id} #{m.status} #{m.subject}" }
# List with filters (date range: last 2 days, recipient, status)
sent_after = (Time.now.utc - (2 * 24 * 3600)).iso8601
sent_before = Time.now.utc.iso8601
filters = {
sent_after: sent_after,
sent_before: sent_before,
subject: { operator: 'not_empty' },
to: { operator: 'ci_equal', value: 'recipient@example.com' },
category: { operator: 'equal', value: %w[Newsletter Alert] }
}
response = email_logs.list(filters: filters)
# List next page using cursor from previous response (keep same filters)
response = email_logs.list(filters: filters, search_after: response.next_page_cursor) if response.next_page_cursor
# Alternative: iterate all pages with list_each (no manual cursor handling)
email_logs.list_each(filters: filters) { |m| puts "#{m.message_id} #{m.status} #{m.subject}" }
# Or as enumerator:
email_logs.list_each(filters: filters).each { |m| puts m.message_id }
# Get a single message by ID (includes events and raw_message_url)
message_id = response.messages.first&.message_id
if message_id
message = email_logs.get(message_id)
# => #<struct Mailtrap::EmailLogMessage
# message_id="a1b2c3d4-...", status="delivered", subject="Welcome", ...,
# raw_message_url="https://storage.../signed/eml/...",
# events=[#<struct Mailtrap::EmailLogEvent event_type="delivery", ...>, ...]>
puts message.raw_message_url
message.events&.each { |e| puts "#{e.event_type} at #{e.created_at}" }
end