diff options
-rwxr-xr-x[-rw-r--r--] | mailer.pl | 86 |
1 files changed, 47 insertions, 39 deletions
diff --git a/mailer.pl b/mailer.pl index c3b53da..cab1509 100644..100755 --- a/mailer.pl +++ b/mailer.pl @@ -12,6 +12,8 @@ use Getopt::Long; use List::Util; use MCE; use MCE::Loop; +use MCE::Flow; +use MCE::Shared; use Path::Tiny; use Pod::Usage; use Time::HiRes qw(gettimeofday tv_interval); @@ -35,6 +37,25 @@ my $chunksize = 1; # end config # #### +=head1 NAME + +mailer.pl - send lots of mails + +=head1 SYNOPSIS + +mailer.pl options ... + + Options: + --help, -h short help message + --to <email or file> recipient(s) + --from <email> from + --subject <string> subject + --content-html <file> body of the mail (html part) (optional) + --content-txt <file> body of the mail (text part) + --debug output extra debug information + +=cut + my %opts = (); Getopt::Long::Configure ("bundling"); @@ -54,19 +75,6 @@ my $from = $opts{from}; my $content_html; my $content_txt; -my $mce_input; - -if ($opts{to} =~ m/.+\@.+\..+/) { - $mce_input = [$opts{to}]; -} else { - if (-r $opts{to}) { - open my $fh, "<", $opts{to}; - $mce_input = $fh; - } else { - print STDERR "Error: can't read recipient file: $!\n"; - } -} - die "Error: subject not set\n" unless ($opts{subject}); die "Error: from not set\n" unless ($opts{from}); die "Error: content-txt not set\n" unless ($opts{"content-txt"}); @@ -94,40 +102,40 @@ $msg->transport($transport); print "done\n" if $opts{debug}; print "creating workers\n" if $opts{debug}; -MCE::Loop::init {max_workers => $thread_count, chunk_size => $chunksize}; print "Sending\n"; my $send_time = [gettimeofday]; -mce_loop { - my ($mce, $chunk_ref, $chunk_id) = @_; - for my $to (@{$chunk_ref}) { +my $queue = MCE::Shared->queue(await => 1, fast => 1); + +mce_flow { + task_name => ['producer', 'sender'], + max_workers => [1, $thread_count]}, +sub { + if ($opts{to} =~ m/.+\@.+\..+/) { + $queue->enqueue($opts{to}); + } else { + if (-r $opts{to}) { + open my $fh, "<", $opts{to}; + while (<$fh>) { + $queue->enqueue($_); + $queue->await($thread_count); + } + close $fh; + } else { + print STDERR "Error: can't read recipient file: $!\n"; + } + } + $queue->end(); +}, +sub { + while (defined (my $to = $queue->dequeue())) { $msg->to($to); $msg->send(); - print "sent mail in worker ".MCE->wid." from chunk ".$chunk_id."\n" if $opts{debug}; + print "sent mail in worker ".MCE->wid."\n" if $opts{debug}; } $transport->disconnect; -} $mce_input; +}; print "done after ".tv_interval($send_time)." seconds\n"; -__END__ - -=head1 NAME - -mailer.pl - send lots of mails - -=head1 SYNOPSIS - -mailer.pl options ... - - Options: - --help, -h short help message - --to <email or file> recipient(s) - --from <email> from - --subject <string> subject - --content-html <file> body of the mail (html part) (optional) - --content-txt <file> body of the mail (text part) - --debug output extra debug information - -=cut |