#!/usr/bin/perl
#
# mailall: Sends STDIN as a mail message to all users.
#          The Subject line must be the first line of the message and
#          must be separated from the body by at least one blank line.
#

# Set some default values.
$script_version = '1.0';
$first_uid = 500;
$passwd = '< /etc/passwd';
$mailprog = '| /usr/sbin/sendmail -oi -t';

# Open the password file for input.
open(PASSWD,$passwd);

# Open a pipe to sendmail for output.
open(MAIL,$mailprog);

# Send the headers.
print MAIL "From: \"System Administrator\" <root>\n";
print MAIL "To: \"All Users\" <root>\n";
print MAIL "X-Mailer: mailall $script_version\n";

# Put all the users in the Bcc header.
while($userline = <PASSWD>) {
  @userdata = split(/:/,$userline);
  $recipient_name = @userdata[0];
  $recipient_uid  = @userdata[2];
  if ($recipient_uid >= $first_uid) {
    print MAIL "Bcc: $recipient_name\n";
  }
}
close(PASSWD);

# Now send the message (including the Subject) as read from STDIN.
while ($line = <STDIN>) {
  print MAIL "$line";
}
close(MAIL);
exit;

