Skip to content

Messages

tomek-ac edited this page Jun 22, 2022 · 8 revisions

For these API requests you will need to use a server API token. Once you obtain it, you will need to use server API client.

server_token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
client = Postmark::ApiClient.new(<server token>, http_open_timeout: 15)

List sent messages

client.get_messages(count: 1, offset: 0)

# => [{:message_id=>"41f03342-xxxx-xxxx-xxxx-558caedb5e82", :to=>[{"Email"=>"[email protected]", "Name"=>nil}], :cc=>[], :bcc=>[], :recipients=>["[email protected]"], :received_at=>"2014-01-15T16:41:22.4533537-05:00", :from=>"\"Postmark\" <[email protected]>", :subject=>"Good Luck With The Gem", :attachments=>[]}] 

Retrieve single message details

Use get_message to get details for a specific message using ID:

client.get_message('41f03342-xxxx-xxxx-xxxx-558caedb5e82')

# => {:text_body=>"...", :body=>"...", :message_id=>"41f03342-xxxx-xxxx-xxxx-558caedb5e82", :to=>[{"Email"=>"[email protected]", "Name"=>nil}], :cc=>[], :bcc=>[], :recipients=>["[email protected]"], :received_at=>"2014-01-15T16:41:22.4533537-05:00", :from=>"\"Postmark\" <[email protected]>", :subject=>"Good Luck With The Gem", :attachments=>[]}

Retriveve single message dump content

Use #dump_message to get the full message body:

client.dump_message('41f03342-xxxx-xxxx-xxxx-558caedb5e82')
# => {:body=>"..."}

There is also a handy #messages enumerator allowing you to easily manipulate big data arrays.

client.messages.lazy.select { |m| DateTime.parse(m[:received_at]).day.even? }.first(5)
# => [{...}, {...}]

Get message opens

Requires v1.3.0 or above. Use #get_opens to retrieve opens (:count and :offset parameters control pagination).

client.get_opens(count: 2, offset: 0)

# => [{:client=>{"Name"=>"Gmail", "Company"=>"Gmail", "Family"=>"Gmail"}, :os=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :platform=>"Unknown", :user_agent=>"Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (via ggpht.com GoogleImageProxy)", :read_seconds=>0, :geo=>{}, :message_id=>"5f4d6322-xxxx-xxxx-xxxx-9c9fe9b82dd2", :received_at=>"2014-06-05T09:50:15.3895163-04:00", :tag=>"welcome", :recipient=>"[email protected]"}, {:client=>{"Name"=>"Gmail", "Company"=>"Gmail", "Family"=>"Gmail"}, :os=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :platform=>"Unknown", :user_agent=>"Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (via ggpht.com GoogleImageProxy)", :read_seconds=>0, :geo=>{}, :message_id=>"010d1ed3-xxxx-xxxx-xxxx-93b8ec2633e1", :received_at=>"2014-06-05T09:15:10.313638-04:00", :tag=>"welcome-user", :recipient=>"[email protected]"}] 

Get single message opens

Use get_opens_by_message_id to access opens related to a specific message.

client.get_opens_by_message_id('41f03342-xxxx-xxxx-xxxx-558caedb5e82', count: 1, offset: 0)

# => [{:client=>{"Name"=>"Gmail", "Company"=>"Gmail", "Family"=>"Gmail"}, :os=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :platform=>"Unknown", :user_agent=>"Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (via ggpht.com GoogleImageProxy)", :read_seconds=>0, :geo=>{}, :message_id=>"5f4d6322-xxxx-xxxx-xxxx-9c9fe9b82dd2", :received_at=>"2014-06-05T09:50:15.3895163-04:00", :tag=>"welcome", :recipient=>"[email protected]"}, {:client=>{"Name"=>"Gmail", "Company"=>"Gmail", "Family"=>"Gmail"}, :os=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :platform=>"Unknown", :user_agent=>"Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (via ggpht.com GoogleImageProxy)", :read_seconds=>0, :geo=>{}, :message_id=>"010d1ed3-xxxx-xxxx-xxxx-93b8ec2633e1", :received_at=>"2014-06-05T09:15:10.313638-04:00", :tag=>"welcome-user", :recipient=>"[email protected]"}] 

There are also two enumerators serving the same purpose:

client.opens.take(3)
# => [...]
client.opens_by_message_id('41f03342-xxxx-xxxx-xxxx-558caedb5e82').first
# => {...}

Get message clicks

Requires v1.11.0 or above. Use #get_clicks to retrieve clicks (:count and :offset parameters control pagination).

client.get_clicks(offset:0, count:1)

# => [{:click_location=>"HTML", :client=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :os=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :platform=>"Unknown", :user_agent=>"", :original_link=>"https://example.com", :geo=>{"CountryISOCode"=>"US", "Country"=>"United States", "RegionISOCode"=>"NJ", "Region"=>"New Jersey", "City"=>"Parsippany", "Zip"=>"07054", "Coords"=>"40.8597,-74.4026", "IP"=>"147.75.197.35"}, :message_id=>"41f03342-xxxx-xxxx-xxxx-558caedb5e82", :received_at=>"2018-03-12T14:19:21-04:00", :tag=>"", :recipient=>"[email protected]"}] 

Get single message clicks

Use #get_clicks_by_message_id to access clicks related to a specific message.

client.get_clicks_by_message_id('41f03342-xxxx-xxxx-xxxx-558caedb5e82', count: 1, offset: 0)

# => [{:click_location=>"HTML", :client=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :os=>{"Name"=>"unknown", "Company"=>"unknown", "Family"=>"unknown"}, :platform=>"Unknown", :user_agent=>"", :original_link=>"https://example.com", :geo=>{"CountryISOCode"=>"US", "Country"=>"United States", "RegionISOCode"=>"NJ", "Region"=>"New Jersey", "City"=>"Parsippany", "Zip"=>"07054", "Coords"=>"40.8597,-74.4026", "IP"=>"147.75.197.35"}, :message_id=>"41f03342-xxxx-xxxx-xxxx-558caedb5e82", :received_at=>"2018-03-12T14:19:21-04:00", :tag=>"", :recipient=>"[email protected]"}] 

There are also two enumerators serving the same purpose:

client.clicks.take(3)
# => [...]
client.clicks_by_message_id('41f03342-xxxx-xxxx-xxxx-558caedb5e82').first
# => {...}

You can get more details about the underlying endpoints and parameters they accept in Postmark Developer Docs.

List processed messages

client.get_messages(count: 1, offset: 0, inbound: true)

List single processed message details

client.get_message('41f03342-xxxx-xxxx-xxxx-558caedb5e82', inbound: true)