#!/bin/perl

die "usage: rndl2mail message rand-mail-list-file" if  ($#ARGV != 1);

$msg = $ARGV[0];
$list = $ARGV[1];

die "can't open $msg" unless open(MSG, $msg); close(MSG);
die "can't open $list" unless open(LIST, $list);

print "message file is $msg\n";
print "list of e-mail addresses with random number is $list\n";

print "Enter subject line: ";
$subj = <STDIN>;
chop($subj);

print "Enter from address: ";
$from = <STDIN>;
chop($from);

print "Enter reply-to address: ";
$repl = <STDIN>;
chop($repl);

print "\n";

while (<LIST>) {
  chop;
  ($addr, $numb) = split(/ /);
  print "doing $addr ...";
  die "can't open $msg" unless open(MSG, $msg); 
  die "can't open sendmail" unless open(SENDM,"|/usr/lib/sendmail $addr");
  print SENDM "From: $from\n";
  print SENDM "Subject: $subj [ballot #$numb]\n";
  print SENDM "Reply-To: $repl\n";
  print SENDM "\n";

  while (<MSG>) {
    print SENDM "$_";
  }
  close(MSG);
  close(SENDM);
  print " done!\n";
}
