-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ksAwsSes.pas
554 lines (478 loc) · 16.3 KB
/
ksAwsSes.pas
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
{*******************************************************************************
* *
* ksSES - AWS SES Interface *
* *
* https://github.com/gmurt/ksAWS *
* *
* Copyright 2020 Graham Murt *
* *
* email: [email protected] *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*******************************************************************************}
unit ksAwsSes;
interface
uses
Sysutils, Classes, ksAwsBase, IdMessage;
type
TEmailIdentity = record
Identity: string;
&Type: string;
Verified: Boolean;
end;
TEmailIdentities = TArray<TEmailIdentity>;
IksAwsSesMessage = interface
['{A58BC820-722F-4E68-8DE6-029A007FA29F}']
function GetBcc: TStrings;
function GetBody: string;
function GetCc: TStrings;
function GetRecipients: TStrings;
function GetReplyTo: TStrings;
function GetSender: string;
function GetSubject: string;
function GetReturnPath: string;
procedure SetBody(const Value: string);
procedure SetSender(const Value: string);
procedure SetSubject(const Value: string);
procedure SetReturnPath(const Value: string);
property Sender: string read GetSender write SetSender;
property Recipients: TStrings read GetRecipients;
property ReplyTo: TStrings read GetReplyTo;
property ReturnPath: string read GetReturnPath write SetReturnPath;
property CC: TStrings read GetCc;
property Bcc: TStrings read GetBcc;
property Subject: string read GetSubject write SetSubject;
property Body: string read GetBody write SetBody;
end;
IksAwsSES = interface
['{95F8984E-2A05-4071-B906-67CDBCBEA51A}']
function GetVerificationStatus(AEmail: string): string;
function ListEmailIdentities: TEmailIdentities;
procedure CreateVerificationTemplate(ATemplateName, AFromEmail, ASubject, ABody: string);
procedure DeleteVerificationTemplate(ATemplateName: string);
procedure SendVerificationTemplate(ATemplateName, ARecipient: string);
procedure GetSenders(ASenders: TStrings; const ALimit: integer = 0);
procedure DeleteIdentity(AIdentity: string);
function SendEmail(AMessage: IksAwsSesMessage): integer;
procedure SendEmailSmtp(AMsg: TIdMessage);
procedure VerifyEmailIdentity(AEmailAddress: string);
end;
function CreateSes(AAccessKey, ASecretKey: string; ARegion: TksAwsRegion): IksAwsSES;
function CreateSesMessage(ARecipient, ASender, ASubject, ABody: string): IksAwsSesMessage; overload;
function CreateSesMessage(ARecipients: TStrings; ASender, ASubject, ABody: string): IksAwsSesMessage; overload;
implementation
uses ksAwsConst, Xml.xmldom, Xml.XMLIntf, Xml.XMLDoc, Math, ksAwsHash, JsonDataObjects,
IdSmtp, IdSSL, IdSSLOpenSSL, IdExplicitTLSClientServerBase;
type
TksAwsSesMessage = class(TInterfacedObject, IksAwsSesMessage)
private
FSender: string;
FSubject: string;
FBody: string;
FHtml: string;
FRecipients: TStrings;
FReplyTo: TStrings;
FCc: TStrings;
FBcc: TStrings;
FReturnPath: string;
function GetBcc: TStrings;
function GetBody: string;
function GetCc: TStrings;
function GetRecipients: TStrings;
function GetReplyTo: TStrings;
function GetSender: string;
function GetSubject: string;
procedure SetBody(const Value: string);
procedure SetSender(const Value: string);
procedure SetSubject(const Value: string);
function GetHtml: string;
procedure SetHtml(const Value: string);
function GetReturnPath: string;
procedure SetReturnPath(const Value: string);
protected
property Sender: string read GetSender write SetSender;
property Recipients: TStrings read GetRecipients;
property CC: TStrings read GetCc;
property Bcc: TStrings read GetBcc;
property ReturnPath: string read GetReturnPath write SetReturnPath;
property Subject: string read GetSubject write SetSubject;
property Body: string read GetBody write SetBody;
property Html: string read GetHtml write SetHtml;
public
constructor Create; virtual;
destructor Destroy; override;
end;
TksAwsSES = class(TksAwsBaseService, IksAwsSES)
private
//function BuildDestinationParams(AName: string; ARecipients, AParams: TStrings): string;
{ Private declarations }
protected
function GetServiceName: string; override;
function GetHostSubdomain: string; override;
function GetVerificationStatus(AEmail: string): string; overload;
function ListEmailIdentities: TEmailIdentities;
procedure CreateVerificationTemplate(ATemplateName, AFromEmail, ASubject, ABody: string);
procedure DeleteVerificationTemplate(ATemplateName: string);
procedure SendVerificationTemplate(ATemplateName, ARecipient: string);
procedure DeleteIdentity(AIdentity: string);
procedure GetSenders(ASenders: TStrings; const AMaxItems: integer = 0);
function SendEmail(AMessage: IksAwsSesMessage): integer;
procedure SendEmailSmtp(AMessage: TIdMessage);
procedure VerifyEmailIdentity(AEmailAddress: string);
public
{ Public declarations }
end;
function CreateSes(AAccessKey, ASecretKey: string; ARegion: TksAwsRegion): IksAwsSES;
begin
Result := TksAwsSES.Create(AAccessKey, ASecretKey, ARegion);
end;
function CreateSesMessage(ARecipient, ASender, ASubject, ABody: string): IksAwsSesMessage;
var
ARecipients: TStrings;
begin
ARecipients := TStringList.Create;
try
ARecipients.Add(ARecipient);
Result := CreateSesMessage(ARecipients, ASender, ASubject, ABody);
finally
ARecipients.Free;
end;
end;
function CreateSesMessage(ARecipients: TStrings; ASender, ASubject, ABody: string): IksAwsSesMessage;
var
ARecipient: string;
begin
Result := TksAwsSesMessage.Create;
for ARecipient in ARecipients do
Result.Recipients.Add(ARecipient);
Result.Sender := ASender;
Result.Subject := ASubject;
Result.Body := ABody;
end;
{ TksAwsSesMessage }
constructor TksAwsSesMessage.Create;
begin
FRecipients := TStringList.Create;
FCc := TStringList.Create;
FBcc := TStringList.Create;
FReplyTo := TStringList.Create;
FReturnPath := '';
end;
destructor TksAwsSesMessage.Destroy;
begin
FRecipients.Free;
FCc.Free;
FBcc.Free;
FReplyTo.Free;
inherited;
end;
function TksAwsSesMessage.GetBcc: TStrings;
begin
Result := FBcc;
end;
function TksAwsSesMessage.GetBody: string;
begin
Result := FBody;
end;
function TksAwsSesMessage.GetCc: TStrings;
begin
Result := FCc;
end;
function TksAwsSesMessage.GetHtml: string;
begin
Result := FHtml;
end;
function TksAwsSesMessage.GetRecipients: TStrings;
begin
Result := FRecipients;
end;
function TksAwsSesMessage.GetReplyTo: TStrings;
begin
Result := FReplyTo;
end;
function TksAwsSesMessage.GetReturnPath: string;
begin
Result := FReturnPath;
end;
function TksAwsSesMessage.GetSender: string;
begin
Result := FSender;
end;
function TksAwsSesMessage.GetSubject: string;
begin
Result := FSubject;
end;
procedure TksAwsSesMessage.SetBody(const Value: string);
begin
FBody := Value;
end;
procedure TksAwsSesMessage.SetHtml(const Value: string);
begin
FHtml := Value;
end;
procedure TksAwsSesMessage.SetReturnPath(const Value: string);
begin
FReturnPath := Value;
end;
procedure TksAwsSesMessage.SetSender(const Value: string);
begin
FSender := Value;
end;
procedure TksAwsSesMessage.SetSubject(const Value: string);
begin
FSubject := Value;
end;
{ TksAwsSES }
procedure TksAwsSES.CreateVerificationTemplate(ATemplateName, AFromEmail, ASubject, ABody: string);
var
AJson: TJsonObject;
begin
AJson := TJsonObject.Create;
try
AJson.S['TemplateName'] := ATemplateName;
AJson.S['FromEmailAddress'] := AFromEmail;
AJson.S['TemplateSubject'] := ASubject;
AJson.S['TemplateContent'] := ABody;
AJson.S['SuccessRedirectionURL'] := '';
AJson.S['FailureRedirectionURL'] := '';
ExecuteHttp('POST', 'CreateCustomVerificationEmailTemplate', Host, '/v2/email/custom-verification-email-templates', nil, nil, AJson.ToString);
finally
AJson.Free;
end;
end;
procedure TksAwsSES.DeleteVerificationTemplate(ATemplateName: string);
begin
ExecuteHttp('DELETE', 'DeleteCustomVerificationEmailTemplate', Host, '/v2/email/custom-verification-email-templates/'+ATemplateName, nil, nil, '');
end;
procedure TksAwsSES.SendVerificationTemplate(ATemplateName, ARecipient: string);
var
AJson: TJsonObject;
begin
AJson := TJsonObject.Create;
try
AJson.S['TemplateName'] := ATemplateName;
AJson.S['EmailAddress'] := ARecipient;
ExecuteHttp('POST', 'SendCustomVerificationEmail', Host, '/v2/email/outbound-custom-verification-emails', nil, nil, AJson.ToString);
finally
AJson.Free;
end;
end;
procedure TksAwsSES.DeleteIdentity(AIdentity: string);
var
AParams: TStrings;
begin
AParams := TStringList.Create;
try
AParams.Values['Identity'] := AIdentity;
ExecuteHttp('DELETE', 'DeleteIdentity', Host, '', nil, AParams, '');
finally
AParams.Free;
end;
end;
{function TksAwsSES.BuildDestinationParams(AName: string; ARecipients, AParams: TStrings): string;
var
ICount: integer;
AKey: string;
begin
if LowerCase(AName) = 'to' then AKey := 'ToAddresses';
if LowerCase(AName) = 'cc' then AKey := 'CcAddresses';
if LowerCase(AName) = 'bcc' then AKey := 'BccAddresses';
for ICount := 1 to ARecipients.Count do
AParams.Values['Destination.'+AKey+'.member.'+IntToStr(ICount)] := ARecipients[ICount-1];
end;}
function TksAwsSES.GetHostSubdomain: string;
begin
Result := 'email';
end;
procedure TksAwsSES.GetSenders(ASenders: TStrings; const AMaxItems: integer = 0);
var
AResponse: string;
AParams: TStrings;
AXml: IXmlDocument;
AIdentities: IXmlNode;
ICount: integer;
ANextToken: string;
AComplete: Boolean;
begin
ASenders.Clear;
ANextToken := '';
AComplete := False;
AXml := TXMLDocument.Create(nil);
while not AComplete do
begin
AParams := TStringList.Create;
try
AParams.Values['IdentityType'] := 'EmailAddress';
if AMaxItems > 0 then
AParams.Values['MaxItems'] := IntToStr(Min(AMaxItems, 1000));
AParams.Values['NextToken'] := ANextToken;
AResponse := ExecuteHttp('POST', 'ListIdentities', Host, '', nil, AParams, '').ContentAsString;
finally
AParams.Free;
end;
AXml.LoadFromXML(AResponse);
AIdentities := AXml.DocumentElement.ChildNodes['ListIdentitiesResult'];
ANextToken := UrlEncode(AIdentities.ChildNodes['NextToken'].Text);
AIdentities := AIdentities.ChildNodes['Identities'];
for ICount := 0 to AIdentities.ChildNodes.Count -1 do
begin
if (AMaxItems = 0) or (ASenders.Count < AMaxItems) then
begin
ASenders.Add(AIdentities.ChildNodes[ICount].Text);
end;
end;
AComplete := (ANextToken = '') or (ASenders.Count = AMaxItems);
if not AComplete then
Sleep(1000); // rate limit for AWS (no more than 1 call per second);
end;
end;
function TksAwsSES.GetServiceName: string;
begin
Result := C_SERVICE_SES;
end;
function TksAwsSES.GetVerificationStatus(AEmail: string): string;
var
AResponse: string;
AParams: TStrings;
ICount: integer;
AStrings: TStrings;
AStr: string;
begin
Result := '';
AStrings := TStringList.Create;
AParams := TStringList.Create;
try
AParams.Values['Identities.member.'+IntToStr(1)] := AEmail;
AResponse := ExecuteHttp('POST', 'GetIdentityVerificationAttributes', Host, '', nil, AParams, '').ContentAsString;
AStrings.Text := AResponse;
for ICount := 0 to AStrings.Count-1 do
begin
AStr := Trim(AStrings[ICount]).ToLower;
if Pos('<verificationstatus>', AStr) > 0 then
begin
AStr := StringReplace(AStr, '<verificationstatus>', '', []);
AStr := StringReplace(AStr, '</verificationstatus>', '', []);
Result := AStr;
Exit;
end;
end;
finally
AParams.Free;
AStrings.Free;
end;
end;
function TksAwsSES.ListEmailIdentities: TEmailIdentities;
var
AData: string;
AJson: TJsonObject;
AParams: TStrings;
ANextToken: string;
AObj: TJsonObject;
AIdentity: TEmailIdentity;
begin
Result := [];
AParams := TStringList.Create;
AJson := TJsonObject.Create;
try
AParams.Values['PageSize'] := '1000';
ANextToken := '';
repeat
AData := ExecuteHttp('GET', 'ListEmailIdentities', Host, '/v2/email/identities', nil, AParams, '').ContentAsString;
AJson.FromJSON(AData);
for AObj in AJson.A['EmailIdentities'] do
begin
if AObj.B['SendingEnabled'] then
begin
AIdentity.Identity := AObj.S['IdentityName'];
AIdentity.&Type := AObj.S['IdentityType'];
AIdentity.Verified := AObj.B['SendingEnabled'];
Result := Result + [AIdentity];
end;
// AResult.Add(AIdentity.S['IdentityName']);
end;
if not AJson.IsNull('NextToken') then
ANextToken := AJson.S['NextToken'];
until ANextToken = '';
finally
AParams.Free;
AJson.Free;
end;
end;
function TksAwsSES.SendEmail(AMessage: IksAwsSesMessage): integer;
var
AJson: TJsonObject;
AAddr: string;
begin
AJson := TJsonObject.Create;
try
AJson.O['Content'].O['Simple'].O['Body'].O['Html'].S['Charset'] := 'UTF-8';
AJson.O['Content'].O['Simple'].O['Body'].O['Html'].S['Data'] := AMessage.Body;
AJson.O['Content'].O['Simple'].O['Subject'].S['Charset'] := 'UTF-8';
AJson.O['Content'].O['Simple'].O['Subject'].S['Data'] := AMessage.Subject;
for AAddr in AMessage.Recipients do
AJson.O['Destination'].A['ToAddresses'].Add(AAddr);
for AAddr in AMessage.CC do
AJson.O['Destination'].A['CcAddresses'].Add(AAddr);
for AAddr in AMessage.Bcc do
AJson.O['Destination'].A['BccAddresses'].Add(AAddr);
AJson.S['FromEmailAddress'] := AMessage.Sender;
for AAddr in AMessage.ReplyTo do
AJson.A['ReplyToAddresses'].Add(AAddr);
Result := ExecuteHttp('POST', 'SendEmail', Host, '/v2/email/outbound-emails', nil, nil, AJson.ToString).StatusCode;
finally
AJson.Free;
end;
end;
procedure TksAwsSES.SendEmailSmtp(AMessage: TIdMessage);
var
ASmtp: TIdSMTP;
ASSl: TIdSSLIOHandlerSocketOpenSSL;
begin
{ASmtp := TIdSMTP.Create(nil);
ASsl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
//AMessage.Sender.
ASmtp.Host := 'email-smtp.'+RegionStr+'.amazonaws.com';
ASmtp.Port := C_AMAZON_PORT;
ASmtp.Username := C_AMAZON_USER;
ASmtp.Password := C_AMAZON_PASS;
ASmtp.IOHandler := ASsl;
ASmtp.UseTLS := utUseExplicitTLS;
ASsl.SSLOptions.Method := TIdSSLVersion.sslvTLSv1_2;
ASsl.SSLOptions.SSLVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];
ASmtp.Connect;
try
ASmtp.Send(AMessage);
finally
ASmtp.Disconnect;
end;
finally
ASmtp.Free;
ASsl.Free;
end; }
end;
procedure TksAwsSES.VerifyEmailIdentity(AEmailAddress: string);
var
AResponse: string;
AParams: TStrings;
begin
AParams := TStringList.Create;
try
AParams.Values['EmailAddress'] := AEmailAddress;
AResponse := ExecuteHttp('POST', 'VerifyEmailIdentity', Host, '', nil, AParams, '').ContentAsString;
finally
AParams.Free;
end;
end;
end.