# Ruby

<a href="https://github.com/mailtrap/mailtrap-ruby" class="button primary">Mailtrap Ruby SDK on GitHub</a>

### Overview

Mailtrap can be integrated with Ruby apps and projects for email sending.

### Email API/SMTP for Ruby

#### SDK integration

> The Mailtrap Ruby SDK has been significantly expanded. It now covers the full Mailtrap feature set — from batch sending and sandbox testing to contact management, templates, and more.

The [Mailtrap Ruby SDK](https://github.com/mailtrap/mailtrap-ruby) provides an idiomatic Ruby interface for sending transactional and bulk emails. The SDK supports:

* Transactional email sending
* Batch email sending
* Template management
* [ActionMailer](https://github.com/mailtrap/mailtrap-ruby/blob/main/examples/action_mailer.rb) integration for Rails applications
* Comprehensive error handling
* Sandbox email sending and messages CRUD
* Contacts management
* Inboxes and Projects management
* Account management

### Installation

Add the SDK to your Gemfile:

{% code title="Gemfile" %}

```ruby
gem 'mailtrap'
```

{% endcode %}

Then run:

{% code title="Terminal" %}

```bash
bundle install
```

{% endcode %}

### Minimal Example Ruby on Rails (Actionmailer)

{% code title="config/environments/production.rb" %}

```ruby
# place this code in config/environments/production.rb:
config.action_mailer.delivery_method = :mailtrap

# then set the MAILTRAP_API_KEY environment variable
# using your hosting solution.
```

{% endcode %}

### Minimal Example Ruby

Here's a minimal example to send your first email:

{% code title="send\_email.rb" %}

```ruby
require 'mailtrap'

client = Mailtrap::Client.new(api_key: 'your-api-token')

mail = Mailtrap::Mail::FromTemplate.new(
  from: { email: 'hello@example.com', name: 'Mailtrap Test' },
  to: [{ email: 'recipient@example.com' }],
  template_uuid: 'template-uuid',
  template_variables: {
    'user_name' => 'John Doe'
  }
)

# Or send a simple email
mail = {
  from: { email: 'hello@example.com', name: 'Mailtrap Test' },
  to: [{ email: 'recipient@example.com' }],
  subject: 'Hello from Mailtrap!',
  text: 'Welcome to Mailtrap Email Sending!',
  html: '<p>Welcome to <strong>Mailtrap</strong> Email Sending!</p>'
}

response = client.send(mail)
puts response
```

{% endcode %}

{% hint style="info" %}
Get your API token from your Mailtrap account under **Settings → API Tokens**.
{% endhint %}

#### SMTP integration

To integrate SMTP with your Ruby app, navigate to the **Integrations** tab, choose either Ruby on Rails or Ruby Net/SMTP, and copy-paste the credentials or ready-made code snippets.

{% hint style="info" %}
SMTP integration is compatible with any Ruby framework or library that sends emails via SMTP.
{% endhint %}

<div data-with-frame="true"><img src="/files/z2yLsjH1uURE99HhJenz" alt=""></div>

Read more about SMTP integration in the [Email API/SMTP - SMTP Integration](/email-api-smtp/setup/smtp-integration.md) article.

#### RESTful API integration

To integrate Mailtrap using RESTful API, use the configuration available among **Code samples** under the API section.

API integration can be used with any Ruby framework or library that supports HTTP requests. For more details, refer to the [API documentation](https://api-docs.mailtrap.io/docs/mailtrap-api-docs/5tjdeg9545058-mailtrap-api).

<div data-with-frame="true"><img src="/files/ZOn8MC72GPLxZyqnsxDF" alt=""></div>

Read more about API integration in the [Email API/SMTP - API Integration](/email-api-smtp/setup/api-integration.md) article.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mailtrap.io/guides/sdk/ruby.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
