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 ;

In reply to Capture smtp mailserver that responds to hello. by shadowfox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.