Quick setup examples
Ruby on Rails
Send your first email from a Rails controller by calling the MailBlastr API with Net::HTTP.
Ruby has an official gem — gem install mailblastr — and it works from any Rails controller or job. The example below shows the dependency-free alternative instead: POST JSON to https://www.mailblastr.com/api/emails with the standard-library net/http, so there is nothing to install. See SDKs for the gem.
Prerequisites
- A MailBlastr API key.
- A verified domain to send from.
1. Add your API key
Store your key in the environment (or Rails credentials). The example reads MAILBLASTR_API_KEY from ENV.
.env
MAILBLASTR_API_KEY=mb_xxxxxxxxx2. Send email from a controller
POST the email body to the MailBlastr API from a controller action with net/http. The easiest way to send is with the html field.
app/controllers/send_controller.rb
require "net/http"
require "json"
require "uri"
class SendController < ApplicationController
def create
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)
render json: JSON.parse(response.body), status: response.code.to_i
end
endA successful call returns the created email's
id. See every supported field in the Send an email reference.