Libraries

Ruby

Elastic Email Ruby API Library

Introduction

When you integrate Elastic Email with your application, you can interact with your Elastic Email account directly by our API. You can easily integrate your email flow, manage your contacts and templates, send emails, and track email statistics directly from your app.

This page will help you easily integrate with Elastic Email using the Ruby library. You can find our whole downloadable Ruby repository on GitHub here. If you need API documentation, you can find it here.

Elastic Email API v4 uses REST architecture, which allows you to perform various code actions, including directly through your app.

The maximum email size of your entire message or message + attachment cannot exceed 20MB.

The attachment format is the file’s content as a byte array or a Base64 string.

The maximum number of recipients has no limit for one campaign. It depends on the pricing plan.

The API has a limit of 20 concurrent connections and a hard timeout of 600 seconds per request.

On this page, you will find how to authenticate your application and what the requirements for the integration are. You will be also provided with a quick start guide on how to start using API, followed by code samples. 

If you like this article, share it with friends:

Authentication

To provide valid authentication and start using our API, you will need an API key. To generate your API key, enter settings on your Elastic Email account and go to Settings -> Manage API Keys -> Create or you can also click on the link: 

https://elasticemail.com/account#/settings/new/create-api

Ceater API key

At this point, you can set custom permissions and optional access for your API key. 

Security tip: The restriction forces the API Key to work only with an IP or IP range that will be specified in this field.

Create API key 2

Once you create your API key, keep it safe as it will be used for every API call you make in order to identify you and confirm your account’s credentials. You can create either up to 15 or an unlimited amount of API keys based on your pricing plan. 

Your API key should be sent inside the header with the parameter name ‘x-elasticemail-apikey’ and your API key as a value.

Installation and Usage

Installation

Our official downloadable Ruby library is available on GitHub.

Build a gem

To build the Ruby code into a gem:

Copied!
gem build ElasticEmail.gemspec

Then either install the gem locally:

Copied!
gem install ./ElasticEmail-4.0.20.gem

(for development, run gem install --dev ./ElasticEmail-4.0.20.gem to install the development dependencies)

or publish the gem to a gem hosting service, e.g. RubyGems.

Finally add this to the Gemfile:

Copied!
gem 'ElasticEmail', '~> 4.0.20'

Install from Git

If the Ruby gem is hosted at a git repository, then add the following in the Gemfile:

Copied!
gem 'ElasticEmail', :git => 'https://github.com/elasticemail/elasticemail-ruby.git'

Include the Ruby code directly

Include the Ruby code directly using -I as follows:

Copied!
ruby -Ilib script.rb

Please follow the installation procedure and then run the following code:

Copied!
# Load the gem
require 'ElasticEmail'
# Setup authorization
ElasticEmail.configure do |config|
  # Configure API key authorization: apikey
  config.api_key['apikey'] = 'YOUR API KEY'
  # Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
  # config.api_key_prefix['apikey'] = 'Bearer'
end
api_instance = ElasticEmail::CampaignsApi.new
name = 'name_example' # String | Name of Campaign to delete
begin
  #Delete Campaign
  api_instance.campaigns_by_name_delete(name)
rescue ElasticEmail::ApiError => e
  puts "Exception when calling CampaignsApi->campaigns_by_name_delete: #{e}"
end

Quick start guide

In this section, we will tell you the steps to start sending emails with our email API.

1. Register a free account

Register and activate your Elastic Email account to get access to our product.

2. Verify your domain

Follow the instructions on our settings page to verify your domain and start sending with us.

3. Create an API Key

Go to your setting to generate an API Key to add to your code later.

4. Install our libraries

Install our Ruby library as explained in the Installation and usage section.

5. Send your first email with API

Now it’s time to send your first test email with API to make sure that everything is set up correctly after downloading the library and the authentication process.

Copied!
require 'ElasticEmail'
 
ElasticEmail.configure do |config|
  config.api_key['apikey'] = '895A382DF3DCC13A97E72EXAMPLEKEY'
end
 
api_instance = ElasticEmail::EmailsApi.new
 
email = ElasticEmail::EmailMessageData.new(
  Hash[
    "recipients" => [ ElasticEmail::EmailRecipient.new(Hash["email" => "MeowWow "]) ],
    "content" => ElasticEmail::EmailContent.new(
      "body" => [ ElasticEmail::BodyPart.new(
        "content" => "My test email content ;)",
        "content_type" => "HTML"
        ) ],
      "subject" => "Ruby EE lib test",
      "from" => "MyEmail "
    )
  ]
)
 
begin
  api_instance.emails_post(email)
rescue ElasticEmail::ApiError => e
  puts "Exception when calling EE API: #{e}"
end
If you like this article, share it with friends:

Ready to get started?

Tens of thousands of companies around the world already send their emails with Elastic Email. Join them and discover your own email superpowers.

Free 100 emails/day No credit card required