Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # Script by Bjorn Nilsen in 2010, for Manux Solutions Limited.
- use strict;
- use DBI;
- use Mail::Sendmail;
- use Getopt::Mixed "nextOption";
- my $dbh = DBI->connect('DBI:mysql:postfix', 'postfix', 'passwordremoved'
- ) || die "Could not connect to database: $DBI::errstr";
- my $subject = '';
- my $sql_domains = '';
- my $message = '';
- my $verbose = 1;
- my $testrun = 1;
- my $sendtoall = 0;
- Getopt::Mixed::init('f=s s=s d=s m=s q all run h help>h');
- while (my ($option, $value, $pretty) = nextOption())
- {
- #print "$option $value $pretty\n";
- if ($option eq 'h')
- {
- usage();
- }
- if ($option eq 'f')
- {
- $from = $value;
- }
- if ($option eq 's')
- {
- $subject = $value;
- }
- elsif ($option eq 'm')
- {
- $message = $value;
- }
- elsif ($option eq 'q')
- {
- $verbose = 0;
- }
- elsif ($option eq 'run')
- {
- $testrun = 0;
- }
- elsif ($option eq 'all')
- {
- $sendtoall = 1;
- }
- elsif ($option eq 'd')
- {
- if ($sql_domains)
- {
- $sql_domains = "$sql_domains or domain=\'$value\'"
- }
- else
- {
- $sql_domains = "domain=\'$value\'";
- }
- }
- }
- Getopt::Mixed::cleanup();
- unless ($subject)
- {
- print "ERROR: A subject must be specified with the \"-s\" option\n\n";
- usage();
- }
- unless ($message)
- {
- print "ERROR: A file containing the message body must be specified with the \"-m\" option\n\n";
- usage();
- }
- open MESSAGE, $message or die "ERROR: can't open file \" $message\" : $!";
- my $body = join('', <MESSAGE>);
- close MESSAGE;
- #print $body if ($verbose);
- if ($sql_domains)
- {
- $sql_domains = "($sql_domains) and";
- }
- else
- {
- print "ERROR: Must specify \"--all\" to send to all domains\n" unless ($sendtoall);
- usage() unless ($sendtoall);
- }
- my $sql_query = "select username from mailbox where $sql_domains active=1";
- #print "SQL: $sql_query\n" if ($verbose);
- my $addresses = $dbh->selectall_arrayref($sql_query);
- foreach (@$addresses)
- {
- my ($address) = @$_;
- print "Sending message to $address\n" if ($verbose && !$testrun);
- print "Test run: Sending message to $address\n" if ($verbose && $testrun);
- my %mail = ( To => $address,
- From => $from,
- Subject => $subject,
- Message => $body
- );
- (sendmail(%mail) or die $Mail::Sendmail::error) unless ($testrun);
- print "OK. Log says:\n", $Mail::Sendmail::log if ($verbose && !$testrun);
- }
- sub usage
- {
- my $prog = "bulletin";
- #my $prog = $0;
- #$prog = $1 if ($prog =~ m{\/(.+)$}g);
- print "Usage: $prog [OPTION]...\n\n";
- print "Mandatory options:\n";
- print " -s <subject>\tSubject line: use singles quotes around subject line\n";
- print " -m <filename>\tFilename of file containing message body\n\n";
- print " and -d or --all must be specified:\n\n";
- print " -d <domain>\tDomain to send to (this option can be specified multiple times)\n";
- print " --all\t\tSend message to all users and domains\n";
- print "\nOther options:\n";
- print " -f <sender>\tEMail address of sender\n";
- print " --run\t\tActually send messages, by default it will not do anything\n";
- print " -q\t\tQuiet mode\n";
- print "\nExamples:\n";
- print " Test run to all domains:\n";
- print " $prog --all -s \'Test message\' -f postmaster\@domainremoved.com -m message.txt\n\n";
- print " Test run to test.com and example.com:\n";
- print " $prog -s \'Test message\' -d test.com -d example.com -m message.txt\n\n";
- print " Send to test.com and example.com:\n";
- print " $prog -s \'Test message\' -d test.com -d example.com -m message.txt --run\n";
- exit 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment