Sample code for 30+ languages & platforms
Lazarus Pascal

Amazon SES v2: Check an address on the account suppression list

See more Amazon SES Examples

Look up one address in the sending Region with ses:GetSuppressedDestination. The record distinguishes a bounce from a complaint. An absent record tells you only that this account suppression lookup found no entry. The example signs the exact encoded mailbox path and also works with temporary credentials.

GetSuppressedDestination

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.CkDateTime,
  Chilkat.Socket,
  Chilkat.Rest,
  Chilkat.JsonObject,
  Chilkat.StringBuilder,
  Chilkat.Crypt2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  address: string;
  region: string;
  accessKey: string;
  secretKey: string;
  sessionToken: string;
  host: TStringBuilder;
  mailbox: TStringBuilder;
  path: TStringBuilder;
  canonicalPath: TStringBuilder;
  replaced: Integer;
  clock: TCkDateTime;
  stamp: TStringBuilder;
  date: string;
  rest: TRest;
  headers: TStringBuilder;
  signedHeaders: string;
  crypto: TCrypt2;
  canonical: TStringBuilder;
  scope: TStringBuilder;
  toSign: TStringBuilder;
  seed: TStringBuilder;
  key: string;
  signature: string;
  authorization: TStringBuilder;
  connection: TSocket;
  responseText: string;
  response: TJsonObject;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  success := False;

  //  Look up the address exactly as stored, including any plus tag.
  address := 'avery+orders@example.com';
  region := 'us-west-2';
  accessKey := 'AWS_ACCESS_KEY_ID';
  secretKey := 'AWS_SECRET_ACCESS_KEY';
  sessionToken := '';
  host := TStringBuilder.Create;
  host.Append('email.');
  host.Append(region);
  host.Append('.amazonaws.com');
  mailbox := TStringBuilder.Create;
  mailbox.Append(address);
  path := TStringBuilder.Create;
  path.Append('/v2/email/suppression/addresses/');
  path.Append(mailbox.GetEncoded('url_rfc3986','utf-8'));

  //  SigV4 for SES encodes the wire path a second time in the canonical request.
  //  Only the canonical copy escapes the percent signs. Send the original path over HTTPS.
  canonicalPath := TStringBuilder.Create;
  canonicalPath.Append(path.GetAsString());
  replaced := canonicalPath.Replace('%','%25');
  clock := TCkDateTime.Create;
  clock.SetFromCurrentSystemTime();
  stamp := TStringBuilder.Create;
  stamp.Append(clock.GetAsTimestamp(False));
  replaced := stamp.Replace('-','');
  replaced := stamp.Replace(':','');
  date := stamp.GetRange(0,8,False);
  rest := TRest.Create;
  rest.AddHeader('Host',host.GetAsString());
  rest.AddHeader('x-amz-date',stamp.GetAsString());
  headers := TStringBuilder.Create;
  headers.Append('host:');
  headers.Append(host.GetAsString());
  headers.Append(#10 + 'x-amz-date:');
  headers.Append(stamp.GetAsString());
  headers.Append(#10);
  signedHeaders := 'host;x-amz-date';
  if (sessionToken <> '') then
    begin
      rest.AddHeader('x-amz-security-token',sessionToken);
      headers.Append('x-amz-security-token:');
      headers.Append(sessionToken);
      headers.Append(#10);
      signedHeaders := 'host;x-amz-date;x-amz-security-token';
    end;
  crypto := TCrypt2.Create;
  crypto.HashAlgorithm := 'sha256';
  crypto.MacAlgorithm := 'hmac';
  crypto.EncodingMode := 'hexlower';
  crypto.Charset := 'utf-8';
  canonical := TStringBuilder.Create;
  canonical.Append('GET' + #10);
  canonical.Append(canonicalPath.GetAsString());
  canonical.Append(#10 + #10);
  canonical.Append(headers.GetAsString());
  canonical.Append(#10);
  canonical.Append(signedHeaders);
  canonical.Append(#10);
  canonical.Append(crypto.HashStringENC(''));
  scope := TStringBuilder.Create;
  scope.Append(date);
  scope.Append('/');
  scope.Append(region);
  scope.Append('/ses/aws4_request');
  toSign := TStringBuilder.Create;
  toSign.Append('AWS4-HMAC-SHA256' + #10);
  toSign.Append(stamp.GetAsString());
  toSign.Append(#10);
  toSign.Append(scope.GetAsString());
  toSign.Append(#10);
  toSign.Append(crypto.HashStringENC(canonical.GetAsString()));

  //  Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
  seed := TStringBuilder.Create;
  seed.Append('AWS4');
  seed.Append(secretKey);
  crypto.SetMacKeyString(seed.GetAsString());
  key := crypto.MacStringENC(date);
  crypto.SetMacKeyEncoded(key,'hex');
  key := crypto.MacStringENC(region);
  crypto.SetMacKeyEncoded(key,'hex');
  key := crypto.MacStringENC('ses');
  crypto.SetMacKeyEncoded(key,'hex');
  key := crypto.MacStringENC('aws4_request');
  crypto.SetMacKeyEncoded(key,'hex');
  signature := crypto.MacStringENC(toSign.GetAsString());
  authorization := TStringBuilder.Create;
  authorization.Append('AWS4-HMAC-SHA256 Credential=');
  authorization.Append(accessKey);
  authorization.Append('/');
  authorization.Append(scope.GetAsString());
  authorization.Append(', SignedHeaders=');
  authorization.Append(signedHeaders);
  authorization.Append(', Signature=');
  authorization.Append(signature);
  rest.AddHeader('Authorization',authorization.GetAsString());
  connection := TSocket.Create;
  connection.RequireSslCertVerify := True;
  success := connection.Connect(host.GetAsString(),443,True,10000);
  if (success <> True) then
    begin
      WriteLn(connection.LastErrorText);
      Exit;
    end;
  success := rest.UseConnection(connection,False);
  if (success <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  responseText := rest.FullRequestNoBody('GET',path.GetAsString());
  if (rest.LastMethodSuccess <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;
  if (rest.ResponseStatusCode = 404) then
    begin
      WriteLn('No account suppression entry for this address in this Region.');
      Exit;
    end;
  if (rest.ResponseStatusCode <> 200) then
    begin
      WriteLn('SES returned HTTP ' + rest.ResponseStatusCode + ' ' + responseText);
      Exit;
    end;
  response := TJsonObject.Create;
  success := response.Load(responseText);
  if (success <> True) then
    begin
      WriteLn(response.LastErrorText);
      Exit;
    end;
  WriteLn(response.StringOf('SuppressedDestination.EmailAddress'));
  WriteLn('Reason: ' + response.StringOf('SuppressedDestination.Reason'));
  WriteLn('Last updated (Unix seconds): ' + response.StringOf('SuppressedDestination.LastUpdateTime'));


  host.Free;
  mailbox.Free;
  path.Free;
  canonicalPath.Free;
  clock.Free;
  stamp.Free;
  rest.Free;
  headers.Free;
  crypto.Free;
  canonical.Free;
  scope.Free;
  toSign.Free;
  seed.Free;
  authorization.Free;
  connection.Free;
  response.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.