Sendmail使用Perl时间和暂停

问题描述 投票:-1回答:2

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

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

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

#!/usr/local/bin/perl
## use: perl send.pl list-email.txt "[email protected]" "subject" html.html

$ARGC = @ARGV;

if ( $ARGC != 4 ) {
    printf "$0 <mailist> <[email protected]> <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;
perl
2个回答
0
投票

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

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

5点后柜台回零

新CODE:

#!/usr/local/bin/perl
## use: perl enviar.pl list-mail.txt "[email protected]" "subject" html.html


$ARGC=@ARGV;

if ($ARGC !=4) {

printf "$0 <mailist> <[email protected]> <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 #####

0
投票

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

**更新 - 完整代码**

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

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

[email protected]
[email protected]
[email protected]
[email protected]

码:

#!/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;
}

附:学习PERL

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.