Sample code for 30+ languages & platforms
Lazarus Pascal

Amazon SES v2: Verify a sending domain with Easy DKIM

See more Amazon SES Examples

Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.

CreateEmailIdentity · GetEmailIdentity · Easy DKIM records

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.StringBuilder,
  Chilkat.Rest,
  Chilkat.AuthAws,
  Chilkat.JsonObject,
  Chilkat.Socket;

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

procedure RunDemo;
var
  success: Boolean;
  domainName: string;
  checkExisting: Boolean;
  region: string;
  host: TStringBuilder;
  signer: TAuthAws;
  connection: TSocket;
  rest: TRest;
  sessionToken: string;
  responseText: string;
  path: TStringBuilder;
  identity: TJsonObject;
  response: TJsonObject;
  count: Integer;
  zone: string;
  i: Integer;
  token: string;

begin
  success := False;

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

  domainName := 'example.com';
  checkExisting := False;
  //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
  region := 'us-west-2';
  host := TStringBuilder.Create;
  host.Append('email.');
  host.Append(region);
  host.Append('.amazonaws.com');

  signer := TAuthAws.Create;
  //  Supply the credentials from your application's credential provider.
  signer.AccessKey := 'AWS_ACCESS_KEY_ID';
  signer.SecretKey := 'AWS_SECRET_ACCESS_KEY';
  signer.Region := region;
  signer.ServiceName := 'ses';

  connection := TSocket.Create;
  connection.RequireSslCertVerify := True;
  success := connection.Connect(host.GetAsString(),443,True,10000);
  if (success <> True) then
    begin
      WriteLn(connection.LastErrorText);
      Exit;
    end;

  rest := TRest.Create;
  success := rest.UseConnection(connection,False);
  if (success <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;
  success := rest.SetAuthAws(signer);
  if (success <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;
  //  For temporary AWS credentials, supply the session token from the same provider.
  sessionToken := '';
  if (sessionToken <> '') then
    begin
      rest.AddQueryParam('X-Amz-Security-Token',sessionToken);
    end;
  responseText := '';
  if (checkExisting = True) then
    begin
      path := TStringBuilder.Create;
      path.Append('/v2/email/identities/');
      path.Append(domainName);
      responseText := rest.FullRequestNoBody('GET',path.GetAsString());
    end
  else
    begin
      identity := TJsonObject.Create;
      identity.UpdateString('EmailIdentity',domainName);
      rest.AddHeader('Content-Type','application/json');
      responseText := rest.FullRequestString('POST','/v2/email/identities',identity.Emit());
    end;
  if (rest.LastMethodSuccess <> True) then
    begin
      WriteLn(rest.LastErrorText);
      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('DKIM status: ' + response.StringOf('DkimAttributes.Status'));
  WriteLn('Verified for sending: ' + response.BoolOf('VerifiedForSendingStatus'));
  count := response.SizeOfArray('DkimAttributes.Tokens');
  if (count < 1) then
    begin
      WriteLn('No Easy DKIM tokens were returned. Check this identity''s DKIM configuration.');
      Exit;
    end;
  if (response.HasMember('DkimAttributes.SigningHostedZone') <> True) then
    begin
      WriteLn('Find the CNAME targets for this identity in the SES console.');
      Exit;
    end;
  zone := response.StringOf('DkimAttributes.SigningHostedZone');
  i := 0;
  while (i < count) do
    begin
      response.I := i;
      token := response.StringOf('DkimAttributes.Tokens[i]');
      WriteLn(token + '._domainkey.' + domainName + ' CNAME ' + token + '.' + zone);
      i := i + 1;
    end;



  host.Free;
  signer.Free;
  connection.Free;
  rest.Free;
      path.Free;
      identity.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.