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.attachments.each do |attachment|
request_body[:message][:attachments] << {
:type => attachment.content_type,
:name => File.basename(attachment.file_path),
:content => Base64.encode64(File.read(attachment.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', = {'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.message}: #{response.body}"
end
|