commonlybon has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I did a perl script that uses 5 smtp servers and sends newsletters to +a recipient All this is done using Parallel::ForkManager to use concurrent connect +ions of 5 smtp servers and until the smtp list is all done it sends f +ine...But now I want to make a perl script that takes the recipient a +ddresses from file like this : #!/usr/bin/perl $fileaddress = $ARGV[0]; open(INFO, $fileaddress); @addresses = <INFO>; close(INFO); $" = ","; chomp @addresses; ... smtp->recipient(@recipients); ... And sends in parallel with 2 smtp servers to 10 recipients and so on u +ntil the $fileaddress is all done...I mean it takes the first 10 emai +l addresses from addresses.txt and sends with the first smtp server b +ut in the same time it sends with the second smtp server to the next +10 email addresses...After this is done it takes the next 2 smtp serv +ers and sends another 10 emails each to 20 different email addresses. +..I want to do this to don't force too much my smtp servers...Please +help

Replies are listed 'Best First'.
Re: Net::SMTP newsletter
by Corion (Patriarch) on Dec 22, 2008 at 12:57 UTC

    Just use threads and Thread::Queue to distribute the batches:

    use strict; use threads; use Thread::Queue; use Net::SMTP; my @servers = qw(192.168.0.1 192.168.0.2 192.168.0.3); my @addresses = read_addresses_from_file(); my $queue = Thread::Queue->new(); # Now put 10 addresses into one batch: while (@addresses) { my @batch = splice @addresses, 0, 10; $queue->enqueue(\@batch); }; my @workers = map { my $s = $_; $queue->enqueue(undef); threads->creat +e( sub { my ($ip) = @_; my $server = Net::SMTP->new( $ip ); while (my $batch = $queue->dequeue(1)) { print "Sending to $ip: @$batch\n"; sleep rand 10; # ... send using $server ... }; print "Sending to $ip done\n"; }, $s ) } @servers; for (@workers) { $_->join; }; print "All done.";
      Is there a way to do this without "sleep rand 10" to take the smtp ser +vers in the correct order

        The sleep rand 10 was just for me to simulate sending the emails. You would simply put the sending code there. There is no "correct order" anymore, because you will be sending in parallel.