Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Net::SMTP isConnected method

by larryk (Friar)
on Jan 22, 2004 at 11:18 UTC ( #323149=snippet: print w/replies, xml ) Need Help??
Description: We've all been there - while (1) client process needs to send an email but it fails because it hasn't sent one since yesterday and the server dropped the connection. You don't want to make a new connection every time, so how do you check if your object is still connected? Use the isConnected method of course; oh, except that it doesn't exist... or does it? See below for the undocumented and pseudo-private _NOOP instance method - it is part of the SMTP RFC and thanks to Mr. Barr's attention to detail it is available without having to hack through Net::SMTP's parent classes to issue the command yourself.
use strict;
use warnings;

use MIME::Lite;
use Net::SMTP;

our $smtp; # "self-healing" SMTP connection

sub smtp_conn {
    my $server = shift;
    if (!defined $smtp or !$smtp->_NOOP) { # there she is ;)
        $smtp = Net::SMTP->new($server);
    return $smtp;

sub send_email {
    my($server,$from,$to,$subject,$body) = @_;
    my $smtp = smtp_conn($server) or return;
    my $msg = MIME::Lite->new(
        To      => $to,
        From    => $from,
        Subject => $subject,
        Data    => $body);
    return $smtp->data($msg->as_string);

while (1) {
    if ($some_condition eq 'met') {
    sleep 60;
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://323149]
[moritz]: I had an office move too, two floors up
[moritz]: so far I've managed not to get into my old office by accident, but once it was close (already stopped at the wrong floor)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-10-17 09:00 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (224 votes). Check out past polls.