http://www.perlmonks.org?node_id=951463

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

We have a primary mailserver address that is actually 3 mailservers working in failover, so if one is down it goes to the next, etc. The problem is that it's not entirely working as intended all the time so I'd like my scripts to have a seperate backup mail server incase the first one can't be contacted. I think I've got that down, but I'd like to log which mail server actually responded after I sent my hello command and save in my logs. I thought it was suppose to be domain() but its not looking to be the case?

Error: Can't locate object method "domain" via package "smtp" (perhaps you forgot to load "smtp"?)
use strict ; use Net::SMTP ; use warnings ; use Sys::Hostname ; my $SRV = hostname ; my $MAILSRV1 = "smtp1.domain.com" ; # actually smtp1.domain.com, smtp2 +.domain.com, smtp3.domain.com my $MAILSRV2 = "backup.domain.com" ; # the backup just incase. my $smtp = Net::SMTP->new($MAILSRV1, Hello => $SRV, Timeout => 30,Debu +g => 1) or print "$! \n"; if(!defined($smtp) || !($smtp)) { print "SMTP ERROR: Connection timeout\n"; my $smtp = Net::SMTP->new($MAILSRV2, Hello => $SRV, Timeou +t => 30,Debug => 1) or print "$! \n"; print "Trying backup email server to send email\n"; } else{ print "Using primary email server to send email\n" ; } $smtp->mail("sender\@domain.com") ; my @emails = ("reciepiant1\@domain.com", "reciepiant2\@domain.com") ; my $subject = "test subject"; my $content ="test data \n") ; my $whoareu = smtp->domain() ; $smtp->to(@emails) ; $smtp->recipient(@emails) ; $smtp->data() ; $smtp->datasend("Priority: Urgent\n") ; $smtp->datasend("Importance: high\n") ; $smtp->datasend("TO: @emails\n") ; $smtp->datasend("Subject: $subject\n") ; $smtp->datasend("$content\n") ; $smtp->datasend("$whoareu\n") ; $smtp->dataend() ; $smtp->quit ; print "The mail server that responded was $whoareu \n" ; exit ;

Replies are listed 'Best First'.
Re: Capture smtp mailserver that responds to hello.
by marto (Cardinal) on Feb 02, 2012 at 15:21 UTC
      Yep, that was it. I got too caught up looking for another way to do it I missed the most basic of problems.

      Thanks!