Ruby
Ruby integration examples for Keplers Mail Service
Send emails using Ruby with Keplers Mail Service REST API.
Installation
# No additional dependencies required for basic usageBasic Setup
# .env
KEPLERS_API_KEY=kms_your_api_key.live_...REST API Integration
Basic Client
require 'net/http'
require 'json'
require 'uri'
class KeplersEmailClient
def initialize(api_key, base_url = 'https://api.keplars.com')
@api_key = api_key
@base_url = base_url
end
def send_email(email_data)
make_request('/api/v1/send-email/queue', email_data)
end
def send_instant_email(email_data)
make_request('/api/v1/send-email/instant', email_data)
end
private
def make_request(endpoint, data)
uri = URI("#{@base_url}#{endpoint}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request['Authorization'] = "Bearer #{@api_key}"
request['Content-Type'] = 'application/json'
request.body = data.to_json
response = http.request(request)
if response.code == '200'
JSON.parse(response.body)
else
raise "Failed to send email: #{response.body}"
end
end
endUsage Examples
client = KeplersEmailClient.new(ENV['KEPLERS_API_KEY'])
def send_welcome_email(client, user_email, user_name)
begin
email_data = {
to: [user_email],
subject: "Welcome #{user_name}!",
body: "<h1>Welcome #{user_name}!</h1><p>Thank you for joining our platform.</p>",
is_html: true
}
result = client.send_email(email_data)
puts "Email sent: #{result}"
result
rescue => e
puts "Failed to send email: #{e.message}"
raise
end
end
def send_verification_email(client, user_email, code)
begin
email_data = {
to: [user_email],
subject: 'Verify Your Email',
body: "Your verification code: #{code}",
is_html: false
}
result = client.send_instant_email(email_data)
puts "Verification email sent: #{result}"
result
rescue => e
puts "Failed to send verification email: #{e.message}"
raise
end
end
send_welcome_email(client, '[email protected]', 'John')
send_verification_email(client, '[email protected]', '123456')Rails Integration
# app/services/email_service.rb
class EmailService
def self.client
@client ||= KeplersEmailClient.new(Rails.application.credentials.keplers_api_key)
end
def self.send_welcome_email(email, name)
email_data = {
to: [email],
subject: "Welcome #{name}!",
body: "<h1>Welcome #{name}!</h1>",
is_html: true
}
client.send_email(email_data)
end
end
# app/controllers/auth_controller.rb
class AuthController < ApplicationController
def register
begin
EmailService.send_welcome_email(params[:email], params[:name])
render json: { success: true }
rescue => e
render json: { error: 'Failed to send email' }, status: 500
end
end
endSinatra Integration
require 'sinatra'
require 'json'
configure do
set :keplers_client, KeplersEmailClient.new(ENV['KEPLERS_API_KEY'])
end
post '/send-email' do
content_type :json
data = JSON.parse(request.body.read)
begin
email_data = {
to: [data['email']],
subject: "Welcome #{data['name']}!",
body: "<h1>Welcome #{data['name']}!</h1>",
is_html: true
}
result = settings.keplers_client.send_email(email_data)
{ success: true, data: result }.to_json
rescue => e
status 500
{ success: false, error: e.message }.to_json
end
endSMTP Integration
SMTP Installation
# Gemfile
gem 'mail'
gem 'net-smtp'SMTP Client
require 'mail'
class KeplersEmailSMTPClient
def initialize(smtp_config)
@config = smtp_config
configure_mail
end
def send_email(email_data)
mail = Mail.new do
from '[email protected]'
to email_data[:to].first
subject email_data[:subject]
if email_data[:is_html]
html_part do
content_type 'text/html; charset=UTF-8'
body email_data[:body]
end
else
text_part do
content_type 'text/plain; charset=UTF-8'
body email_data[:body]
end
end
end
mail.deliver!
{ success: true, data: { status: 'sent', method: 'smtp' } }
rescue => e
raise "SMTP send failed: #{e.message}"
end
private
def configure_mail
Mail.defaults do
delivery_method :smtp, {
address: 'smtp.keplars.com',
port: 587,
user_name: @config[:username],
password: @config[:password],
authentication: 'plain',
enable_starttls_auto: true
}
end
end
endSMTP Usage
smtp_client = KeplersEmailSMTPClient.new({
username: ENV['KEPLERS_SMTP_USERNAME'],
password: ENV['KEPLERS_SMTP_PASSWORD']
})
def send_email_via_smtp(smtp_client, user_email, subject, body)
begin
email_data = {
to: [user_email],
subject: subject,
body: body,
is_html: true
}
result = smtp_client.send_email(email_data)
puts "SMTP email sent: #{result}"
result
rescue => e
puts "SMTP send failed: #{e.message}"
raise
end
end
send_email_via_smtp(smtp_client, '[email protected]', 'Test Email', '<p>Hello World!</p>')Choose REST API for advanced features or SMTP for traditional email client integration.