Class: Mailtrap::EmailLogsAPI

Inherits:
Object
  • Object
show all
Includes:
BaseAPI
Defined in:
lib/mailtrap/email_logs_api.rb

Instance Attribute Summary

Attributes included from BaseAPI

#account_id, #client

Instance Method Summary collapse

Methods included from BaseAPI

included, #initialize

Instance Method Details

#get(sending_message_id) ⇒ EmailLogMessage

Fetches a single email log message by ID.

Parameters:

  • sending_message_id (String)

    Message UUID

Returns:

  • (EmailLogMessage)

    Message with events and raw_message_url when available

Raises:



63
64
65
# File 'lib/mailtrap/email_logs_api.rb', line 63

def get(sending_message_id)
  base_get(sending_message_id)
end

#list(filters: nil, search_after: nil) ⇒ EmailLogsListResponse

Lists email logs with optional filters and cursor-based pagination.

Parameters:

  • filters (Hash, nil) (defaults to: nil)

    Optional filters. Top-level date keys use string values (ISO 8601); other keys use { operator:, value: }. value can be a single value or an Array for operators that accept multiple values (e.g. equal, not_equal, ci_equal, ci_not_equal). Examples: { sent_after: “2025-01-01T00:00:00Z”, sent_before: “2025-01-31T23:59:59Z” } { to: { operator: “ci_equal”, value: “[email protected]” } } { category: { operator: “equal”, value: [“Welcome Email”, “Transactional Email”] } }

  • search_after (String, nil) (defaults to: nil)

    Message UUID cursor for the next page (from previous next_page_cursor)

Returns:

Raises:



27
28
29
30
31
32
33
# File 'lib/mailtrap/email_logs_api.rb', line 27

def list(filters: nil, search_after: nil)
  query_params = build_list_query_params(filters:, search_after:)

  response = client.get(base_path, query_params)

  build_list_response(response)
end

#list_each(filters: nil) {|EmailLogMessage| ... } ⇒ Enumerator<EmailLogMessage>

Iterates over all email log messages matching the filters, automatically fetching each page. Use this when you want to process every message without manually handling next_page_cursor.

Parameters:

  • filters (Hash, nil) (defaults to: nil)

    Same as list

Yields:

  • (EmailLogMessage)

    Gives each message from every page when a block is given.

Returns:

  • (Enumerator<EmailLogMessage>)

    if no block given; otherwise the result of the block

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mailtrap/email_logs_api.rb', line 42

def list_each(filters: nil, &block)
  first_page = nil
  fetch_first_page = -> { first_page ||= list(filters: filters) }
  enum = Enumerator.new(-> { fetch_first_page.call.total_count }) do |yielder|
    response = fetch_first_page.call
    loop do
      response.messages.each { |message| yielder << message }
      break if response.next_page_cursor.nil?

      response = list(filters: filters, search_after: response.next_page_cursor)
    end
  end

  block ? enum.each(&block) : enum
end