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

Hi Monks,

I've used Mail::Mailer a bit with great success, but there's one thing that irks me about it. Creating a new mailer thus (from the docs):

$mailer = new Mail::Mailer 'smtp', Server => $server;

Means that I always have to hardcode a value for $server in my script. Not portable!

My question:

Is there a default value for server, possibly in an environment variable somewhere, that I can rely on to fill $server on its own (assuming the machine is properly configured)?

I'm running SunOS 5.7, but expect to be moving to other *nix OSes soon, fyi.

Thanks,

Chris

Replies are listed 'Best First'.
Re: Mail::Mailer and default value of $server?
by footpad (Abbot) on Jan 10, 2002 at 21:26 UTC

    I suppose you could try localhost, but that could be dangerous. In my experience, it's best to be as clear as possible.

    It might be wiser to store the name of your server in a private configuration file and then load that into a scalar before calling Mail::Mailer. I've used that approach before and it makes things easier to maintain in my book. (YMMV)

    You may find AppConfig (alt.) helpful.

    --f

Re: Mail::Mailer and default value of $server?
by vek (Prior) on Jan 10, 2002 at 21:21 UTC
    Or just put the value of $server in a config file and read the file. Can be ported anywhere if you do that.
      No doubt. That's my 2nd choice. Chris
Re: Mail::Mailer and default value of $server?
by KILNA (Acolyte) on Jan 11, 2002 at 01:27 UTC
    $server = $ENV{'MAIL_SERVER'};
Re: Mail::Mailer and default value of $server?
by Rich36 (Chaplain) on Jan 10, 2002 at 22:18 UTC
    Are you always going to be using the current server where the script is running as the smtp server? Or I am misunderstanding this?
    If so, you can also get a value for the current hostname using the Sys::Hostname module. This should work for any *nix hostname.
    #!/usr/bin/perl -w use strict; use Sys::Hostname; my $server = hostname; print "Hostname is $server\n";

    Rich36
    There's more than one way to screw it up...