Send Rails ActionMailer email through the AhaSend API with the ahasend-rails gem: one delivery method, idempotent retries, per-message options and sandbox mode.
On this page
We released ahasend-rails, a gem that sends your Rails email through the AhaSend API instead of SMTP. Your mailers do not change. You swap one delivery method, and ActionMailer sends through AhaSend.
It supports Rails 7.0 to 8.1 on Ruby 3.1 or newer, and the plain mail gem outside Rails. The source is on GitHub under the MIT license.
One delivery method to swap
Add the gem:
bundle add ahasend-railsStore your API key and account ID in Rails credentials, then pick the delivery method in each environment that should send through AhaSend:
config.action_mailer.delivery_method = :ahasend
config.action_mailer.ahasend_settings = {
api_key: Rails.application.credentials.dig(:ahasend, :api_key),
account_id: Rails.application.credentials.dig(:ahasend, :account_id)
}That is the whole change. Every mailer you already wrote keeps working, and its mail now goes out over the AhaSend API. Environments that use another delivery method need no AhaSend credentials, because the gem reads its settings on the first send, not at boot.
Retries that do not send a second email
An HTTP send can time out after the server already accepted the message. Retry it blindly and the recipient gets the email twice. The gem puts an idempotency key on every request, so its own retries, on a timeout, a 429 or a 5xx, never send twice.
When your own job may run twice, set your key and the guarantee extends to your side:
headers["AhaSend-Idempotency-Key"] = "order-confirmation-#{order.id}"A retried job that builds the same message sends the email at most once. Our post on idempotency keys explains why that matters.
Per-message options as mailer headers
Set AhaSend options in the mailer action, with AhaSend-* headers or with message.ahasend:
headers["AhaSend-Tags"] = "welcome,onboarding"
headers["AhaSend-Track-Opens"] = "true"Tags, open and click tracking, retention, scheduling, substitutions and sandbox mode are all available per message. The gem reads these headers and does not pass them on to the recipient. Attachments, To, Cc and Bcc work as they do in any mailer.
Test without sending
A key in sandbox mode, or sandbox: true in the settings, runs the whole path without delivering anything. The message shows up in your dashboard and no email leaves. Useful on staging, and for a first run before you trust it in production.
Errors you can rescue
Every error inherits from AhaSend::Error, with a specific class for each case: a missing setting, a message that cannot be built, an authentication failure, a paused domain, a rate limit, a timeout. The gem also emits a deliver.ahasend notification with AhaSend's IDs and each recipient's result, so you can match later delivery, bounce and open webhooks to your own records. The notification carries no credentials and no message content.
Moving from SMTP
If you send through AhaSend over SMTP today, the gem's header names match the SMTP special headers. An app that set AhaSend-Track-Opens over SMTP keeps the same header on the API, so switching is the delivery-method change above and nothing in your mailers.
If you would rather stay on SMTP, that path is unchanged. The gem is for teams that want the HTTP API: idempotent retries, per-message options and structured errors, with the connection handling left to the gem.
Outside Rails
The gem works with the plain mail gem too, so a Ruby script or a non-Rails app can send through the same delivery method.
The Rails guide has the full setup, every per-message option, recipient handling for To, Cc and Bcc, and the error table.