Guide · Ruby
How to send email with Ruby
Send email from Ruby with Net::HTTP and the Stack Emails API, with no gems required.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
export STACKEMAILS_API_KEY=se_live_xxxxxxxxxxxx
2. Send your first email
require "net/http"
require "json"
uri = URI("https://stackemails.com/v1/emails")
req = Net::HTTP::Post.new(uri, {
"Authorization" => "Bearer #{ENV.fetch("STACKEMAILS_API_KEY")}",
"Content-Type" => "application/json",
})
req.body = {
from: "Acme <hello@mail.acme.com>",
to: "ada@example.com",
subject: "Welcome to Acme",
html: "<p>Thanks for signing up!</p>",
}.to_json
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
raise JSON.parse(res.body).dig("error", "message") unless res.is_a?(Net::HTTPSuccess)
puts JSON.parse(res.body)["id"]
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.