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

My question has to do with connecting to an smtp host. Most email modules like Mail::Sender or Mail::Bulkmail require knowing the smtp machine name. In my case I want to use the recipient's smtp machine when doing a mailing. For example, sending email to peamasii@hotmail.com, I would connect to mail.hotmail.com as the smtp.

I need to know how to find out the smtp host name for each domain in my list (sometimes, it's mail.domain.com, other times it's smtp.domain.com, etc.). Does anyone have some code that would do this? Most POP servers do this internally but I have no idea how (the POP RFC didn't help me).

Replies are listed 'Best First'.
Re: How to determine smtp host on a domain
by Kanji (Parson) on Feb 19, 2002 at 02:15 UTC

    As blakem points out, you usually want to use a local SMTP server and let it worry about getting the mail to the correct place, but if you want to do this yourself you need to employ DNS; specifically looking up of MX records, which -- as luck would have it --Net::DNS's (alt.) mx method can help ...

    use Net::DNS; my @mx = mx("perlmonk.org");

    For more info, see RFC 2821: Simple Mail Transfer Protocol (specifically Section 5).

        --k.


Re: How to determine smtp host on a domain
by blakem (Monsignor) on Feb 19, 2002 at 02:08 UTC
    From the documentation of Mail::Sender :
    smtp = the IP or domain address of your SMTP (mail) server
           This is the name of your LOCAL mail server, do not
           try to guess and contact directly the adressee's
           mailserver!
    

    -Blake

Re: How to determine smtp host on a domain
by gav^ (Curate) on Feb 19, 2002 at 02:11 UTC
    You'll need to look up the MX record for the host. Net::DNS will do the trick. I'm not 100% sure why you'd want to though, it's much easier setting up a mail server (postfix for example) and relaying through it.

    gav^

Re: How to determine smtp host on a domain
by beebware (Pilgrim) on Feb 27, 2002 at 20:27 UTC
    Why don't you just grab something like Sendmail/Qmail or something like that and run a mail server on your server? That way, you won't have to mess around with contacting remote servers and working out which one to send to via Perl (Sendmail et al will do the hard work for you and they've been heavily tested, understand the possibly complexities of MX records (what happens if you have records 4,5,5 and 10 but host '4' is unavailable... etc etc) and they are dedicated programs for sending email)...