Send email with Ruby on Rails
A server-side call to the SendSend API from your Ruby on Rails app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Add the key to Rails credentials (bin/rails credentials:edit) or set SENDSEND_API_KEY in the environment.
- Add this to
app/controllers/mail_controller.rb:
require "net/http"
require "securerandom"
class MailController < ApplicationController
def send_mail
uri = URI("https://api.sendsend.email/v1/emails")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json",
"Authorization" => "Bearer #{ENV.fetch('SENDSEND_API_KEY')}",
"Idempotency-Key" => SecureRandom.uuid)
req.body = { from: "Acme <hello@mail.acme.com>", to: [params[:to]], subject: "Hello from Rails", html: "<p>It works!</p>" }.to_json
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
render json: res.body, status: res.code.to_i
end
endGood to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.