-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostmark.cfc
365 lines (350 loc) · 10.5 KB
/
postmark.cfc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
component {
function init(
required string apiKey
, string spoolDir= "ram:///postmark"
, string defaultFrom= ""
, string defaultReplyTo= ""
, string defaultBCC= ""
, boolean compress= false
, numeric httpTimeOut= 120
, boolean debug
) {
arguments.debug = ( arguments.debug ?: request.debug ?: false );
this.apiUrl= "https://api.postmarkapp.com/";
this.httpTimeOut= arguments.httpTimeOut;
this.compress= arguments.compress;
this.debug= arguments.debug;
this.addRack(
rack= "default"
, apiKey= arguments.apiKey
, spoolDir= arguments.spoolDir
, compress= arguments.compress
);
this.bounceTypes= [
"HardBounce"
, "Transient"
, "Unsubscribe"
, "Subscribe"
, "AutoResponder"
, "AddressChange"
, "DnsError"
, "SpamNotification"
, "OpenRelayTest"
, "Unknown"
, "SoftBounce"
, "VirusNotification"
, "ChallengeVerification"
, "BadEmailAddress"
, "SpamComplaint"
, "ManuallyDeactivated"
, "Unconfirmed"
, "Blocked"
];
this.blankMail= {
"To"= ""
, "Bcc"= arguments.defaultBCC
, "From"= arguments.defaultFrom
, "ReplyTo"= arguments.defaultReplyTo
, "Subject"= ""
, "HtmlBody"= ""
, "TextBody"= ""
, "Tag"= ""
};
return this;
}
function debugLog( required input ) {
if ( structKeyExists( request, "log" ) && isCustomFunction( request.log ) ) {
if ( isSimpleValue( arguments.input ) ) {
request.log( "Postmark: " & arguments.input );
} else {
request.log( "Postmark: (complex type)" );
request.log( arguments.input );
}
} else if( this.debug ) {
var info= ( isSimpleValue( arguments.input ) ? arguments.input : serializeJson( arguments.input ) );
cftrace(
var= "info"
, category= "Postmark"
, type= "information"
);
}
return;
}
string function htmlCompressFormat(required string html) {
return reReplace( arguments.html, "[[:space:]]{2,}", chr( 13 ), "all" );
}
function addRack( required string rack, required string apiKey, string spoolDir= "ram:///postmark-#arguments.rack#", boolean compress= true ) {
this.rack[ arguments.rack ]= {
apiKey= arguments.apiKey
, spoolDir= arguments.spoolDir
, secure= arguments.secure
, compress= arguments.compress
};
if ( len( arguments.spoolDir ) && !directoryExists( arguments.spoolDir ) ) {
directoryCreate( arguments.spoolDir );
}
return;
}
struct function getBlankMail() {
var mail= duplicate( this.blankMail );
structAppend( mail, arguments, true );
return mail;
}
/*
* mail structure
* - subject
* - htmlBody
* - textBody
* - from
* - replyTo
* - to
* - bcc
* - tag
*/
struct function sendMail( required struct mail, boolean spool= false, string send= true, string rack= "default" ) {
var thisRack= this.rack[ arguments.rack ];
var out= {
success= false
};
var args= {
"To"= arguments.mail.to
, "Bcc"= arguments.mail.bcc
, "From"= arguments.mail.from
, "ReplyTo"= arguments.mail.replyTo
, "Subject"= arguments.mail.subject
, "HtmlBody"= arguments.mail.htmlBody
, "TextBody"= arguments.mail.textBody
, "Tag"= ( arguments.mail.tag ?: "" )
};
var json= "";
if ( structKeyExists( arguments.mail, "headers" ) ) {
args[ "Headers" ]= arguments.mail.headers;
}
if ( thisRack.compress ) {
args[ "HtmlBody" ]= this.htmlCompressFormat( args.HtmlBody );
}
json= serializeJSON( args, false, false );
this.debugLog( "!!Send mail with postmark to #arguments.mail.to#" );
this.debugLog( args );
if ( arguments.send ) {
if ( !arguments.spool ) {
out= this.apiRequest( uri= "email", json= json, rack= arguments.rack );
if ( !out.success ) {
arguments.spool= true;
}
}
if ( len( thisRack.spoolDir ) && arguments.spool ) {
var fn= "#thisRack.spoolDir#/send_#getTickCount()#_#randRange( 1, 10000 )#.json";
fileWrite( fn, json );
this.debugLog( "Spooled mail to #fn#" );
out.success= true;
}
}
return out;
}
struct function processSpool( numeric threads= 1, string rack= "default" ) {
var thisRack= this.rack[ arguments.rack ];
var json= "";
var out= {};
if( !len( thisRack.spoolDir ) ) {
return out;
}
var aMail= directoryList( thisRack.spoolDir, false, "path", "*.json", "DateLastModified", "file" );
// lucee multithreading
if( structKeyExists( server, "lucee" ) ) {
arrayEach( aMail, function( fn ) {
out[ fn ]= "";
lock type="exclusive" name="postmark_#fn#" timeout="0" {
if ( fileExists( fn ) ) {
json= fileRead( fn );
if ( len( json ) ) {
var send= this.apiRequest( uri= "email", json= json, rack= arguments.rack );
out[ fn ]= ( send.success ? "sent" : "error:" & send.error );
if( send.success ) {
fileDelete( fn );
}
} else {
out[ fn ]= "error: empty json file";
}
} else {
out[ fn ]= "error: file #fn# is missing";
}
}
}, ( arguments.threads > 1 ), arguments.threads );
} else {
arrayEach( aMail, function( fn ) {
out[ file ]= "";
lock type="exclusive" name="postmark_#fn#" timeout="0" {
if ( fileExists( fn ) ) {
json= fileRead( fn );
if ( len( json ) ) {
var send= this.apiRequest( uri= "email", json= json, rack= arguments.rack );
out[ fn ]= ( send.success ? "sent" : "error:" & send.error );
if( send.success ) {
fileDelete( fn );
}
} else {
out[ fn ]= "error: empty json file";
}
} else {
out[ fn ]= "error: file #fn# is missing";
}
}
} );
}
return out;
}
/**
* Fetches a portion of bounces according to the specified input criteria. Supported filters: type, inactive, email like, tag.
*/
function getBounces( string type= "", string inactive= "", string emailFilter= "", string tag= "", numeric start= 0, numeric limit= 25, string rack= "default" ) {
var out= this.apiRequest(
uri= "bounces"
, type= arguments.type
, emailFilter= arguments.emailFilter
, tag= arguments.tag
, offset= arguments.start
, count= arguments.limit
, rack= arguments.rack
);
return out;
}
/**
* Get details about a single bounce. Note that the bounce ID is a numeric value that you typically obtain after a getting a list of bounces.
*/
function getBounce( required string id, string rack= "default" ) {
var out= this.apiRequest(
uri= "bounces/#arguments.id#"
, rack= arguments.rack
);
return out;
}
/**
* Returns the raw source of the bounce we accepted. If Postmark does not have a dump for that bounce, it will return an empty string.
*/
function getBounceDump( required string id, string rack= "default" ) {
var out= this.apiRequest(
uri= "bounces/#arguments.id#/dump"
, rack= arguments.rack
);
return out;
}
/**
* Activates a deactivated bounce. Note that you do not need to send anything in the request body.
*/
function retryBounce( required string id, string rack= "default" ) {
var out= this.apiRequest(
uri= "bounces/#arguments.id#/activate"
, rack= arguments.rack
);
return out;
}
/**
* Returns a list of tags used for the current server.
*/
function getTags( string rack= "default" ) {
var out= this.apiRequest(
uri= "bounces/tags"
, rack= arguments.rack
);
return out;
}
/**
* Returns a summary of inactive emails and bounces by type
*/
function deliveryStats( string rack= "default" ) {
var i= "";
var out= this.apiRequest(
uri= "deliverystats"
, rack= arguments.rack
);
out.bounces= { "All"= 0 };
for ( i in this.bounceTypes ) {
out.bounces[ i ]= 0;
}
out.inactive= 0;
if ( out.success ) {
for ( i in out.response.bounces ) {
if ( structKeyExists( i, "Type" ) ) {
out.bounces[ i.Type ]= i.Count;
} else {
out.bounces[ "All" ]= i.Count;
}
}
out.inactive= out.response.InactiveMails;
}
return out;
}
struct function apiRequest( required string uri, string rack= "default" ) {
var thisRack= this.rack[ arguments.rack ];
var http= {};
var item= "";
var out= {
success= false
, verb= "GET"
, url= this.apiUrl & arguments.uri
, error= ""
, status= ""
, statusCode= 0
, response= ""
};
structDelete( arguments, "rack" );
structDelete( arguments, "uri" );
if ( structKeyExists( arguments, "json" ) ) {
out.verb= "POST";
}
for ( item in arguments ) {
if ( len( arguments[ item ] ) && item != "json" ) {
out.url &= ( find( "?", out.url ) ? "&" : "?" ) & lCase( item ) & "=" & urlEncodedFormat( arguments[ item ] );
}
}
this.debugLog( "POSTMARK: #out.verb# #out.url#" );
if ( this.debug ) {
this.debugLog( duplicate( arguments ) );
}
cfhttp( result="http", method=out.verb, url=out.url, charset="utf-8", throwOnError=false, timeOut=this.httpTimeOut ) {
cfhttpparam( name="Accept", type="header", value="application/json" );
cfhttpparam( name="Content-Type", type="header", value="application/json" );
cfhttpparam( name="X-Postmark-Server-Token", type="header", value=thisRack.apiKey );
if ( structKeyExists( arguments, "json" ) && len( arguments.json ) ) {
cfhttpparam( type="body", value=arguments.json );
}
}
out.response= toString( http.fileContent );
//this.debugLog( out.response );
out.statusCode = http.responseHeader.Status_Code ?: 500;
if ( out.statusCode == "401" ) {
out.error= "401 unauthorized";
} else if ( out.statusCode == "422" ) {
out.error= "422 unprocessable";
} else if ( out.statusCode == "500" ) {
out.error= "500 server error";
} else if ( listFind( "4,5", left( out.statusCode, 1 ) ) ) {
out.error= "#out.statusCode# unknown error";
} else if ( out.statusCode == "" ) {
out.error= "unknown error, no status code";
} else if ( out.response == "Connection Timeout" || out.response == "Connection Failure" ) {
out.error= out.response;
} else if ( out.statusCode == "200" ) {
// out.success
out.success= true;
}
// parse response
if ( out.success ) {
try {
if ( left( out.response, 1 ) == "{" ) {
out.response= deserializeJSON( out.response );
} else {
out.error= "Non-JSON Response: #out.response#";
}
} catch (any cfcatch) {
out.error= "JSON Error: " & (cfcatch.message?:"No catch message") & " " & (cfcatch.detail?:"No catch detail");
}
}
if ( len( out.error ) ) {
out.success= false;
}
this.debugLog( out.statusCode & " " & out.error );
return out;
}
}