Gem:
unosend ยท Ruby: 3.0+Installation
Gemfile
Copy
gem 'unosend'
Copy
bundle install
# or
gem install unosend
Quick Start
Copy
require 'unosend'
client = Unosend::Client.new(api_key: 'un_your_api_key')
result = client.emails.send(
from: 'hello@yourdomain.com',
to: ['user@example.com'],
subject: 'Hello from Unosend!',
html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>'
)
puts "Email sent: #{result.id}"
Configuration
Copy
client = Unosend::Client.new(
api_key: ENV['UNOSEND_API_KEY'],
base_url: 'https://www.unosend.co/api/v1', # default
timeout: 30 # seconds
)
Never hardcode your API key. Use environment variables:
ENV['UNOSEND_API_KEY']Emails
Send an Email
Copy
result = client.emails.send(
from: 'John Doe <john@yourdomain.com>',
to: ['user@example.com'],
subject: 'Order Confirmation',
html: '<h1>Order #1234</h1><p>Your order has been confirmed.</p>',
text: 'Order #1234 - Your order has been confirmed.',
reply_to: 'support@yourdomain.com',
cc: ['manager@yourdomain.com'],
bcc: ['archive@yourdomain.com'],
tags: [{ name: 'category', value: 'order' }],
tracking: { open: true, click: true }
)
Send with Attachments
Copy
require 'base64'
pdf_content = Base64.strict_encode64(File.read('invoice.pdf'))
result = client.emails.send(
from: 'billing@yourdomain.com',
to: ['customer@example.com'],
subject: 'Your Invoice',
html: '<p>Please find your invoice attached.</p>',
attachments: [
{
filename: 'invoice.pdf',
content: pdf_content,
content_type: 'application/pdf'
}
]
)
Send with Templates
Copy
result = client.emails.send(
from: 'hello@yourdomain.com',
to: ['user@example.com'],
subject: 'Welcome, {{first_name}}!',
template_id: 'tmpl_abc123',
template_data: {
first_name: 'Alice',
company: 'Acme Inc'
}
)
Send Batch Emails
Copy
results = client.emails.send_batch([
{
from: 'hello@yourdomain.com',
to: ['alice@example.com'],
subject: 'Welcome, Alice!',
html: '<h1>Welcome!</h1>'
},
{
from: 'hello@yourdomain.com',
to: ['bob@example.com'],
subject: 'Welcome, Bob!',
html: '<h1>Welcome!</h1>'
}
])
Get & List Emails
Copy
# Get email details
email = client.emails.get('email_abc123')
puts email.status # 'delivered'
puts email.opened_at # '2026-02-28T10:30:00Z'
# List emails
emails = client.emails.list(limit: 50, status: 'delivered')
emails.data.each do |email|
puts "#{email.to} โ #{email.status}"
end
Domains
Copy
# Create domain
domain = client.domains.create(domain: 'yourdomain.com')
puts "DNS records: #{domain.dns_records}"
# Verify after adding DNS records
verified = client.domains.verify(domain.id)
puts "Verified: #{verified.verified}"
Contacts
Copy
# Create audience
audience = client.audiences.create(name: 'Newsletter Subscribers')
# Add contact
contact = client.contacts.create(audience.id,
email: 'user@example.com',
first_name: 'Alice',
last_name: 'Smith',
metadata: { plan: 'pro' }
)
SMS
Copy
sms = client.sms.send(
to: '+14155551234',
message: 'Your verification code is 384729. Expires in 10 minutes.'
)
puts "SMS sent: #{sms.id} (Cost: $#{sms.cost})"
Copy
msg = client.whatsapp.send(
to: '+919876543210',
type: 'template',
template: 'order_update',
language: 'en',
variables: ['Alice', 'ORD-1234']
)
Error Handling
Copy
begin
result = client.emails.send(
from: 'hello@yourdomain.com',
to: ['user@example.com'],
subject: 'Hello',
html: '<h1>Hello</h1>'
)
puts "Sent: #{result.id}"
rescue Unosend::AuthenticationError
puts 'Invalid API key'
rescue Unosend::ValidationError => e
puts "Validation error: #{e.message}"
rescue Unosend::RateLimitError => e
puts "Rate limited. Retry after #{e.retry_after} seconds"
rescue Unosend::Error => e
puts "Error #{e.status_code}: #{e.message}"
end
Framework Examples
Ruby on Rails
Copy
# config/initializers/unosend.rb
UNOSEND = Unosend::Client.new(api_key: Rails.application.credentials.unosend_api_key)
# app/controllers/emails_controller.rb
class EmailsController < ApplicationController
def create
result = UNOSEND.emails.send(
from: 'welcome@yourdomain.com',
to: [params[:email]],
subject: "Welcome, #{params[:name]}!",
html: "<h1>Welcome, #{params[:name]}!</h1>"
)
render json: { id: result.id }
end
end
Sinatra
Copy
require 'sinatra'
require 'unosend'
client = Unosend::Client.new(api_key: ENV['UNOSEND_API_KEY'])
post '/send' do
data = JSON.parse(request.body.read)
result = client.emails.send(
from: 'hello@yourdomain.com',
to: [data['email']],
subject: data['subject'],
html: data['html']
)
{ id: result.id }.to_json
end
Environment Variables
.env
Copy
UNOSEND_API_KEY=un_your_api_key
Full API Reference
See all available endpoints and parameters