Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Get a Mail-Merge Replacement String by Pattern

See more Email Object Examples

Demonstrates the Chilkat Email.GetReplaceString2 method, which returns the replacement string for a previously-defined pattern/replacement pair, looked up by the pattern itself (a mail-merge feature). This example defines two pairs and looks up the replacement for one pattern by name.

Background: Where GetReplaceString fetches a replacement by its numeric position, GetReplaceString2 fetches it by the pattern — convenient when you already know the token (say FIRST_NAME) and just want its configured substitution without scanning the whole list.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Email;

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

procedure RunDemo;
var
  email: TEmail;
  repl: string;

begin
  //  Demonstrates the GetReplaceString2 method, which returns the replacement string for a
  //  previously-defined pattern/replacement pair, looked up by the pattern itself (a
  //  mail-merge feature).

  email := TEmail.Create;
  email.Subject := 'Hello FIRST_NAME';
  email.Body := 'Dear FIRST_NAME, welcome to CITY.';

  email.SetReplacePattern('FIRST_NAME','John');
  email.SetReplacePattern('CITY','Denver');

  //  Look up the replacement for a specific pattern.
  repl := email.GetReplaceString2('FIRST_NAME');
  WriteLn('FIRST_NAME -> ' + repl);


  email.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.