Skip to content

Instantly share code, notes, and snippets.

@tarolandia
Created May 28, 2013 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarolandia/5665626 to your computer and use it in GitHub Desktop.
Save tarolandia/5665626 to your computer and use it in GitHub Desktop.
Postman Mandril API adapter
require "net/http"
require "uri"
require "json"
module Postman
class MandrilAPI
@key = ''
@request = {}
@uri = ''
ENDPOINT = 'https://mandrillapp.com/api/1.0'
def initialize(key)
@key = key
@request = Hash.new
@uri = URI.parse(URI.encode(ENDPOINT))
end
def send_mail(email)
@request = {
:key => '',
:message => {
:html => '',
:text => '',
:subject => '',
:from_email => '',
:from_name => '',
:to => [],
:headers => {},
:track_opens => true,
:track_clicks => true,
:auto_text => true,
:url_strip_qs => true,
:preserve_recipients => false,
:bcc_address => '',
},
:async => true
}.to_hash
@request[:key] = @key
@request[:message][:to] << { :email => email.to, :name => ''}
@request[:message][:from_name] = email.from
@request[:message][:from_email] = 'info@plupin.com'
@request[:message][:html] = email.body
@request[:message][:text] = email.body.gsub(/<\/?[^>]*>/, "")
@request[:message][:subject] = email.subject
req = Net::HTTP::Post.new('/api/1.0/messages/send.json', initheader = {'Content-Type' =>'application/json'})
req.body = @request.to_json
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = true
response = http.start {|http| http.request(req)}
raise DeliverError.new("Mandrill response.code not equal to 200") unless response.code.to_i == 200
puts "Response #{response.code} #{response.message}: #{response.body}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment