#!/usr/bin/perl ################################################## # limitmail -- Mike Schilli, 2001 (m@perlmeister.com) ################################################## use GDBM_File; use Getopt::Long; use Mail::Mailer; use Pod::Usage; my $VERSION = "1.0"; our $CVSVERSION = '$Revision: 1.4 $'; # File where we remember when we sent what my $LOG_BOOK_FILE = "/tmp/limitmail.dat"; # Minimum timeframe in secs between mails my $NO_MORE_THAN_EVERY = 3600 * 24; my %log_book = (); tie %log_book, "GDBM_File", $LOG_BOOK_FILE, &GDBM_WRCREAT, 0640 or die "Can't tie to $LOG_BOOK_FILE"; my (@to, $from, @cc, @bcc, $subject); GetOptions ("to=s" => \@to, "from=s" => \$from, "subject=s" => \$subject, ); usage("Mandatory parameter missing") if !@to or !defined $from or !defined $subject; @to = grep { ! exists $log_book{$_} or time() - $log_book{$_} > $NO_MORE_THAN_EVERY } @to; exit 0 unless @to; print "Sending mail to @to\n"; @log_book{@to} = time(); my $m = Mail::Mailer->new('sendmail'); $m->open({To => shift(@to), Cc => join(',', @to), Subject => $subject}); print $m join('', <>); untie \%log_book; ################################################## sub usage { ################################################## my ($msg) = @_; use File::Basename; my $base = basename($0); print "$base: $msg\n"; pod2usage(); exit (1); } __END__ =head1 NAME limitmail - Send out notification emails but avoid flooding =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS limitmail --to email --from email --subject subject [file] Options: -h get help --to specify receiver's email address --from specify sender's email address --subject specify the subject of the message The mail text can be either specified as a file name or piped in through STDIN. =head1 DESCRIPTION B is a tool for sending out notification messages via email. It works like a regular mailer, but keeps track who it sent a message to and when. On the next call of B, it will silently discard messages to users who already received messages in a specified timeframe. The parameter C<$NO_MORE_THAN_EVERY> in the top section of the script specifies this timeframe in seconds. Bottom line: You can call this script as often as you want, it'll send off messages to users no more often than once in a specified time frame. =head1 EXAMPLES This example sends the text in C off to the addressee specified: limitmail --to bgates@microsoft.com \ --from m@perlmeister.com \ --subject "Where do you want to go today?" \ bill.txt This example uses the pipe-in mechanism: cat bill.txt | limitmail --to bgates@microsoft.com \ --from m@perlmeister.com \ --subject "Where do you want to go today?" \ bill.txt =head1 LEGALESE Copyright 2002 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2001, Mike Schilli