Sendmail使用Perl时间和暂停

时间:2014-09-18 19:21:04

标签: perl

我有以下代码e完美无缺,但是,我想设置发送每封电子邮件的时间。

示例:发送100封电子邮件,PAUSE脚本发送1小时,然后再发送100封电子邮件。

这里的代码直接发送。我需要让2工作,并根据txt列表慢慢发送电子邮件。

#!/usr/local/bin/perl
## use: perl send.pl list-email.txt "noreply@mail.com" "subject" html.html

$ARGC = @ARGV;

if ( $ARGC != 4 ) {
    printf "$0 <mailist> <tes@test.com> <HELLO friend> <html.htm>\n\n";
    #printf "Script for sending emails";
    exit(1);
}

$mailtype = "content-type: text/html";
$sendmail = '/usr/sbin/sendmail';
$sender   = $ARGV[1];
$subject  = $ARGV[2];
$efile    = $ARGV[0];
$emar     = $ARGV[0];
$count    = 1;
open( FOO, $ARGV[3] );
@foo = <FOO>;
$corpo = join( "\n", @foo );

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

while (<BANDFIT>) {
    ( $ID, $options ) = split( /\|/, $_ );
    chop($options);

    foreach ($ID) {
        $recipient = $ID;

        open( SENDMAIL, "| $sendmail -t" );
        print SENDMAIL "$mailtype\n";
        print SENDMAIL "Subject: $subject\n";
        print SENDMAIL "From: $sender\n";
        print SENDMAIL "To: $recipient\n\n";
        print SENDMAIL "$corpo\n\n";
        close(SENDMAIL);

        printf "Enviado para $recipient [ OK $count ]";
        $count++;
    }
}
close(BANDFIT);

===============其他代码/时间暂停===============

#!/usr/bin/env perl
sub mostraMensagem() {
    while (1) {
        sleep(1);
        print("Hello World!\n");
        $count++;

        if ( $count == 5 ) {
            print("PAUSE!\n");
            $count = 0;
            sleep(5);
            print("CONTINUE..\n");
            mostraMensagem;
        }
    }
}

mostraMensagem;

2 个答案:

答案 0 :(得分:0)

基本上,您会计算发送的电子邮件数量,当您达到100分时,暂停3600秒然后继续。

** UDPATE - 完整代码**

在RHEL 5上测试(较小的数字)

假设email-list.txt看起来像:

user_1_@company.com
user_2_@company.com
user_99_@company.com
user_100_@company.com

代码:

#!/usr/bin/perl

# =========================
# Assign $ARGV[x] -> var
# =========================
if (@ARGV < 4){ usage() }

my $sendAs          =   $ARGV[1];
my $subject         =   $ARGV[2];
my $htmlFile        =   $ARGV[3];
my $sendList        =   $ARGV[0];

# =========================
# Get Send List -> var
# =========================
open(LIST, $sendList) || die "Could not open $sendList: $!\n";
    my @recipients = <LIST>;
close(LIST);

# =========================
# Iterate / Send Email
# =========================
my $count = 1;

foreach my $recipient (@recipients)
{
    chomp($recipient);

    if  ( $count < 100 )
    {
        my $cmd = 'cat ' . $htmlFile . ' | /usr/sbin/sendmail -s "$(echo -e "' . $subject . '\nContent-Type: text/html")" ' . $recipient . ' -v -- -F ' . $sendAs;
        my $results = `$cmd`;
    }
    elsif ( $count == 100 )
    {
        my $cmd = 'cat ' . $htmlFile . ' | /usr/sbin/sendmail -s "$(echo -e "' . $subject . '\nContent-Type: text/html")" ' . $recipient . '  -- -F ' . $sendAs;
        my $results = `$cmd`;

        sleep(3600);
        $count = 0;
    }
    $count++;
}

# =========================
# Essential Subroutines
# =========================
sub usage()
{
    print "\nUsage:\n\t$0 <mailist.txt> <test\@mail.com> <\"Hello friend\"> <test.html>\n\n";
    exit;
}

<强> P.S。学习PERL

答案 1 :(得分:0)

我有朋友!但还是需要你......

它发送5封电子邮件并暂停5秒钟,但是,计数不会继续,它会返回零。我们能做什么?

5点后计数器回零。

新代码:

#!/usr/local/bin/perl
## use: perl enviar.pl list-mail.txt "my@mail.com" "subject" html.html


$ARGC=@ARGV;

if ($ARGC !=4) {

printf "$0 <mailist> <my@myemail.com> <subject> <msg.htm>\n\n";

#printf "Script sending emails";

  exit(1);

} 


$mailtype = "content-type: text/html";

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

$sender = $ARGV[1];
$subject = $ARGV[2];
$efile = $ARGV[0];
$emar = $ARGV[0];
$count=1;

open(FOO, $ARGV[3]); 
@foo = <FOO>;

$corpo = join("\n", @foo);
open (BANDFIT, "$emar") || die "Can't Open $emar";

while(<BANDFIT>)        {


($ID,

$options) = split(/\|/,$_);

chop($options);

 foreach ($ID) {

$recipient = $ID;
## this changes =>>> ###
### send 5 email of list.txt, and pause 5 seconds, continue.. ### 

if ( $count == 5 ) {

           print("PAUSE!\n");
           $count = 0;
          sleep(5);
          print("CONTINUE..\n");
}

open (SENDMAIL, "| $sendmail -t");
print SENDMAIL "$mailtype\n";
print SENDMAIL "Subject: $subject\n";
print SENDMAIL "From: $sender\n";
print SENDMAIL "To: $recipient\n\n";
print SENDMAIL "$corpo\n\n";
close (SENDMAIL);

printf "sending for $recipient [ Ok Send; $count ]";
$count++;
}

} 
close(BANDFIT);

#### end #####