Sending mail from 'current user' in Python

L

Leo Breebaart

I am writing a utility in Python and I'd like to add a
command-line option "--mailto <address>" that will cause an
e-mail summary to be sent to <address> when the utility finishes
running.

My first thought was to use smtplib.sendmail(), and basically
this works like a charm, except that this function expects a
valid 'sender' address as a parameter.

Every single smtplib example I've found so far shows a hardcoded
'sender' address, but as my utility can be run by any user on any
system I am looking for a way to obtain or create a valid default
value.

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?)

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.

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.

Does anybody here have any thoughts on this?
 
M

Mike Meyer

Leo Breebaart said:
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
 
M

Matt

Mike said:
Leo Breebaart said:
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.

This does work (at least on WinXP Pro, using python 2.3.4):
os.environ["USERNAME"]
'myusername'



<mike
 
G

Grig Gheorghiu

I use this function as a platform-independent way of finding out the
current user name:

def get_username():
if sys.platform == 'win32':
return win32api.GetUserName()
else:
return getpass.getuser()
 
I

Irmen de Jong

Grig said:
I use this function as a platform-independent way of finding out the
current user name:

def get_username():
if sys.platform == 'win32':
return win32api.GetUserName()
else:
return getpass.getuser()


[e:\]python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.'idj'

So why bother with the win32api??

--Irmen
 
G

Grig Gheorghiu

There may have been a reason for the win32 stuff at some point....but I
don't remember and you're right, it does seem like getpass by itself
would do the job.

Grig
 
M

Marcus Alanen

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

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

No, but at least it can be expected to do the right thing w.r.t. sending
the mail.
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?

I'd be very surprised if the typical SMTP server is localhost on
unix-like computers. Rather, sendmail is configured to transport the
message to company/university mailserver(s). If that happens to fail,
the mail is put on the queue at localhost, and transported later (e.g.
via a cronjob) to the server. At no point is there a server on localhost
involved. Of course, not everybody's computer is on such a network and a
sendmail server may indeed be running on localhost, but that's not a
very informed guess. Let the sendmail program take care of those details.

The Unix Programming Frequently Asked Questions "Q5.2 What's the best
way to send mail from a program?" is worth reading.

I'd try some simple autodetection (Mike's suggestion sounded great) and
prompt the user to correct the information, although sendmail itself
ought to give good defaults, so this might not be necessary. Then try
/usr/sbin/sendmail, /usr/libexec/sendmail and /usr/lib/sendmail. You
could try using exitcode 127 to detect "program could not be found or
executed" but I don't know how portable that is.

I can't comment on the Windows side of things.

Regards,
Marcus
 
D

Dennis Lee Bieber

Or LOGNAME. Don't about windows, though.

How old a version of Windows?

WinXP Pro: (I really need to figure out how to change the
HOMEDRIVE/HOMEPATH -- though after yesterday, when APPDATA somehow got
set to the W9x C:\Application Data, making all my applications think
they were new installs -- I should have the info needed...)

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Dennis Lee Bieber>echo %USERNAME%
Wulfraed

C:\Documents and Settings\Dennis Lee Bieber>

{I figured out how to change the account name from the "real name"
whilst figuring out how to clean up the above-mentioned APPDATA
glitch...}
--
 
M

Mike Meyer

Marcus Alanen said:
I'd be very surprised if the typical SMTP server is localhost on
unix-like computers.

On reflection, you're right. It was the way I described when I started
working with Unix, and I still configure all my Unix systems that
way. But recently there's been move to have no servers configured by
default. I've been bit by this, now having to enable TCP on Postgres
by every time I upgrade the server. So most modern Unix systems
probably come out of the box without a local SMTP listener.
Rather, sendmail is configured to transport the message to
company/university mailserver(s).

Sendmail? Don't you mean qmail/postfix/exim?
If that happens to fail,
the mail is put on the queue at localhost, and transported later
(e.g. via a cronjob) to the server. At no point is there a server on
localhost involved. Of course, not everybody's computer is on such a
network and a sendmail server may indeed be running on localhost, but
that's not a very informed guess. Let the sendmail program take care
of those details.

Well, maybe you do mean sendmail. The other MTAs typically aren't that
configurable. qmail, for instance, always queues messages to the local
disk if they are bound for another host.

In lieue of the current discussion, this stuff doesn't matter. All of
these MTAs come with a "sendmail" program designed specifically so
that programs that invoke sendmail to arrange delivery of a message
will continue work if you replace the system sendmail with their
version. On FreeBSD, this has been carried a step further: the system
sendmail is a wrapper that reads /etc/mail/mailer.conf to figure out
what sendmail binary to invoke, so that users don't have to deal with
replacing the system sendmail, or worry about accidently overwriting
the replaced system sendmail if they upgrade their system.

<mike
 
L

Leo Breebaart

Marcus Alanen said:
I've also tried opening a pipe to sendmail, and feeding the
message to that instead. This too works great [but] doesn't
seem like much of an improvement, portability-wise.

No, but at least it can be expected to do the right thing
w.r.t. sending the mail.

Good point.
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?

I'd be very surprised if the typical SMTP server is localhost
on unix-like computers. [...] Let the sendmail program take
care of those details.

The Unix Programming Frequently Asked Questions "Q5.2 What's the best
way to send mail from a program?" is worth reading.

Thanks to you and everyone else who contributed to this thread.

I've now decided to accept the inherent non-portabilitiness of
the whole concept of 'sending mail from within a program', and
will be going with a Unix-only see-if-you-can-find-sendmail
approach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,785
Latest member
undedgini

Latest Threads

Top