-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.rb
40 lines (34 loc) · 1.07 KB
/
mail.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
#!/usr/bin/env ruby
require 'mandrill'
require 'thread'
Excon.defaults[:ssl_verify_peer] = false
$mandrill = Mandrill::API.new 'TNYvuLR_xCXZg4L_-mKyfQ'
$mailQueue = Queue.new
module Mail
# Params = from:User, to:Array (of emails), subject:String, text:String
def Mail.send(from, to, subject , text)
message = {"text"=>text,
"to"=> to.map { |mail| {"type"=> "to", "email"=> mail} },
"preserve_recipients"=>true,
"from_name"=>from.fullname,
"from_email"=>from.email,
"subject"=>subject}
$mailQueue << message
end
end
Thread.new do
while true
if $mailQueue.empty?
sleep 1
else
begin
$mandrill.messages.send $mailQueue.pop, false, "Main Pool", nil
rescue Mandrill::Error => e
# Mandrill errors are thrown as exceptions
puts "A mandrill error occurred: #{e.class} - #{e.message}"
# A mandrill error occurred: Mandrill::UnknownSubaccountError - No subaccount exists with the id 'customer-123'
return false
end
end
end
end