forked from vespina/sendgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendgrid.PRG
More file actions
382 lines (318 loc) · 8.68 KB
/
sendgrid.PRG
File metadata and controls
382 lines (318 loc) · 8.68 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
* SENDGRID.PRG
*
* SMALL UTILITY TO SEND EMAILS USING SENDGRID
* WEB PLATFORM.
*
* USAGE:
*
* DO SendGrid
* SG.Initialize(senderEmail, senderName, APIKey)
*
* LOCAL oMsg
* oMsg = SG.New()
* WITH oMsg
* .addRecipient("foo1@gmail.com","Foo1")
* .addRecipient("foo2@gmail.com","Foo2")
* .addCC("foo3@gmail.com", "Foo3")
* .Subject = "SendGrid Test"
* .Body = "This is a test email"
* .appendBody(" from SendGrid library")
* .addAttachment("c:\folder\file1.bmp")
* .addAttachment("c:\folder\file2.bmp","application/octet","logo.bmp")
* .addInlineAttachment("c:\folder\file3.txt","text/plain")
* ENDWITH
*
* LOCAL lResult
* lResult = SG.Send(oMsg)
* IF lResult
* ??"SENT!"
* ELSE
* FOR i = 1 TO SG.Errors.Count
* ?SG.Errors(i).MEssage, SG.Errors(i).Field, SG.Errors(i).Help
* ENDFOR
* ENDIF
*
checkForNQInclude()
IF !FILE("vfplegacy.prg") OR !FILE("vfplegacy.h")
IF NOT NQInclude("vfplegacy")
CANCEL
ENDIF
MESSAGEBOX("This library requires VFPLEGACY.PRG library wich has been automatically downloaded. Please, erase SENDGRID.FXP file and try again",48,"SENDGRID.PRG")
CANCEL
ENDIF
IF NOT NQInclude("json") ;
OR NOT NQInclude("base64Helper")
CANCEL
ENDIF
SET PROCEDURE TO vfplegacy ADDITIVE
DO JSON
DO Base64Helper
PUBLIC SG
SG = CREATEOBJECT("SendGridHelper")
RETURN
DEFINE CLASS sendGridHelper AS Custom
Version = "1.0"
SenderEmail = ""
SenderName = ""
APIKey = ""
targetUrl = "https://api.sendgrid.com/v3/mail/send"
Errors = NULL
lastPayload = ""
lastResp = NULL
* Initialize (method)
*
* INITIALIZE HELPER CONFIGURATION VALUES
*
PROCEDURE Initialize(pcSenderEmail, pcSenderName, pcAPIKey)
THIS.senderEmail = pcSenderEmail
THIS.senderName = pcSenderName
THIS.APIKey = pcAPIKey
THIS.Errors = CREATEOBJECT("Collection")
RETURN
* New (method)
*
* RETURNS A NEW SendGridMessage INSTANCE
*
PROCEDURE New
RETURN CREATEOBJECT("sendGridMsg")
* Send (method)
*
* SENDS AN EMAIL USING SENDGRID PLATFORM
*
PROCEDURE Send(poMsg)
LOCAL cEndPoint,cHeaders,oResp
cEndpoint = THIS.targetUrl
cHeaders = "Authorization: Bearer " + THIS.APIKey
THIS.lastPayload = poMsg.ToJSON()
THIS.lastResp = JSON.httpPost(cEndpoint, THIS.lastPayload, cHeaders)
THIS.Errors = CREATEOBJECT("Collection")
oResp = THIS.lastResp
DO CASE
CASE BETWEEN(oResp.statusCode,200,299)
lResult = .T.
CASE oResp.hasError
lResult = .F.
THIS.Errors = oResp.lastError
CASE oResp.statusCode <> 200
lResult = .F.
IF ATC("json",oResp.contentType)<>0
THIS.Errors = JSON.Parse(oResp.raw)
IF !JSON.lastError.hasError
THIS.Errors = THIS.Errors.Errors
ELSE
THIS.Errors = JSON.lastError.Message
ENDIF
ELSE
THIS.Errors = oResp.raw
ENDIF
ENDCASE
IF TYPE("THIS.Errors") = "C"
THIS.Errors = JSON.Parse([{ "message": "] +THIS.Errors + [", "field": null, "help": null }])
ENDIF
RETURN lResult
ENDDEFINE
DEFINE CLASS sendGridMsg AS Custom
To = NULL
CC = NULL
Subject = ""
Body = ""
htmlBody = ""
Attachments = NULL
lastError = ""
* Constructor
*
PROCEDURE Init
THIS.To = CREATEOBJECT("Collection")
THIS.CC = CREATEOBJECT("Collection")
THIS.Attachments = CREATEOBJECT("Collection")
RETURN
* addRecipient (method)
*
* ADDS A RECIPIENT TO THE "TO" LIST
*
PROCEDURE addRecipient(pcEmail, pcName)
THIS.TO.Add( CREATEOBJECT("sendGridRecipient", pcEMail, pcName) )
RETURN
* addCC (method)
*
* ADDS A RECIPIENT TO THE "CC" LIST
*
PROCEDURE addCC(pcEmail, pcName)
THIS.CC.Add( CREATEOBJECT("sendGridRecipient", pcEMail, pcName) )
RETURN
* apendBody (method)
*
* APPENDS TEXT TO THE BODY STRING
*
PROCEDURE appendBody(pcText)
THIS.body = THIS.body + pcText
RETURN
* apendHtmlBody (method)
*
* APPENDS TEXT TO THE HTML BODY STRING
*
PROCEDURE appendHtmlBody(pcText)
THIS.htmlBody = THIS.htmlBody + pcText
RETURN
* addAttachment (method)
*
* ADDS A NEW ITEM TO THE ATTACHMENTS LIST
*
PROCEDURE addAttachment(pcFile, pcMIME, pcFullName, pcKind)
LOCAL oAttach
oAttach = CREATEOBJECT("sendGridAttachment", pcFile)
IF NOT oAttach.IsValid
THIS.lastError = oAttach.lastError
RETURN .F.
ENDIF
IF !EMPTY(pcMIME)
oAttach.MIMEType = pcMIME
ENDIF
IF !EMPTY(pcFullName)
oAttach.FullName = pcFullName
IF EMPTY(JUSTEXT(oAttach.FullName))
oAttach.fullName = oAttach.FullName + "." + JUSTEXT(pcFile)
ENDIF
ENDIF
IF !EMPTY(pcKind)
oAttach.Inline = (LOWER(pcKind) == "inline")
ENDIF
THIS.Attachments.Add(oAttach)
RETURN
* addInlineAttachment (method)
*
* ADDS AN INLINE ATTACHMENT
*
PROCEDURE addInlineAttachment(pcFile, pcMIME, pcFullName)
RETURN THIS.addAttachment(pcFile, pcMIME, pcFullName, "inline")
* ToJSON (method)
*
* RETURN THE JSON REPRESENTATION OF THE MESSAGE
* TO BE SENT
*
PROCEDURE ToJSON
LOCAL cTO,cCC,cAttachs,i,oAttach,oRecipient,cContentType,cContent,cJSON
STORE "" TO cTO,cCC,cAttachs
FOR i = 1 TO THIS.TO.Count
oRecipient = THIS.TO.Item(i)
cTO = cTO + IIF(i=1,"",",") + oRecipient.ToJSON()
ENDFOR
FOR i = 1 TO THIS.CC.Count
oRecipient = THIS.CC.Item(i)
cCC = cCC + IIF(i=1,"",",") + oRecipient.ToJSON()
ENDFOR
FOR i = 1 TO THIS.Attachments.Count
oAttach = THIS.Attachments.Item(i)
cAttachs = cAttachs + IIF(i=1,"",",") + oAttach.ToJSON()
ENDFOR
IF EMPTY(THIS.htmlBody)
cContentType = "text/plain"
cContent = THIS.Body
ELSE
cContentType = "text/html"
cContent = THIS.htmlBody
ENDIF
cJSON = ""
TEXT TO cJSON TEXTMERGE NOSHOW
{
"from": { "email":"<<SG.senderEmail>>","name":"<<SG.senderName>>" },
"personalizations":[
{
"to": [<<cTO>>]
,"cc": [<<cCC>>]
,"subject": "<<THIS.subject>>"
}
],
"content": [
{"type": "<<cContentType>>", "value": "<<cContent>>"}
]
,"Attachments": [<<cAttachs>>]
}
ENDTEXT
IF EMPTY(cCC)
cJSON = STRT(cJSON,',"cc": []', "")
ENDIF
IF EMPTY(cAttachs)
cJSON = STRT(cJSON,',"Attachments": []', "")
ENDIF
RETURN cJSON
ENDDEFINE
DEFINE CLASS sendGridRecipient AS Custom
Email = ""
fullName = ""
PROCEDURE Init(pcEmail, pcName)
THIS.Email = pcEmail
THIS.fullName = EVL(pcName,"")
RETURN
PROCEDURE ToJSON
RETURN [{"email":"] + THIS.email + [","name":"] + THIS.fullName + ["}]
ENDDEFINE
DEFINE CLASS sendGridAttachment AS Custom
fileName = ""
fullName = ""
MIMEType = ""
Inline = .F.
Content = ""
IsValid = .F.
lastError = ""
PROCEDURE Init(pcFile)
IF NOT FILE(pcFile)
THIS.lastError = "File '" + pcFile + "' not found"
RETURN
ENDIF
THIS.fileName = FULLPATH(pcFile)
THIS.fullName = JUSTFNAME(pcFile)
THIS.Content = B64.encodeFile(THIS.fileName, .T.)
THIS.Inline = .F.
THIS.IsValid = .T.
LOCAL cExt
cExt = UPPER(JUSTEXT(pcFile))
DO CASE
CASE INLIST(cExt,"TXT","CSV")
THIS.MIMEType = "text/plain"
CASE INLIST(cExt,"HTM","HTML")
THIS.MIMEType = "text/html"
CASE cExt = "PDF"
THIS.MIMEType = "application/pdf"
CASE cExt = "JSON"
THIS.MIMEType = "application/json"
CASE cExt = "XML"
THIS.MIMEType = "application/xml"
CASE INLIST(cExt,"MP4","F4A","F4B","M4V","F4P","F4V")
THIS.MIMEType = "audio/mp4"
OTHERWISE
THIS.MIMEType = "application/octet-stream"
ENDCASE
RETURN
PROCEDURE ToJSON
RETURN [{] +;
[ "content": "] + THIS.content + [",] + ;
[ "type": "] + THIS.MIMEType + [",] + ;
[ "filename": "] + THIS.fullName + [",] + ;
[ "disposition": "] + IIF(THIS.inline,"inline","attachment") + ["] + ;
[ }]
ENDDEFINE
PROCEDURE checkForNQInclude
IF NOT FILE("nqinclude.prg")
STRTOFILE(httpGetFile("https://raw.githubusercontent.com/vespina/nqinclude/main/nqinclude.prg"),"nqinclude.prg")
IF NOT FILE("nqinclude.prg")
MESSAGEBOX("This library requires NQINCLUDE.PRG wich could not be downloaded at this time",48,"JSON.PRG")
CANCEL
ENDIF
ENDIF
RETURN
PROCEDURE httpGetFile(pcUrl)
pnTimeout = IIF(VARTYPE(pnTimeOut)<>"N",15,pnTimeout)
LOCAL oHTTP
oHTTP = CREATEOBJECT("MSXML2.XMLHTTP")
oHTTP.open("GET", pcUrl, .F.)
oHTTP.Send()
LOCAL nTimeOut
nTimeout = SECONDS()
DO WHILE oHTTP.readyState<>4 OR (SECONDS() - nTimeout) > 15
DOEVENTS
ENDDO
IF oHTTP.readyState <> 4 OR !BETWEEN(oHTTP.status,200,299)
RETURN ""
ENDIF
RETURN oHTTP.responseText()