# MailSlurp Ruby Client > Create real email addresses on demand. Send and receive emails and attachments from code and tests using Ruby. MailSlurp is an email API service that lets you create real email addresses in code. You can then send and receive emails and attachments in Ruby applications and tests. ## Quick links - [Method Documentation](https://www.mailslurp.com/docs/ruby/docs/) - [Gem Package](https://rubygems.org/gems/mailslurp_client) - [Github Source](https://github.com/mailslurp/mailslurp-client-ruby) ### Common controllers - [Email controller](https://www.mailslurp.com/docs/ruby/docs/EmailControllerApi/) send and receive emails - [Inbox controller](https://www.mailslurp.com/docs/ruby/docs/InboxControllerApi/) create and manage email addresses - [WaitFor controller](https://www.mailslurp.com/docs/ruby/docs/WaitForControllerApi/) wait for expected emails to arrive ### Example tutorials ## Get started This section describes how to get up and running with the Ruby client. See the [examples page](https://www.mailslurp.com/examples/) for more examples and use with common frameworks such as Rails and RSpec. See the method documentation for a [list of all functions](https://www.mailslurp.com/docs/ruby/docs/) ### Create API Key First you'll need an API Key. [Create a free account](https://app.mailslurp.com) and copy the key from your dashboard. ### Ruby requirements The MailSlurp client requires Ruby 2.x or 3.x and the ruby-dev package. You most likely have these packages but if not: `sudo apt-get install ruby-dev` ### Install Gem ```bash gem install mailslurp_client ``` Or in your `Gemfile`: ```ruby gem 'mailslurp_client', '~> 8.3', '>= 8.3.0' ``` And then run bundler install: ```bash gem install bundler bundle install ``` #### Libcurl requirements You may need to install `typhoeus` if you encounter libcurl errors. ### Configure the client ```ruby require 'mailslurp_client' # configure mailslurp client globally with API key (or pass each controller a client instance) MailSlurpClient.configure do |config| config.api_key['x-api-key'] = "YOUR_API_KEY_HERE" end ``` ## Common uses MailSlurp can be used for anything email related: sending and receiving emails, creating email addresses, or testing email processes. Here are some common uses: ### Create inboxes To use MailSlurp you need to create inboxes. These are email accounts that have an ID and a real email address. See methods on the [inbox controller](https://www.mailslurp.com/docs/ruby/docs/InboxControllerApi/) for more information. ```ruby inbox_controller = MailSlurpClient::InboxControllerApi.new inbox = inbox_controller.create_inbox ``` In a test: ```ruby it 'can create email addresses' do inbox_controller = MailSlurpClient::InboxControllerApi.new inbox = inbox_controller.create_inbox expect(inbox.id).not_to be_nil expect(inbox.email_address).to include("mailslurp.com") end ``` #### More options The `create_inbox` method has some limitations in the Ruby client. To create inboxes with more options use the alternative `create_inbox_with_options` method. (This uses a request body instead of query parameters.) ```ruby it 'can an inbox with tags' do inbox_controller = MailSlurpClient::InboxControllerApi.new # create an inbox with tags inbox = inbox_controller.create_inbox_with_options({ tags: ['t1','t2'], description: "test with tags", name: "test name" }) # has tags expect(inbox.id).to be_truthy expect(inbox.description).to be_truthy expect(inbox.name).to be_truthy expect(inbox.tags).to include('t1') expect(inbox.tags).to include('t2') # can update tags inbox_updated = inbox_controller.update_inbox(inbox.id, { tags: ['newtag'] }) expect(inbox_updated.tags).to eq(['newtag']) end ``` ### List inboxes Inboxes you create can be listed in a paginated way using the [InboxController](https://www.mailslurp.com/docs/ruby/docs/InboxControllerApi/)). ```ruby it 'can list inboxes' do inbox_controller = MailSlurpClient::InboxControllerApi.new paged_inboxes = inbox_controller.get_all_inboxes({ page: 0, size: 20 }) # assert on pagination fields expect(paged_inboxes.content).not_to be_empty expect(paged_inboxes.number).to be(0) expect(paged_inboxes.size).to be(20) # can access inbox result expect(paged_inboxes.content[0].id).not_to be_empty end ``` ### Send emails You can send HTML emails easily with the inbox controller. First create an inbox then use its ID with the `send_email` method. To send attachments see the [Method Documentation](https://www.mailslurp.com/docs/ruby/docs/). ```ruby # create an inbox inbox_controller = MailSlurpClient::InboxControllerApi.new inbox = inbox_controller.create_inbox # send an email from the inbox inbox_controller.send_email(inbox.id, { send_email_options: { to: ["test@example.org"], subject: "Test", isHTML: true, body: <<-HEREDOC
MailSlurp supports HTML
HEREDOC } }) ``` You can also use objects for most method options: ```ruby # for send options see https://github.com/mailslurp/mailslurp-client-ruby/blob/master/docs/SendEmailOptions opts = { send_email_options: MailSlurpClient::SendEmailOptions.new( { to: [inbox_2.email_address], subject: 'Test email', from: inbox_1.email_address, body: 'Test email content', is_html: true, attachments: attachment_ids } ) } inbox_controller.send_email(inbox_1.id, opts) ``` ### Receive emails To read already existing emails use the [Email Controller](https://www.mailslurp.com/docs/ruby/docs/EmailControllerApi/). To wait for expected emails to arrive use the [WaitFor Controller](https://www.mailslurp.com/docs/ruby/docs/WaitForControllerApi/). You can use MailSlurp to wait for at least 1 unread email in an inbox and return it. If a timeout is exceeded it will throw an error instead: ```ruby waitfor_controller = MailSlurpClient::WaitForControllerApi.new email = waitfor_controller.wait_for_latest_email({ inbox_id: inbox.id, unread_only: true, timeout: 30_000 }) # verify email contents expect(email.subject).to include("Test") expect(email.body).to include("Your email body") ``` ### Extract email content To parse an email and extract content use regex patterns like so: ```ruby wait_controller = MailSlurpClient::WaitForControllerApi.new 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}" end code, * = match.captures ``` ### Attachments You can send attachments by first uploading files with the [AttachmentControllerApi](https://www.mailslurp.com/docs/ruby/docs/AttachmentControllerApi/) then using the returned attachment IDs in the send email method. MailSlurp endpoints use base64 string encoding for upload and download files. To encode or decode strings in Ruby make sure you use the **strict** variables that avoid added newlines. #### Upload and send ```ruby # upload a file to mailslurp to use as attachment # @return [Array