Guest User

bulletin.pl

a guest
Jun 3rd, 2010
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.53 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. # Script by Bjorn Nilsen in 2010, for Manux Solutions Limited.
  3.  
  4. use strict;
  5. use DBI;
  6. use Mail::Sendmail;
  7. use Getopt::Mixed "nextOption";
  8.  
  9. my $dbh = DBI->connect('DBI:mysql:postfix', 'postfix', 'passwordremoved'
  10.            ) || die "Could not connect to database: $DBI::errstr";
  11.  
  12. my $from='[email protected]';
  13. my $subject = '';
  14. my $sql_domains = '';
  15. my $message = '';
  16. my $verbose = 1;
  17. my $testrun = 1;
  18. my $sendtoall = 0;
  19.  
  20. Getopt::Mixed::init('f=s s=s d=s m=s q all run h help>h');
  21. while (my ($option, $value, $pretty) = nextOption())
  22. {
  23.     #print "$option $value $pretty\n";
  24.     if ($option eq 'h')
  25.     {
  26.         usage();
  27.     }
  28.     if ($option eq 'f')
  29.     {
  30.         $from = $value;
  31.     }
  32.     if ($option eq 's')
  33.     {
  34.         $subject = $value;
  35.     }
  36.     elsif ($option eq 'm')
  37.     {
  38.         $message = $value;
  39.     }
  40.     elsif ($option eq 'q')
  41.     {
  42.         $verbose = 0;
  43.     }
  44.     elsif ($option eq 'run')
  45.     {
  46.         $testrun = 0;
  47.     }
  48.     elsif ($option eq 'all')
  49.     {
  50.         $sendtoall = 1;
  51.     }
  52.     elsif ($option eq 'd')
  53.     {
  54.         if ($sql_domains)
  55.         {
  56.             $sql_domains = "$sql_domains or domain=\'$value\'"
  57.         }
  58.         else
  59.         {
  60.             $sql_domains = "domain=\'$value\'";
  61.         }
  62.     }
  63. }
  64. Getopt::Mixed::cleanup();
  65.  
  66. unless ($subject)
  67. {
  68.     print "ERROR: A subject must be specified with the \"-s\" option\n\n";
  69.     usage();
  70. }
  71. unless ($message)
  72. {
  73.     print "ERROR: A file containing the message body must be specified with the \"-m\" option\n\n";
  74.     usage();
  75. }
  76.  
  77. open MESSAGE, $message or die "ERROR: can't open file \" $message\" : $!";
  78. my $body = join('', <MESSAGE>);
  79. close MESSAGE;
  80. #print $body if ($verbose);
  81.  
  82. if ($sql_domains)
  83. {
  84.     $sql_domains = "($sql_domains) and";
  85. }
  86. else
  87. {
  88.     print "ERROR: Must specify \"--all\" to send to all domains\n" unless ($sendtoall);
  89.     usage() unless ($sendtoall);
  90. }
  91.  
  92. my $sql_query = "select username from mailbox where $sql_domains active=1";
  93. #print "SQL: $sql_query\n" if ($verbose);
  94.  
  95. my $addresses = $dbh->selectall_arrayref($sql_query);
  96.  
  97. foreach (@$addresses)
  98. {
  99.     my ($address) = @$_;
  100.     print "Sending message to $address\n" if ($verbose && !$testrun);
  101.     print "Test run: Sending message to $address\n" if ($verbose && $testrun);
  102.  
  103.     my %mail = ( To => $address,
  104.              From    => $from,
  105.              Subject => $subject,
  106.              Message => $body
  107.            );
  108.  
  109.     (sendmail(%mail) or die $Mail::Sendmail::error) unless ($testrun);
  110.     print "OK. Log says:\n", $Mail::Sendmail::log if ($verbose && !$testrun);
  111. }
  112.  
  113. sub usage
  114. {
  115.     my $prog = "bulletin";
  116.     #my $prog = $0;
  117.     #$prog = $1 if ($prog =~ m{\/(.+)$}g);
  118.  
  119.     print "Usage: $prog [OPTION]...\n\n";
  120.     print "Mandatory options:\n";
  121.     print "  -s <subject>\tSubject line: use singles quotes around subject line\n";
  122.     print "  -m <filename>\tFilename of file containing message body\n\n";
  123.     print "  and -d or --all must be specified:\n\n";
  124.     print "  -d <domain>\tDomain to send to (this option can be specified multiple times)\n";
  125.     print "  --all\t\tSend message to all users and domains\n";
  126.     print "\nOther options:\n";
  127.     print "  -f <sender>\tEMail address of sender\n";
  128.     print "  --run\t\tActually send messages, by default it will not do anything\n";
  129.     print "  -q\t\tQuiet mode\n";
  130.     print "\nExamples:\n";
  131.     print "  Test run to all domains:\n";
  132.     print "    $prog --all -s \'Test message\' -f postmaster\@domainremoved.com -m message.txt\n\n";
  133.     print "  Test run to test.com and example.com:\n";
  134.     print "    $prog -s \'Test message\' -d test.com -d example.com -m message.txt\n\n";
  135.     print "  Send to test.com and example.com:\n";
  136.     print "    $prog -s \'Test message\' -d test.com -d example.com -m message.txt --run\n";
  137.     exit 1;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment