By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Sending mail from 'current user' in Python

P: n/a
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?

--
Leo Breebaart <le*@lspace.org>
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Leo Breebaart <le*@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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #2

P: n/a


Mike Meyer wrote:
Leo Breebaart <le*@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.


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

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


Jul 19 '05 #3

P: n/a
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()

Jul 19 '05 #4

P: n/a
Grig Gheorghiu wrote:
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.
import getpass
getpass.getuser()

'idj'

So why bother with the win32api??

--Irmen
Jul 19 '05 #5

P: n/a
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

Jul 19 '05 #6

P: n/a
Mike Meyer wrote:
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.
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.


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


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
Jul 19 '05 #7

P: n/a
On Sun, 12 Jun 2005 14:24:45 +0300, Marcus Alanen <ma***********@abo.fi>
declaimed the following in comp.lang.python:
Mike Meyer wrote:
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.


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...}
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #8

P: n/a
Marcus Alanen <ma***********@abo.fi> writes:
Mike Meyer wrote:
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?

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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #9

P: n/a
Marcus Alanen <ma***********@abo.fi> writes:
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.
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?


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.

--
Leo Breebaart <le*@kronto.org>
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.