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

Hello:

I am creating an automated Mail Manager called automailer.pl that use MySQL and perl modules. I am using MIME:: Lite, Net::SMTP and of course, DBI. The script will run fine for about 40 or 50 emails, but eventually, I will get this crazy error and hte script will fail:

Exception caught: Failed to connect to mail server: Bad file descriptor at automailer.pl line 219

Could not commicate with "mailserver". Connection refused at automailer.pl line 182, <STDIN> line 3.

The code that sends the emails is here:

#### Subroutine for sending email message: #### Subroutine for sending email message: sub mailfunc { my($to, $from, $subject, @body) = @_; use MIME::Lite; use Net::SMTP; my $relay = "gorillatrades.com"; ## Mail Server. my $smtp = Net::SMTP->new($relay); ## Mail Object Handler. die "Could not commicate with $relay. $!" if (!defined $smtp); my @images; ## Stores Images references; my $message = join(' ', @body),"\n"; # Create the initial text of the message my $mime_msg = MIME::Lite->new( From => $from, To => $to, Subject => $subject, Type => "multipart/mixed", ) or die "Error creating MIME body: $!\n"; ##Find all image references ##Converting MIME type into String: #print "$message"; #HTML $mime_msg-> attach(Type => 'text/html', Data => "$message"); my $message_body = $mime_msg-> body_as_string(); #### Send out Email. ## Second Send Message Using ONLY MIME MIME::Lite->send('smtp', $mailserver, Timeout=>60); eval { $mime_msg-> send(); }; $@ and warn "Exception caught: $@\n\n"; print "Sent Normally to $to.\n\n"; $@ and warn "Exception caught: $@\n\n"; }

The failure happens sporadically. It will work fine for 200 emails and then the next day it will fail on 20.
Is there some bug in MIME or Net that I don't know about? could there be something else going on?

- Cappa

Janitored by holli - added code tags

Replies are listed 'Best First'.
Re: MIME Lite Module constantly crashing
by zentara (Cardinal) on Jun 14, 2005 at 12:34 UTC
    Maybe your mailserver has some limit on sending to prevent excessive bulkmailling? Maybe slow down your script or ask the sysadmin if you are allowed unlimited mail sending? Maybe their counting algorithm isn't exact and each time you run it, a different number of mails get through before they detect you.

    I'm not really a human, but I play one on earth. flash japh
      I added a test to slow down the script (pausing every 200 messages). I have also increased the timeout to 300 seconds (5 minutes.)I'll see what happens.
Re: MIME Lite Module constantly crashing
by fireartist (Chaplain) on Jun 14, 2005 at 12:13 UTC

    Why is the error message "crazy"?

    Could not commicate with "mailserver". Connection refused
    It appears to be a problem with your SMTP server. Have you monitored it to check it's waiting for the full 60 second timeout? Try increasing the timeout. Try increasing the time between running the program.

    update: minor change (submitted instead of previewing)

      Its still failing....................ARRGGG!!! This makes no sense. I have done every means to check server to ensure that it connects and it still fails. I've several checks and fail safes. The system still fails and I am completely out of ideas. I'm close to losing a client over this bug!!! SOS!!! - Cappa

        Well, if you've done "every means to check", then there's not really anything left to try, is there?.

        I'd suggest checking the smtp server's logs if you have access to them, to see if it's refusing the connection. Otherwise try testing with a different smtp server.

Re: MIME Lite Module constantly crashing
by cowboy (Friar) on Jun 14, 2005 at 15:04 UTC

    This most likely has nothing to do with perl. sendmail will start refusing connections once the system gets to a certain load average (configurable) which is probably what is happening.

      In my case it had nothing to do with the system load
      Dodge This!
Re: MIME Lite Module constantly crashing
by Ultra (Hermit) on Jun 14, 2005 at 13:55 UTC
    I've seen this too with MIME::Lite, no NET::SMTP, on a sendmail server using a milter plugin....
    Hope this helps

    Update:

    This always happened to me after restarting the milter plugin
    The problem "fixed" when restarting the sendmail daemon too. Are you using sendmail or other MTA?
    Dodge This!