SendMailToAddressGroup (include)

From 40tude Dialog Wiki

Contents

Send Mail To Address Group (Include)

Using the SendMailToAddressGroup-script enables you to organize your email contacts similar to mailing lists. After you define a group of addresses, sending an email to the group's handle will automatically expand it to the email addresses in the group.

If you found bugs or have recommendations, feel free to contact the author Thomas Barghahn (Th.Barghahn@t-online.de).


Introduction

Address groups are defined in an ini file:

; This is a comment. Lines containing comments have to start
; with a semicolon (;).

; "AddressGroups.ini"
; Datum: 21.03.2006
; Thomas Barghahn

[mail@40tDialog.group]
example <user1@example.xx>
example <user2@example.yy>
example <user3@example.zz>

[mail@Familie.group]
; Tante Klara <klara@fqdn.part>
Onkel Ernie <ernie@fqdn.part>
Freddy <freddy@fqdn.part>
Frida <frida@fqdn.part>

The text inside the square brackets start a group and define a group handle, the following lines contain the addresses in the group. You need to create a plain text file similar to the above with a text editor (like notepad) and save it.


Install & setup the script

Please note that this script is an include-file! You have to paste the script at the bottom of this page into a new custom script (for example "Include_SendMailToAddressGroup") and safe it. Take the time now to configure the script, by changing the values of "MyIniFolder" and "MyIniFile" to the folder and the name of the file with your address groups. Safe it again. You do not need to compile this custom script!

After that you have to add the following lines to your OnBeforeSending-Script:

 {$I Include_SendMailToAddressGroup.ds}    

and

  Change_To_Addresses ( Message, IsEmail);

This may look like this:

program OnBeforeSendingMessage;
 {$I Include_SendMailToAddressGroup.ds}   

function OnBeforeSendingMessage(var Message: TStringlist; Servername: string; IsEmail: boolean):boolean;
begin
  result:=true;

  //possibly some other calls to scripts

    Change_To_Addresses ( Message, IsEmail);

end;

begin
end.

Compile this OnBeforeSending-Script now to get your include-file to work. Please note that whenever you change the "Include_SendMailToAddressGroup" custom script, you have to recompile the OnBeforeSending-Script to apply the changes.


The Include-File

Please be sure that this is the only content in the include-file!

procedure Init_Change_To_Addresses ( var ChangeInEmails    : Boolean;
                                     var ChangeInNews      : Boolean;
                                     var X_OldTo           : Boolean;
                                     var MyIniFolder       : String;
                                     var MyIniFile         : String
);          

begin 

// Date: 2006/03/22

//  ----------------------------------------------------
//  Configuration settings
//  ----------------------------------------------------

    // change To-Addresses in emails and/or postings
    // set 'true' or 'false'
    ChangeInEmails := true;
    ChangeInNews := false;
    
    // create Header "X-Old-To:"?    
    // set 'true' or 'false'
    X_OldTo := true;
    
    MyIniFolder := 'C:\Programme\40tude_Dialog\';
    MyIniFile := 'AddressGroups.ini';

//  ----------------------------------------------------
//  End of configuration settings
//  ---------------------------------------------------- 

end;

   // --------------------------------------------------------------- //
   // ----  No user maintainable parts below this line -------------- //
   // --------------------------------------------------------------- //
    

procedure Change_To_Addresses (Message:TStringlist;IsEmail:boolean);

var i, j, k           : integer;
    ChangeInEmails    : Boolean;
    ChangeInNews      : Boolean;
    X_OldTo           : Boolean;
    MyIniFolder       : String;
    MyIniFile         : String;
    AddressList       : TStringList;
    MaxInt            : Integer;
    GroupAddress      : String;
    TempAddress       : String;
    
begin
   Init_Change_To_Addresses ( ChangeInEmails, ChangeInNews, X_OldTo, MyIniFolder, MyIniFile );
   if ((IsEmail=true)  and (ChangeInEmails=true)) or
      ((IsEmail=false) and (ChangeInNews=true)) then begin
      try
      AddressList := TStringList.Create;
      AddressList.LoadFromFile(MyIniFolder + MyIniFile);
      AddressList.Add (#13#10);
      GroupAddress := '';
      i := 0;
      MaxInt := 2147483647;
      while (Message.Strings[i]<>'') do begin
         if pos('To: ',Message.Strings[i]) = 1 then begin
            TempAddress := copy (Message.Strings[i], 5, MaxInt);
            For j := 0 to AddressList.Count - 1 do begin
               If LowerCase (AddressList.Strings[j]) = LowerCase ('[' + TempAddress + ']') then begin
                  k := j + 1;
                  repeat
                     If copy (AddressList.Strings[k],1 ,1) <> ';' then begin
                        If GroupAddress = '' then
                           GroupAddress := AddressList.Strings[k]
                        else
                           GroupAddress := GroupAddress + ', ' + AddressList.Strings[k];
                     end; // if
                     k := k + 1;
                  until (k = AddressList.Count - 1) or (AddressList.Strings[k] = '') or
                        (copy (AddressList.Strings[k],1 ,1) = '['); 
               end; // if    
            end; // for
            If GroupAddress <> '' then begin
               Message.Strings[i] := 'To: ' + GroupAddress;
               If X_OldTo = true then
                  Message.insert (i + 1, 'X-Old-To: ' + TempAddress);
            end; // if   
         end; //if
         i := i + 1;
      end; {end of while}
      finally
         AddressList.Free;
      end; //try .. finally   
   end; //if
end;

Maik Prinz