Send with Ruby
Send email from Ruby with the usend gem, over plain HTTP or over SMTP.
The official gem uses only the Ruby standard library and needs Ruby 3.0 or newer. You need a verified domain and an API key.
1. Install and set the key
gem install usend
export USEND_API_KEY="us_123456789"2. Send
require "usend"
usend = Usend::Client.new(api_key: ENV["USEND_API_KEY"])
begin
email = usend.emails.send_email(
{
from: "Acme <[email protected]>",
to: ["[email protected]"],
subject: "Hello from usend",
html: "<p>It works!</p>"
},
idempotency_key: "welcome-ann-1" # optional
)
puts "queued #{email["id"]}"
rescue Usend::Error => e
warn "#{e.status_code} #{e.name}: #{e.message}"
exit 1
endruby send.rbA failure raises Usend::Error with status_code, name and message; see Errors. Rate limits and server errors are retried automatically, and the idempotency key makes a retry safe: see Idempotency.
Webhooks
event = Usend::Webhook.verify(payload: raw_body, headers: headers, secret: ENV["USEND_WEBHOOK_SECRET"])It raises Usend::Error when the signature is wrong. See Verify signatures.
Without the gem
The API is one JSON request: POST https://api.usend.et/emails with an Authorization: Bearer header works from Net::HTTP or any HTTP client. You can also send over SMTP with the mail gem or net-smtp: host smtp.usend.et, port 465 or 587, username usend, your API key as the password. usend.smtp_settings returns these settings.
Next steps
- Send emails for every field.
- Attachments: base64-encode the file in
content.