#!/usr/bin/perl use strict; use warnings; use Mail::Send; sub get_user_list( ) { my $i; my @passwd; my @users; open(PASSWD, ' < /etc/passwd') or die "Can't open $?: $!"; $i = 0; while() { chomp; @passwd = split ':', $_; if($passwd[0] ne '+' && $passwd[0] ne '#') { if($passwd[2] > 1000 && $passwd[3] == 115) { $users[$i++] = "$passwd[0]\@cs.earlham.edu"; } } } close(PASSWD); return @users; } sub get_body( $ ) { my($body_file) = @_; my $body; undef $/; open(BODY, "< $body_file") or die "Can't open $?: $!"; $body = ; close(BODY); return $body; } sub send_mail ( $$$@ ) { my($from_address,$subject,$body,$to_addresses) = @_; my $fh; my $msg = new Mail::Send; $msg = new Mail::Send Subject=>"$subject",To=>'EC-CS Users'; $msg->to('EC-CS Users'); $msg->bcc("$to_addresses"); $msg->subject("$subject"); $fh = $msg->open; print $fh "$body"; $fh->close; } sub insert_commas ( @ ) { my(@users) = @_; my $comma_users; my $user; my $i; $i = 0; foreach $user ( @users ) { if($i < $#users) { $comma_users .= "$user,"; } else { $comma_users .= "$user"; } $i++; } return $comma_users; } my @users; my $comma_users; my $user; my $body; my $subject; my $body_file; my $from_address; if($ARGV[0]) { $body_file = $ARGV[0]; } else { die "ERROR: You have to specify a filename to mail.\n"; } if($ARGV[1]) { $from_address = $ARGV[1]; } else { $from_address = 'admin@cs.earlham.edu'; } if($ARGV[2]) { $subject = $ARGV[2]; } else { $subject = "[Admin-announce] Downtime"; } @users = get_user_list(); $body = get_body($body_file); $comma_users = insert_commas(@users); send_mail("$from_address","$subject","$body","$comma_users");