sendsend.email

Send email with Ruby on Rails

A server-side call to the SendSend API from your Ruby on Rails app.

  1. Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
  2. Add the key to Rails credentials (bin/rails credentials:edit) or set SENDSEND_API_KEY in the environment.
  3. 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
end

Good to know