Library Reference

The Mailosaur Ruby library lets you integrate email and SMS testing into your continuous integration process.

This guide provides details on all the methods available within the library.

To check out some common use cases, take a look at getting started with Ruby.

Getting started

Install the Mailosaur Ruby library

Install the package via gem:

gem install mailosaur

‘Hello world’

To use the library, simply add it as a dependency and create a client instance using your API key. You can then use any of the methods described in this document.

To try this out, create a single file called hello.rb with the following code inside it:

require "mailosaur"
mailosaur = Mailosaur::MailosaurClient.new("YOUR_API_KEY")

result = mailosaur.servers.list()

print("You have a server called: " + result.items[0].name)

Just replace YOUR_API_KEY with the your API key, then run the example with ruby hello.rb. If everything is set up correctly, you should see a server name printed out.

Using a test framework

It’s more likely that you are using a testing framework. In this case, the concept is still very much the same:

require 'mailosaur'
require 'test/unit'
require 'shoulda/context'

module Tests
  class PasswordResetTest < Test::Unit::TestCase
    class << self
      def startup
        @@mailosaur = Mailosaur::MailosaurClient.new("YOUR_API_KEY")
        @@server = "SERVER_ID"
      end
    end

    context 'password reset' do
      should 'should send reset email' do
        # 1. Generate a unique email address for this test
        email_address = @@mailosaur.servers.generate_email_address(@@server)

        # 2. **Your automation code that triggers an email to `email_address`**

        # 3. Wait for email to arrive at the target email address
        criteria = Mailosaur::Models::SearchCriteria.new()
        criteria.sent_to = email_address
        email = @@mailosaur.messages.get(@@server, criteria)

        assert_equal(email.subject, "Set a new password")
      end
    end
  end
end

To learn about Server IDs, and what to replace SERVER_ID with, see sending email to Mailosaur.

Messages

Messages is the collective name given to the email and/or SMS messages that are sent into Mailosaur for testing. The message object contains everything you need to perform in-depth automated testing.

messages.get(server, criteria, options)

Waits for a message to be found. Returns as soon as a message matching the specified search criteria is found.

Recommended: This is the most efficient method of looking up a message, therefore we recommend using it whereever possible.

criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "someone.SERVER_ID@mailosaur.io"

message = mailosaur.messages.get("SERVER_ID", criteria)

To learn about Server IDs, and what to replace SERVER_ID with, see sending email to Mailosaur.

Criteria

  • sent_from - The full email address from which the target message was sent.
  • sent_to - The full email address to which the target message was sent.
  • subject - The subject line of the target email.
  • body - Search for part of the message body.
  • match - If set to ALL (default), then only results that match all specified criteria will be returned. If set to ANY, results that match any of the specified criteria will be returned.

Options

  • timeout - Specify how long to wait for a matching result (in milliseconds, default value is 10 seconds).
  • received_after - Limits results to only messages received after this date/time (default 20 seconds ago).
criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "someone.SERVER_ID@mailosaur.io"
ten_minutes_ago = DateTime.now - (10 / 1440.0)

# Search all messages received in the last 10 minutes
message = mailosaur.messages.get("SERVER_ID", criteria, received_after: ten_minutes_ago)

messages.list(server, options)

Returns a list of your messages in summary form. The summaries are returned sorted by received date, with the most recently-received messages appearing first.

Message summaries The method returns a message summary, rather than the full message object. This means that several properties, like the message body, are not included. To get this data, you’ll need to call get_by_id. Alternatively, we recommend using the get method, which is a far more efficient approach.
# List the most recent messages
result = mailosaur.messages.list("SERVER_ID")

# Get the most recent message (the first one in the list)
message = result.items[0]

print('Subject: ' + message.subject)

Options

  • received_after - Allows you to customise how far back to look for messages.
  • page - Used alongside items_per_page to paginate through results. This is zero-based, meaning 0 is the first page of results.
  • items_per_page - A limit on the number of results to be returned. This can be set between 1 to 1000, with the default being 50.
ten_minutes_ago = DateTime.now - (10 / 1440.0)

# List all results received in the last 10 minutes.
# Limit results to the first 10 matches only.
result = mailosaur.messages.list("SERVER_ID", page: 0, items_per_page: 10, received_after: ten_minutes_ago)

messages.search(server, criteria, options)

Returns a list of messages matching the specified search criteria, in summary form. The messages are returned sorted by received date, with the most recently-received messages appearing first.

Message summaries The method returns a message summary, rather than the full message object. This means that several properties, like the message body, are not included. To get this data, you’ll need to call get_by_id. Alternatively, we recommend using the get method, which is a far more efficient approach.
criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "someone.SERVER_ID@mailosaur.io"

result = mailosaur.messages.search("SERVER_ID", criteria)

# Get the most recent match
message = result.items[0]

print('Subject: ' + message.subject)

Criteria

  • sent_from - The full email address from which the target message was sent.
  • sent_to - The full email address to which the target message was sent.
  • subject - The subject line of the target email.
  • body - Search for part of the message body.
  • match - If set to ALL (default), then only results that match all specified criteria will be returned. If set to ANY, results that match any of the specified criteria will be returned.

Options

  • timeout - If provided, determines how long to wait for a matching result (provided in milliseconds).
  • error_on_timeout - When set to false, an error will not be throw if timeout is reached (default: true).
  • received_after - Allows you to customise how far back to look for messages.
  • page - Used alongside items_per_page to paginate through results. This is zero-based, meaning 0 is the first page of results.
  • items_per_page - A limit on the number of results to be returned. This can be set between 1 to 1000, with the default being 50.
criteria = Mailosaur::Models::SearchCriteria.new()
criteria.sent_to = "someone.SERVER_ID@mailosaur.io"

# Search for all messages sent to someone.SERVER_ID@mailosaur.io,
# received in the last 2 hours. Limit results to the first 10 matches only.
two_hours_ago = DateTime.now - (2 / 24.0)
result = mailosaur.messages.search("SERVER_ID", criteria, page: 0, items_per_page: 10, received_after: two_hours_ago)

messages.get_by_id(id)

Retrieves the detail for a single email message. Must be used in conjunction with either list or search in order to get the unique identifier for the required message. We always recommend using the get method instead.

result = mailosaur.messages.list("SERVER_ID")
message_id = result.items[0].id

message = mailosaur.messages.get_by_id(message_id)

print('Subject: ' + message.subject)

messages.delete(id)

Permanently deletes a message. Also deletes any attachments related to the message. This operation cannot be undone.

mailosaur.messages.delete(message_id)

messages.delete_all(server)

Permanently deletes all messages held by the specified server. Also deletes any attachments related to each message. This operation cannot be undone.

mailosaur.messages.delete_all("SERVER_ID")

Servers

Servers contain all the configuration and set up for.

servers.list()

Returns a list of your virtual servers. Servers are returned sorted in alphabetical order.

result = mailosaur.servers.list()

print('You have a server called: ' + result.items[0].name)

servers.create(server)

Creates a new virtual server. Only the name property is required to create a new server via the API.

create_options = Mailosaur::Models::ServerCreateOptions.new()
create_options.name = "My email tests"

mailosaur.servers.create(create_options)

servers.get(id)

Retrieves the detail for a single server. Simply supply the unique identifier for the required server.

server = mailosaur.servers.get("SERVER_ID")

servers.update(id, server)

Updates a single server.

retrieved_server = mailosaur.servers.get("SERVER_ID")
retrieved_server.name = "Updated server name"

mailosaur.servers.update(retrieved_server.id, retrieved_server)

servers.delete(id)

Permanently deletes a server. Also deletes all messages and associated attachments within the server. This operation cannot be undone.

mailosaur.servers.delete("SERVER_ID")

servers.generate_email_address(id)

Utility method to help you generate a random email address for a given server.

email_address = mailosaur.servers.generate_email_address("SERVER_ID")

print(email_address) # "bgwqj@SERVER_ID.mailosaur.io"

To learn about Server IDs, and what to replace SERVER_ID with, see sending email to Mailosaur.

Files

files.get_email(message_id)

Downloads an EML file representing the specified email. Simply supply the unique identifier for the required email.

files.get_attachment(attachment_id)

Downloads a single attachment. Simply supply the unique identifier for the required attachment.

Analysis

analysis.spam(message_id)

Perform spam testing on the specified email.