forked from ActiveCampaign/postmark-gem
/
postmark.rb
206 lines (165 loc) · 4.93 KB
/
postmark.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require 'net/http'
require 'net/https'
require 'postmark/bounce'
require 'postmark/json'
require 'postmark/http_client'
require 'postmark/tmail_mail_extension'
require 'postmark/mail_message_extension'
module Postmark
class InvalidApiKeyError < StandardError; end
class UnknownError < StandardError; end
class InvalidMessageError < StandardError; end
class InternalServerError < StandardError; end
class UnknownMessageType < StandardError; end
module ResponseParsers
autoload :Json, 'postmark/response_parsers/json'
autoload :ActiveSupport, 'postmark/response_parsers/active_support'
autoload :Yajl, 'postmark/response_parsers/yajl'
end
HEADERS = {
'Content-type' => 'application/json',
'Accept' => 'application/json'
}
MAX_RETRIES = 2
class << self
attr_accessor :host, :path_prefix, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
:proxy_host, :proxy_port, :proxy_user, :proxy_pass, :max_retries, :sleep_between_retries
attr_writer :response_parser_class
def response_parser_class
@response_parser_class ||= Object.const_defined?(:ActiveSupport) ? :ActiveSupport : :Json
end
# The port on which your Postmark server runs.
def port
@port || (secure ? 443 : 80)
end
# The host to connect to.
def host
@host ||= 'api.postmarkapp.com'
end
# The path of the listener
def path_prefix
@path_prefix ||= '/'
end
def http_open_timeout
@http_open_timeout ||= 5
end
def http_read_timeout
@http_read_timeout ||= 15
end
def max_retries
@max_retries ||= 3
end
def sleep_between_retries
@sleep_between_retries ||= 10
end
def configure
yield self
end
def send_through_postmark(message) #:nodoc:
@retries = 0
begin
HttpClient.post("email", Postmark::Json.encode(convert(message)))
rescue Exception => e
if @retries < max_retries
@retries += 1
retry
else
raise
end
end
end
def convert(message)
if message.is_a?(TMail::Mail)
convert_tmail(message)
else
convert_mail(message)
end
end
def delivery_stats
HttpClient.get("deliverystats")
end
protected
def convert_mail(message)
options = { "From" => message.from.to_s, "Subject" => message.subject }
headers = extract_mail_headers(message)
options["Headers"] = headers unless headers.length == 0
options["To"] = message.to.join(", ")
options["Tag"] = message.tag.to_s unless message.tag.nil?
options["Cc"] = message.cc.join(", ").to_s unless message.cc.nil?
options["Bcc"] = message.bcc.join(", ").to_s unless message.bcc.nil?
if reply_to = message['reply-to']
options["ReplyTo"] = reply_to.to_s
end
html = message.body_html
text = message.body_text
if message.multipart?
options["HtmlBody"] = html
options["TextBody"] = text
elsif html
options["HtmlBody"] = html
else
options["TextBody"] = text
end
options
end
def extract_mail_headers(message)
headers = []
message.header.fields.each do |field|
key = field.name
value = field.value
next if bogus_headers.include? key.dup.downcase
name = key.split(/-/).map {|i| i.capitalize }.join('-')
headers << { "Name" => name, "Value" => value }
end
headers
end
def convert_tmail(message)
options = { "From" => message['from'].to_s, "To" => message['to'].to_s, "Subject" => message.subject }
headers = extract_tmail_headers(message)
options["Headers"] = headers unless headers.length == 0
options["Tag"] = message.tag.to_s unless message.tag.nil?
options["Cc"] = message['cc'].to_s unless message.cc.nil?
options["Bcc"] = message['bcc'].to_s unless message.bcc.nil?
if reply_to = message['reply-to']
options["ReplyTo"] = reply_to.to_s
end
html = message.body_html
text = message.body_text
if message.multipart?
options["HtmlBody"] = html
options["TextBody"] = text
elsif html
options["HtmlBody"] = message.body_html
else
options["TextBody"] = text
end
options
end
def extract_tmail_headers(message)
headers = []
message.each_header do |key, value|
next if bogus_headers.include? key.dup.downcase
name = key.split(/-/).map {|i| i.capitalize }.join('-')
headers << { "Name" => name, "Value" => value.body }
end
headers
end
def bogus_headers
%q[
return-path
x-pm-rcpt
from
reply-to
sender
received
date
content-type
cc
bcc
subject
tag
]
end
end
self.response_parser_class = nil
end