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

I recently wrote a program that needs to send messages through SMTP every few minutes. Since I'm running this behind an Earthlink DSL modem, I can only connect on port 25 if I'm connecting to the Earthlink STMPAUTH server. (Damn them and their restrictions!).

Anyway, I wrote a script that sends the mail and it works perfectly. The problem is that if the message gets sent twice from the same script, I get an error and the mail doesn't go through. I figured it might have been the rest of my code, so I isolated the mailing sub and tried again. No dice. Why would this happen?

Here's my code:

#!/usr/bin/perl use Net::SMTP; use strict; use warnings; for(1..2) { my $smtp = Net::SMTP->new('smtpauth.earthlink.net', Hello => 'milkbone.org', Debug => 1) or die +"death"; $smtp->auth('batkins86@earthlink.net', '***************************'); $smtp->mail('monitor@milkbone.org'); $smtp->to('test@batkins.com'); $smtp->data(); $smtp->datasend("To: postmaster\n"); $smtp->datasend("\n"); $smtp->datasend("A simple test message\n"); $smtp->dataend(); $smtp->quit; }
Of course, the password is blanked out. The first time this goes through perfectly. But the second time, the tail end of the Debug output reads:
Net::SMTP=GLOB(0x81b38e0)<<< 250-puffin.mail.pas.earthlink.net Hello n +ode-423a0c29.lga.onnet.us.uu.net [66.58.12.41] Net::SMTP=GLOB(0x81b38e0)<<< 250-SIZE 10485760 Net::SMTP=GLOB(0x81b38e0)<<< 250-PIPELINING Net::SMTP=GLOB(0x81b38e0)<<< 250-AUTH PLAIN LOGIN CRAM-MD5 Net::SMTP=GLOB(0x81b38e0)<<< 250-STARTTLS Net::SMTP=GLOB(0x81b38e0)<<< 250 HELP Can't locate object method "_secflags" via package "Authen::SASL::Perl +::CRAM_MD5" at /usr/lib/perl5/site_perl/5.8.0/Authen/SASL/Perl.pm lin +e 32.
I'm stumped. Any ideas?
milkbone - perl/tk instant messaging - it's the only way to fly

You know anyone who'll debug two million lines of code for what I get this job?
- Dennis Nedry

Replies are listed 'Best First'.
Re: Sending two messages thru an smtp server with authentication
by Mr. Muskrat (Canon) on Jul 11, 2003 at 15:56 UTC

    I'm not sure why it's happening but it just makes more sense to me to try and send two messages while only authenticating once.

    Untested code follows:

    #!/usr/bin/perl use Net::SMTP; use strict; use warnings; my $smtp = Net::SMTP->new('smtpauth.earthlink.net', Hello => 'milkbone.org', Debug => 1) or die +"death"; $smtp->auth('batkins86@earthlink.net', '***************************'); $smtp->mail('monitor@milkbone.org'); for(1..2) { $smtp->to('test@batkins.com'); $smtp->data(); $smtp->datasend("To: postmaster\n"); $smtp->datasend("\n"); $smtp->datasend("A simple test message\n"); $smtp->dataend(); } $smtp->quit;

      The problem is that the messages aren't sent out constantly in the real app. It could be five hours between each send, so the SMTP server would boot me by then.
      milkbone - perl/tk instant messaging - it's the only way to fly

      You know anyone who'll debug two million lines of code for what I get this job?
      - Dennis Nedry

        So what happens if you put a sleep statement at the end of the loop? Something like:
        #!/usr/bin/perl use Net::SMTP; use strict; use warnings; for(1..2) { my $smtp = Net::SMTP->new('smtpauth.earthlink.net', Hello => 'milkbone.org', Debug => 1) or die +"death"; $smtp->auth('batkins86@earthlink.net', '***************************' +); $smtp->mail('monitor@milkbone.org'); $smtp->to('test@batkins.com'); $smtp->data(); $smtp->datasend("To: postmaster\n"); $smtp->datasend("\n"); $smtp->datasend("A simple test message\n"); $smtp->dataend(); $smtp->quit; sleep(5); }

        Perhaps Net::SMTP hasn't fully cleaned up before you call it again? Don't think it would matter but I'm trying to cover all the bases...

Re: Sending two messages thru an smtp server with authentication
by Mr. Muskrat (Canon) on Jul 11, 2003 at 16:57 UTC
    Does the debugging output look the same for both messages (at least up until the error)?
      Yep. What I ended up doing was going into Authen::SASL::Perl and removing the call to _secflags. It's a horrible hack, but it appears to work - I can now send two messages to myself from the same script. I suppose I should report this to the maintainer of Authen::SASL::Perl.

      ++ for your help though.


      milkbone - perl/tk instant messaging - it's the only way to fly

      You know anyone who'll debug two million lines of code for what I get this job?
      - Dennis Nedry