Quick setup examples

Sinatra

Send your first email from a Sinatra app using Net::HTTP against the MailBlastr API.

Ruby has an official SDK — gem install mailblastr — and it works unchanged inside a Sinatra route: set Mailblastr.api_key once, then call Mailblastr::Emails.send({ ... }). The example below deliberately takes the dependency-free route instead, POSTing JSON to https://www.mailblastr.com/api/emails with the standard-library net/http, so there is nothing extra to install beyond Sinatra itself.

Prerequisites

1. Install Sinatra

Add Sinatra to your project if you have not already.

RubyGems
gem install sinatra

2. Set your API key

Read your API key from the environment so it never lives in source.

.env
MAILBLASTR_API_KEY=mb_xxxxxxxxx

3. Send email using HTML

The easiest way to send an email is with the html field. POST it to the MailBlastr API from a route handler.

index.rb
require "sinatra"
require "net/http"
require "json"
require "uri"

set :port, 5000
set :bind, "0.0.0.0"

get "/" do
  content_type :json

  uri = URI("https://www.mailblastr.com/api/emails")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri)
  request["Authorization"] = "Bearer #{ENV.fetch('MAILBLASTR_API_KEY')}"
  request["Content-Type"] = "application/json"
  request.body = {
    from: "Acme <onboarding@yourdomain.com>",
    to: ["delivered@mailblastr.dev"],
    subject: "hello world",
    html: "<strong>it works!</strong>"
  }.to_json

  response = http.request(request)
  response.body
end
A successful call returns the created email's id. See Send an email for all supported fields.