views:

99

answers:

2

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  = '[email protected] [email protected] [email protected]';
my $owner   = '[email protected]';

...    

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 [email protected], 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 [email protected] 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?

+5  A: 

I'd set up a role address, such as [email protected] 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
+2  A: 

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.

drewk
Or you can just use one of the free ones.
Fozi
@Fozi: Yes, exactly.
drewk