Skip to main content
The ahasend-rails gem adds an :ahasend delivery method to ActionMailer, so your existing mailers send through the AhaSend API v2 instead of SMTP. This guide installs the gem, sends a test email in sandbox mode and covers per-message options, errors and retries. The gem supports Rails 7.0 to 8.1 on Ruby 3.1 or newer, and the plain mail gem outside Rails. Its source is on GitHub.

Prerequisites

Install the gem

From your Rails app’s root:
Store the key and account ID in your Rails credentials, for example with bin/rails credentials:edit:
config/credentials.yml.enc

Configure ActionMailer

Select the delivery method in each environment that should send through AhaSend:
config/environments/production.rb
The gem checks these settings when it sends the first message, not when the app boots, so environments that use another delivery method need no AhaSend credentials. A missing or invalid setting, or an unknown key such as a typo, raises AhaSend::ConfigurationError. To validate everything without delivering, for example on staging, add sandbox: true:
config/environments/staging.rb
Keep ActionMailer’s :test delivery method in your test environment.

Send a test email

Create a mailer:
app/mailers/welcome_mailer.rb
Replace hello@yourdomain.com with an address on your verified domain, then send it from an environment that uses the :ahasend delivery method:
The command returns without an error when AhaSend accepted the message. With a sandbox key or sandbox: true, the message shows up in your AhaSend dashboard and nothing is delivered. In your app, send with deliver_later from web requests. deliver_now waits for the API, including its retries: with the default settings, one delivery can block for two to three minutes in the worst case. Lower max_retries and the timeouts if you must send inline.

Set AhaSend options per message

Set AhaSend options in the mailer action, either with AhaSend-* headers or with message.ahasend. The gem reads these headers and does not send them on. deliver_later queues the mailer’s arguments, not the built message, so set the options where the message is built:
app/mailers/user_mailer.rb
The tracking, retention and sandbox headers have the same names as the SMTP special headers, so an app that moves from SMTP keeps working. message.ahasend wins over a header, and a header wins over the defaults in ahasend_settings. A malformed value or an unknown AhaSend-* header raises AhaSend::MessageError before anything is sent. Other custom headers, such as X-Campaign, go to AhaSend as message headers.

Recipients

A message with Cc but no To, more than 100 recipients, or more than 50 To, Cc and Bcc addresses together raises AhaSend::MessageError before anything is sent, as do substitutions together with Cc or Bcc.

Handle errors

All errors inherit from AhaSend::Error. With ActionMailer’s default raise_delivery_errors = true, they raise from deliver_now and fail the job of deliver_later. The API can accept some recipients of a message and reject others. That does not raise an error, because a retried job would send the accepted recipients a second copy. The gem reports the rejected recipients in the deliver.ahasend notification instead. To stop retrying a message that every recipient rejected, handle AhaSend::DeliveryError in your mailer:
app/mailers/application_mailer.rb

Retries and idempotency

Every request carries an Idempotency-Key. Without your own key, the gem makes one for each delivery, so its own retries never send an email twice. Set your own key with AhaSend-Idempotency-Key when your job itself may run twice, for example order-confirmation-#{order.id}. The API accepts a repeated key only for an identical request, so a retried job must build the same message. Give inline images a fixed Content-ID, because the mail gem otherwise makes a new one on each build. See idempotency.

Match messages with webhook events

AhaSend assigns each email’s Message-ID header. After delivery, message.message_id holds it. The gem also emits a deliver.ahasend notification for every API call, with AhaSend’s IDs and each recipient’s result:
config/initializers/ahasend.rb
The payload contains no credentials and no message content. The message_id_header is the data.message_id_header of the message’s webhook events, so you can store it and match delivery, bounce and open events to your records.

Use the gem without Rails

To send over SMTP instead of the API, see Ruby SMTP.