Wait for emails - receive inbound emails in code

Fetch emails and attachments in code by waiting for new emails to arrive. MailSlurp waitFor methods provide automatic long-polling.

  • Table of contents

MailSlurp has many methods for sending and receiving emails. Here are three main ways to receive emails with MailSlurp.

See the Email Model Specification for available properties and content for emails. See the attachments guide for help with attachments.

1) Email controller

EmailControllerApi methods such as getEmail and getEmailsPaginated return existing emails and content from inboxes. Call the getEmailsPaginated method to return a paginated list of existing emails inside your inboxes. Call the getEmail to fetch an individual email and content.

2) WaitFor controller

WaitForControllerApi methods such as waitForLastestEmail hold a connection open until conditions are met - such as a new email arriving. Use wait for methods in code or tests to perform actions and then test expected emails. Here is a example using the Ruby SDK:

wait_controller = MailSlurpClient::WaitForControllerApi.new

# wait for email to arrive
email = wait_controller.wait_for_latest_email({ inbox_id: inbox.id, unread_only: true, timeout: 30_000 })

# assert the email is a confirmation 
expect(email.subject).to include("Please confirm your email address")

# extract a 6 digit code from the email body
match = email.body.match(/code is ([0-9]{6})/)
if match == nil then
  raise "Could not find match in body #{email.body}"
code, * = match.captures

Use unreadOnly=true to ensure only new emails are returned. See the receiving emails guide for more information.