Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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;
Comment on Net::SMTP isConnected method
Download Code

Back to Snippets Section

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://323149]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2015-11-30 01:10 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (755 votes), past polls