insert perl timer

Question:

I need to put a timer in this perl below:

It works by firing emails in a row to the end of the email list.

Example if the list has 1 thousand emails it fires email by email until it runs out.

I would like to put a timer on this code.

EX: trigger 500 emails every 30 seconds. It would be possible ? I thank the attention.

Below is the code or at https://pastebin.com/nj8bYyZ8

#!/usr/local/bin/perl
#by IV ' lost
use MIME::Base64;
$ARGC=@ARGV;

if ($ARGC !=4) {
  printf "$0 <mailist> <remetente> <assunto> <fake.htm>\n\n";
 exit(1);
}

$sendmail = '/usr/sbin/sendmail';

$sender = $ARGV[1];

$subject = $ARGV[2];

$efile = $ARGV[0];

$emar = $ARGV[0];

open(FOO, $ARGV[3]);

@foo = <FOO>;

open (BANDFIT, "$emar") || die "Can't Open $emar";

$cont=0;

while(<BANDFIT>)        {
($ID,
 $options) = split(/\|/,$_);
chop($options);
  foreach ($ID) {
$r2 = int(rand(99));
$r3 = int(rand(999));
$r4 = int(rand(9999));
$r5 = int(rand(99999));
$r6 = int(rand(999999));
$r7 = int(rand(9999999));
$r8 = int(rand(99999999));
$r9 = int(rand(999999999));
$recipient = $ID;
$corpox = join("\n", @foo);
$corpox =~ s/%email%/$ID/g;
$corpox =~ s/%rand1%/$r1/g;
$corpox =~ s/%rand2%/$r2/g;
$corpox =~ s/%rand3%/$r3/g;
$corpox =~ s/%rand4%/$r4/g;
$corpox =~ s/%rand5%/$r5/g;
$corpox =~ s/%rand6%/$r6/g;
$corpox =~ s/%rand7%/$r7/g;
$corpox =~ s/%rand8%/$r8/g;
$corpox =~ s/%rand9%/$r9/g;
$corpo = encode_base64($corpox);

$subject =~ s/%email%/$ID/g;
$subject =~ s/%rand1%/$r1/g;
$subject =~ s/%rand2%/$r2/g;
$subject =~ s/%rand3%/$r3/g;
$subject =~ s/%rand4%/$r4/g;
$subject =~ s/%rand5%/$r5/g;
$subject =~ s/%rand6%/$r6/g;
$subject =~ s/%rand7%/$r7/g;
$subject =~ s/%rand8%/$r8/g;
$subject =~ s/%rand9%/$r9/g;

$sender =~ s/%rand4%/$r4/g;

open (SENDMAIL, "| $sendmail -t");
print SENDMAIL "MIME-Version: 1.0\n";
print SENDMAIL "Content-type:  text/html; charset=UTF-8\n";
print SENDMAIL "Content-Transfer-Encoding: base64\n";
#print SENDMAIL "$mailtype\n";
print SENDMAIL "Subject: $subject\n";
print SENDMAIL "From: $sender\n";
print SENDMAIL "To: $recipient\n";
print SENDMAIL "$corpo\n\n";
close (SENDMAIL);

$cont=$cont+1;
printf "$cont * Enviado para > $recipient";

}

}

close(BANDFIT);

Answer:

Miguel, If I understand correctly, do you intend to take breaks, (so as not to saturate the system?).

If so, try something like attaching a little sleep(x) to each send (where x is in seconds),

#...ciclo de envio{
# ...enviar mail
       sleep(2);
#...}

or a big sleep at the end of every 500

#...ciclo de envio
#...  enviar mail
      $cont=$cont+1;
      printf "$cont * Enviado para > $recipient";
      sleep(30) if $cont % 500 == 0;
#...

Finally, consider using Mail :: Sender

 use Mail::Sender;
 $sender = new Mail::Sender {smtp => 'mail.yourdom.com', from => 'tu@address.br'};
 $sender->MailFile({to => 'some@address.com',
      subject => 'Here is the file',
      msg => "I'm sending you the list you wanted.",
      file => 'filename.txt'});
Scroll to Top