# Send email from Rails

Add the gem with Bundler, call `send_email` from a job or a controller, and take delivery events in a controller of their own. That controller skips Rails’ CSRF check and verifies `request.raw_post` instead.

## Install the Ruby SDK

`bundle add mailset` writes it into your Gemfile. It adds no other gem to the bundle.

gem:

```bash
gem install mailset
```

Bundler:

```bash
bundle add mailset
```

Gemfile:

```ruby
gem 'mailset', '~> 0.7'
```

## Send your first email from Rails

`Mailset::Client.new` reads `MAILSET_KEY`, and `send_email` takes keywords: `deliver_by: '10m'` here, or `10.minutes` with Active Support. The receipt answers `sent.id` and `sent.status`, and a refusal raises `Mailset::Error`, which a job can rescue.

```ruby
require 'mailset'

mailset = Mailset::Client.new

sent = mailset.send_email(
  from: 'Acme <hello@example.com>',
  to: 'ana@example.org',
  subject: 'Your Acme code',
  text: 'Your code is 123456.',
  html: '<p>Your code is <b>123456</b>.</p>',
  deliver_by: '10m'
)

puts sent.id, sent.status
```

## Verify webhooks in a controller

`create` verifies `request.raw_post`: `params`, or the body parsed as JSON first, lose the bytes the signature covers. `skip_forgery_protection` lets Mailset’s POST past the CSRF check, which the signature replaces. `Mailset::Webhooks.verify` returns the events, a hard bounce answers `event.bounce.hard?`, and `head :no_content` marks the batch kept.

```ruby
# app/controllers/mailset_events_controller.rb
# config/routes.rb:
#   post 'mailset/events', to: 'mailset_events#create'
class MailsetEventsController < ApplicationController
  skip_forgery_protection

  def create
    # The signature covers the body as it came: verify
    # request.raw_post. params, or the body parsed as
    # JSON first, lose the bytes it was made over.
    events = Mailset::Webhooks.verify(
      body: request.raw_post,
      headers: request.headers,
      secret: ENV.fetch('MAILSET_HOOK_SECRET')
    )
    events.each do |event|
      next unless event.type == 'bounced'
      next unless event.bounce.hard?

      logger.info("Stop writing to #{event.recipient}")
    end
    # 2xx once the events are kept.
    # Until then Mailset posts the batch again.
    head :no_content
  rescue Mailset::Error => e
    render plain: e.code, status: :unauthorized
  end
end
```

In the SDK’s tests: run against a stand-in controller.

## Send from your own domain

Your product sends as your own domain either way. What changes is where your team’s mail lives.

- **Sending only.** Your team’s mail stays at Google, Microsoft or wherever it is now. Mailset asks for DKIM, a bounce host and DMARC, and leaves your MX alone. [Send from a domain whose mail stays elsewhere](https://mailset.app/docs/recipes/sending-only)
- **Mailboxes too.** Your team’s mail moves to Mailset, with a real mailbox for every address on the domain and your product’s email on the same bill. [Set up a domain](https://mailset.app/guide/onboarding)

```bash
mailset domains add example.com --sending
mailset domains apply example.com --sending
mailset domains check example.com
mailset keys create --send --domain example.com --from hello@example.com --name "Acme API"
```

## What the Ruby SDK’s build checks

Each line comes from the SDK’s own build, which fails when one stops being true.

- **Ruby 3.0+**: The oldest its manifest allows (its manifest)
- **No dependencies**: Nothing else to install beside it (its manifest)
- **Typed throughout**: Model classes, RBS signatures checked (a test)

## Where to go from here

- [The Ruby SDK](https://mailset.app/docs/sdk/ruby): Every task, from the client’s options to errors and retries.
- [Send email from Ruby](https://mailset.app/send-email/ruby): The Ruby guide, with no framework around it.
- [Webhooks](https://mailset.app/docs/webhooks): The events Mailset posts about your product’s email, and how to check a request’s signature.
- [Give sign-in codes a deadline](https://mailset.app/docs/recipes/deadlines): Use deliverBy so a code that would arrive late is never sent.
- [Retry without sending twice](https://mailset.app/docs/recipes/retries): Make a send safe to repeat with an idempotency key or a Message-ID.
- [Handle bounces](https://mailset.app/docs/recipes/bounces): Tell a dead address from a full inbox, and keep your list clean.
- [Send many messages at once](https://mailset.app/docs/recipes/batches): Up to 100 messages a request, each with its own answer.

## Product email on every plan

Both include 10,000 emails a month, your product’s and your team’s together. Past that it’s $0.40 per 1,000 with pay as you go. Every plan and price: https://mailset.app/pricing

## Sending from Rails, asked and answered

### Does it replace Action Mailer?

It doesn’t plug into Action Mailer. Call `send_email` where you would call `deliver_later`, in a job, with an `idempotency_key` so a retried job sends once.

### Why skip_forgery_protection?

Rails refuses a POST without its authenticity token, and Mailset can’t have one. The signature check takes its place: an unsigned or altered request still gets 401.

### Where should the client live?

In an initializer, as a constant. One client is safe to share between threads, so Puma’s threads and your jobs can all use it.

## Try it on your own domain

7 days free, with everything on. Make a key, run the example above and watch the receipt come back. Start at https://mailset.app/start

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content