Class: MonkeyMailer::Adapters::MandrilAPI
- Inherits:
-
Object
- Object
- MonkeyMailer::Adapters::MandrilAPI
- Defined in:
- lib/monkey-mailer/adapters/mandrilapi.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ MandrilAPI
constructor
A new instance of MandrilAPI.
- #parse_recipients(recipients_list, names_list = "") ⇒ Object
- #send_email(email) ⇒ Object
Constructor Details
#initialize(options) ⇒ MandrilAPI
Returns a new instance of MandrilAPI.
12 13 14 |
# File 'lib/monkey-mailer/adapters/mandrilapi.rb', line 12 def initialize() @key = [:mandril_api_key] end |
Instance Method Details
#parse_recipients(recipients_list, names_list = "") ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/monkey-mailer/adapters/mandrilapi.rb', line 60 def parse_recipients(recipients_list, names_list = "") recipients = recipients_list.split(",") names = names_list.to_s.split(",") to_list = [] raise RuntimeError.new("No recipients specified") if recipients.empty? if names.any? && (names.count != recipients.count) raise RuntimeError.new("Recipients and Names lists don't match") end recipients.each_with_index do |recipient, idx| to_list << { "email" => recipient.strip, "name" => names[idx] ? names[idx].strip : "", "type" => "to" } end to_list end |
#send_email(email) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/monkey-mailer/adapters/mandrilapi.rb', line 16 def send_email(email) request_body = { :key => @key, :message => { :to => parse_recipients(email.to_email, email.to_name), :from_name => email.from_name, :from_email => email.from_email, :subject => email.subject, :html => email.body, :text => email.body.to_s.gsub(/<\/?[^>]*>/, ""), :headers => {}, :track_opens => true, :track_clicks => true, :auto_text => true, :url_strip_qs => true, :preserve_recipients => false, :bcc_address => '', :attachments => [] }, :async => false } email..each do || request_body[:message][:attachments] << { :type => .content_type, :name => File.basename(.file_path), :content => Base64.encode64(File.read(.file_path)) } end uri = URI('https://mandrillapp.com') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new('/api/1.0/messages/send.json', initheader = {'Content-Type' =>'application/json'}) request.body = request_body.to_json response = http.start {|http| http.request(request)} raise MonkeyMailer::DeliverError.new("Mandril response.code not equal to 200") unless response.code.to_i == 200 puts "Response #{response.code} #{response.}: #{response.body}" end |