Ruby SDK
Send email with Cuba
Use Parcel Wing from a compact Cuba route with JSON request parsing and structured errors.
Install the SDK
The official Ruby SDK is published as parcelwing.
Terminal
gem install parcelwing
Send from a route
Create the client once during boot and use Cuba's routing blocks to send trusted server-side email.
app.rb
require "cuba"require "json"require "parcelwing"parcelwing = ParcelWing::Client.new(api_key: ENV.fetch("PARCELWING_API_KEY"))Cuba.define doon post, "send-welcome" dopayload = JSON.parse(req.body.read)emails = parcelwing.emails.send(from: ENV.fetch("PARCELWING_FROM_EMAIL"),to: payload.fetch("email"),template_alias: "welcome",template_params: { first_name: payload["first_name"] || "friend" })res["content-type"] = "application/json"res.write({ id: emails.first.fetch("id") }.to_json)rescue ParcelWing::Error => errorres.status = error.status || 500res["content-type"] = "application/json"res.write({ error: error.code, request_id: error.request_id }.to_json)endon default dores.status = 404res.write "Not found"endend
Production notes
- Keep
PARCELWING_API_KEYin server-side environment variables. - Use saved templates for onboarding, invites, receipts, and account notifications.
- Log
request_idfromParcelWing::Errorfor support and debugging.