Compiling mailing List at Runtime - MSDOS

Have you ever heard some one saying "you can't do much in .BAT scripts". People used to believe this back in 80's; and this belief may still persist. But actually the scripting has changed so transparently that most people did not notice. These days it is possible to do virtually anything. Secondly as my belief, the languages are never complete and so are the scripts. But at the same time logic never ends. Limitations in languages have never limited the logics and new ways were always discovered.

This article explains how we can set up mailing list at run time. This means that you have contact list for say few employees who will be watching some daily job running for errors and/or warnings. The job script will incorporate this batch script to send emails. Variable Notify will contain a number that will represent the groups that will receive email and each group separately will be able to filter their email based on severity.

This mailer component can handle up to six groups to configure their emails. These six can be groups, individuals, or one person having several six email-ids grouped in six. The component have nothing to do with the number of email-ids, it only performs as six entries. I personally used it to configure email-list for 3 persons having two email-ids each, first as regular (office) email and second as an wireless(mobile) email-id.

To start with I must explain the main idea behind it. We have six flat-text files. One file is one unit or group of email-ids. These files will be added to current mailing list file that will be read by mailer program like blat.exe (-tf maillist.txt). So we will be having six permanent files which will represent six groups.

The variable named NOTIFY will be used in this example to test the current mailing list additions. This variable will store the binary value based on groups.

The permanent files will be called MailList1, MailList2 . . . . MailList6. The mailer program will be blat.exe and it will use MailList.txt to read the email-ids that are going to be notified in case of error.

There will be one more file called Notify that will contain the setting value for different groups. If it contains value 4095 ( 11 11 11 11 11 11 ) then all the six groups will be mailed all kind of broadcasts irrespective of severity. Value 0 ( 00 00 00 00 00 00 ) will indicate to send no email. Similarly value 3 ( 00 00 00 00 00 11) will add first group in the list and send all messages ti the ids in MailList1 file.

Two bits (11) are used for one group. Because 2 bits can have possibility as 0 (00), 1 (01), 2 (10) and 3 (11) . Value 0 will exclude the group, value 1 will direct errors only, value 2 will direct errors and warnings and value 3 will direct all messages to this group. Now groups are identified by position of bits. Position 0,1 for first group, 2,3 for second group . . . . and lastly position 10,11 for sixth group.

Bitwise operations will be performed with SET /A "Var=Val" syntax in Windows NT environment. The script file can be used as .CMD or .BAT script.

And before we discuss the subroutine line by line, it is worth reminding that this routine uses Notify environmental variable to compile mailing list. The value can be hard coded or can be saved in Notify flat file which will be read by the script (line 18) and the mailing list can be compiled accordingly.

Description:: Lines 1 to 6 are documentation lines (REM). Line 8 sets hard coded groups definition value. Lines 9 to 17 is some more documentation. Line 18 reads file Notify into environmental variable Notify. This file helps customization of groups’ definition.

1. ::==================**GURUNANAK**========================+ 2. :: MaiList :: 01 emp 23 team leader 45 manager | 3. :: Notify :: 0 none 1 errors 2 errors+warnings 3 all | 4. ::11 11 11 11 11 11 | 5. ::=======================================================+ 6. 7. @echo off 8. set /a "Notify=4095" 9. :: Bit Map for MailList & Notifications 10. :: 11 11 11 11 11 11 ----->4095 -- Full List Full Notifications 11. :: 01 01 01 01 01 01 -----> 1365 - Only errors for ALL 12. :: 3 12 48 192 768 3072---> and with values 13. :: and shift to right 14. :: 0 2 4 6 8 10 ----------> SHR with these to get actual 15. :: Now 1 bits will range from 0-3 for notification 16. ::================Compilling %MaiList%================== 17. :: 18. for /F %%x in (Notify) do set /a "Notify=%%x" 19. copy nul MailList.txt >nul 20. call :CompileMailList 1 21. type MailList.txt 22. goto :eof 23. :CompileMailList 24. set /a "bit=%Notify%>>0" 25. set /a "bit &=%1" 26. if exist MailList1 if not [%bit%]==[0] type MailList1>>MailList.txt 27. set /a "bit=%Notify% >>2" 28. set /a "bit &=%1" 29. if exist MailList2 if not [%bit%]==[0] type MailList2>>MailList.txt 30. set /a "bit=%Notify% >>4" 31. set /a "bit &=%1" 32. if exist MailList3 if not [%bit%]==[0] type MailList3>>MailList.txt 33. set /a "bit=%Notify% >>6" 34. set /a "bit &=%1" 35. if exist MailList4 if not [%bit%]==[0] type MailList4>>MailList.txt 36. set /a "bit=%Notify% >>8" 37. set /a "bit &=%1" 38. if exist MailList5 if not [%bit%]==[0] type MailList5>>MailList.txt 39. set /a "bit=%Notify% >>10" 40. set /a "bit &=%1" 41. if exist MailList6 if not [%bit%]==[0] type MailList6>>MailList.txt 42. goto :eof 43. ::[email protected] 44. ::[email protected] 45. ::[email protected] 46. ::[email protected] 47. ::[email protected] 48. ::[email protected]

Line 19 Nullifies MaiList.txt file.

Line 20 calls subroutine :CompileMailList with argument 1. Argument will be used to compare for notification level 1, 2 or 3. If you call it with argument 0, believe me, no one will be emailed. This parameter (argument) will be used as %1 to compare bits for notification level.

Line 21 in above code types the compiled list . Actually this line should be used to call blat.exe –tf MailList.txt . . . . . . .

Line 22 declares the end of this component (script)

Line 23 declares the real useful compile mailing list subroutine.

Lines 24,25,26 does art work of decoding bit values for a particular group. You see 6 such sets that process bit settings of the group it belongs to.

First line : This shifts the value right by such a number that two bits belonging to group are shifted to the right most place.

Second line : Then these bits are ANDed by the parameter %1. This will filter rightmost two bits and the result will be actual bit settings for this group.

Third Line : compare the result with zero. If not zero then corresponding mail list file is copied into final list MailList.txt

This process is repeated six times. And at the end of this subroutine ( line 42 ) the MailList.txt is ready for use with blat.exe to broadcast email message to the list.

Lines 43-48 are sample email ids. I used one email per file. You may try using more than one email id per file as required.

Set Up:: Use following procedure to set up mailing list.

  1. Set up 6 files with names as MailList1... MaiList6. You do not have to set all files. You can set 2, 4 or any number below 7.
  2. Add email ids in these files. One id per line followed by CR+LF.
  3. Set Notify file with value ( preferably use decimal number) Use Windows advanced calculator to compile bits as below:
    1. Type in the notification value. ( 1 errors only 2 errors & warnings 3 all)
    2. Shift this value Left (Lsh) by (GroupNo -1)*2.
    3. Note this value somewhere or store in calculator memory.
    4. Now feed current Notify value in calculator.
    5. XOR it with 0.
    6. XOR the result with stored value.
    7. Your browser may not support display of this image.This value is the new value for Notify.

If there is no value of Notify when you start encoding, assume it to be 0.

Repeat this encoding for every group you add.

You Are Done:: Simply what you have to do is to call blat.exe ( use any mailer utility ) to send messages using MailList.txt file.

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I recommend thoroughy testing on a production-like test system first before moving to production.