Ruby’s standard library has everything you need: net/http and json. Using Rails? See Send with Rails.
Setup
export ROBUSTEMAIL_API_KEY="rb_test_..."
Send an email
# send_email.rb
require "net/http"
require "json"
require "uri"
API = URI("https://robustemail.com/api/v2/emails")
def send_email(email, idempotency_key: nil)
request = Net::HTTP::Post.new(API)
request["Authorization"] = "Bearer #{ENV.fetch('ROBUSTEMAIL_API_KEY')}"
request["Content-Type"] = "application/json"
request["Idempotency-Key"] = idempotency_key if idempotency_key
request.body = JSON.generate(email)
response = Net::HTTP.start(API.host, API.port, use_ssl: true, read_timeout: 15) do |http|
http.request(request)
end
body = JSON.parse(response.body)
raise "#{body['name']}: #{body['message']}" unless response.is_a?(Net::HTTPSuccess)
body["id"]
end
id = send_email(
{
from: "Acme <hello@acme.com>",
to: "delivered@sandbox.robustemail.dev",
subject: "Welcome to Acme",
html: "<h1>Welcome!</h1><p>Thanks for signing up.</p>"
},
idempotency_key: "welcome-user-1042"
)
puts "Queued #{id}"
Attach a file
pack("m0") gives the same strict Base64 as Base64.strict_encode64, without needing the base64 gem.
pdf = File.binread("invoice-2041.pdf")
send_email(
{
from: "Acme Billing <billing@acme.com>",
to: "jane@example.com",
subject: "Your invoice #2041",
html: "<p>Your invoice is attached.</p>",
attachments: [
{ filename: "invoice-2041.pdf", content: [pdf].pack("m0"), content_type: "application/pdf" }
]
},
idempotency_key: "invoice-2041"
)