1

My Perl script to monitor a directory on Unix, stores a list of users to whom a notification mail is sent when the directory it is monitoring is updated.

This is the construct used

dirmon.pl

my $subject = '...';    
my $msg     = '...';
my $sendto  = 'a@server.com b@server.com c@server.com';
my $owner   = 'me@server.com';

...    

open my $fh, "|-", "mail", "-s", $subject, $owner, "-c", $sendto
    or die "$0: could not start mail: $!";

print $fh $msg or warn "$0: print: $!";
close $fh;

So, right now, for every new user that I want to send the notification mails to, I need to go to the code and add them to $sendto. It is fine for me, but I want to distribute the utility to users later and do not want them adding addresses to the list manually, at least not editing the Perl code directly.

There are two alternatives I can think of

  1. Maintaining an external file that has the list of recipients. I can add a flag so that when the user says dirmon.pl -a d@server.com, the email address is appended to the file and the next time a mail is sent, the mail goes to this recipient too (dirmon.pl -r d@server.com to remove the user from the list). The only problem with this is that I need to have one more external file with the script, which I am trying to minimize.

  2. I can have self modifying Perl code on the lines of "Can a perl script modify itself?". I am not sure if this is a good idea.

Is first way the best way? Is there any better method to maintain the list of recipients?

Community
  • 1
  • 1
Lazer
  • 90,700
  • 113
  • 281
  • 364
  • 4
    You could create a mailing list and have the script post the changes to that. Your users could subscribe/unsubscribe to that list as they like. – Fozi Sep 13 '10 at 14:31
  • a mailing list as in a text file? where would the list reside? – Lazer Sep 13 '10 at 14:33
  • 1
    No, I meant a mailing list as a service. http://en.wikipedia.org/wiki/Electronic_mailing_list – Fozi Sep 13 '10 at 14:37
  • @negvoter: Whats "unclear" or "not useful" about this question? – Lazer Sep 13 '10 at 14:43
  • @Fozi: I know about the electronic mailing list concept. That is too complex and not worth the effort, I think. (Thats why I assumed you were talking about a simple mailing list) – Lazer Sep 13 '10 at 14:45
  • 1
    A self modifying script is almost never the answer, unless the question is "How do I cause a lot of problems for no good or valid reason?" – brian d foy Sep 14 '10 at 04:47

2 Answers2

5

I'd set up a role address, such as noc@example.com then manage the people to send it through your mail delivery program. That way, as people come and go you aren't changing code. This is especially important in watchdog scripts where you'll adjust the recipients based on who is on vacation, who just joined the team, and so on. You want to push all that complexity out of the code.

If you don't want to do it the easy way, put the addresses in a config file. You want your program to respond to changes in the real world without changing code. Any solution that requires you to change the source is risky. I talk about this quite a bit in Mastering Perl.

You'll also have a much easier time if you use one of the Email::Sender modules to send the mail instead of jumping through hoops to call a command-line program. Beyond that, you might be interested in frameworks such as AnyEvent and Watchdog that are designed to handle the other bits for you.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
2

There is a 90% plus probability that your mail server will do this for you. Set up a mail address similar to "build@[yourco.com]" that your script send reports to. Interested parties add themselves to this list through whatever mechanism is used by your server.

Exchange, Postfix, Dovecot, Sendmail, apple mail, Zimbra all support distribution lists. Those servers are probably more than 90% share. It is literally seconds to set up a distribution list.

Another solution: use some of the public distribution lists that allow people to add / delete themselves and send to that. I think Google has one.

Alternatively, you can take an existing Perl or Apache "guest book" script and set that up on an internal server. There are hundreds of theses floating around. Then people add themselves to the "guestbook" web page of your script and they receive email reports. Potentially, you can use the same to host the most recent report. This can be complicated by firewall issues, but you get the drift... This is far more trivial than it sounds; less than a couple hour's work.

All three ideas here are a lot less work that writing your own.

Cheers.

dawg
  • 98,345
  • 23
  • 131
  • 206