Sending mail from 'current user' in Python

Mike Meyer mwm at mired.org
Fri Jun 10 19:13:30 EDT 2005


Leo Breebaart <leo at lspace.org> writes:

> I can get the username info (at least on Unix) via the 'pwd'
> module, but that still leaves me with the domainname, or rather
> the mailname, and I have not been able to spot a way of finding
> that from within Python. (I could try opening /etc/mailname by
> hand, but how standard is that filename/location?)

Not very. Certainly doesn't exist on FreeBSD, and doesn't appear
anywhere in the sources for my UMA.

BTW, an alternative for the username is the USER environment
variable. I don't know whether or not it exists on Windows.

> I've also tried opening a pipe to sendmail, and feeding the
> message to that instead. This too works great (and does give an
> appropriate default 'From'), but that also turns my problem into
> the problem of finding the location of the sendmail program,
> which doesn't seem like much of an improvement, portability-wise.

Well, you could provide a list of places to look for it. But you're
right, this doesn't help much with portability.

> Finally, if at all possible I'd also like to get this working on
> Windows, so I'd rather stick with the standard smtplib if I can.

smtplib needs an SMTP server to connect to. For unix systems, this is
typically localhost. What do you use for Windows systems? Or are you
connecting to your machine to deliver the mail?

The problem with getting this working on Windows is that, IIRC, the
information you need is configured in the UMA, and not in some
system-wide location, at least on some versions of Windows, and it
typically is unrelated to the name of the Windows box you're on.

Given those constraints, the simple solution is probably to ask the
user for the information you need.

Failing that, and assuming os.env['USER'] exists on windows, you could
try:

        1) Looking up the IP address of the host in question. Not the
           interface - you need the address the outside world sees.
           See <URL: http://www.whatismyip.com/ > for example.
        2) Do a reverse DNS lookup on that ip address to get a host name.
           If they've got a dynamic IP address, this will probably be
           something ugly, if you get anything at all.
        3) Start doing MX lookups on that host name, stripping off one
           domain level at a time from the left until you get an address
           with an MX record, or you've got nothing left. This assumes
           that an MX record will exist for the part of the domain name
           which get mail - which may not be true.

This entire procedure also assumes that the user reads mail using
their ISP-provided maildrop, which may not be true.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list