Ruby SDK
Send email with Ruby on Rails
Use the Parcel Wing Ruby SDK from Rails services, controllers, and background jobs.
Install the SDK
The official Ruby SDK is published as parcelwing.
Terminal
gem install parcelwing
Configure Rails
Load your API key and default sender from encrypted credentials or deployment-only environment variables.
config/initializers/parcelwing.rb
# config/initializers/parcelwing.rbRails.application.config.x.parcelwing = ActiveSupport::OrderedOptions.newRails.application.config.x.parcelwing.api_key = ENV.fetch("PARCELWING_API_KEY")Rails.application.config.x.parcelwing.from_email = ENV.fetch("PARCELWING_FROM_EMAIL")
Create a mail service
Wrap common lifecycle messages in a service class so controllers, jobs, and models share one sending interface.
app/services/parcelwing_mailer.rb
# app/services/parcelwing_mailer.rbrequire "parcelwing"class ParcelwingMailerdef initialize(client: default_client)@client = clientenddef send_welcome(email:, first_name: nil)emails = @client.emails.send(from: Rails.configuration.x.parcelwing.from_email,to: email,template_alias: "welcome",template_params: { first_name: first_name.presence || "friend" })emails.firstendprivatedef default_clientParcelWing::Client.new(api_key: Rails.configuration.x.parcelwing.api_key)endend
Send from a controller
Validate inputs before calling the service, then return the queued Parcel Wing message ID.
app/controllers/welcome_emails_controller.rb
# app/controllers/welcome_emails_controller.rbclass WelcomeEmailsController < ApplicationControllerdef createmessage = ParcelwingMailer.new.send_welcome(email: params.require(:email),first_name: params[:first_name])render json: { id: message.fetch("id") }rescue ParcelWing::Error => errorRails.logger.warn("Parcel Wing request_id=#{error.request_id} code=#{error.code}")render json: { error: error.code, request_id: error.request_id }, status: error.statusendend
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.