Sending mail from 'current user' in Python

Leo Breebaart leo at lspace.org
Fri Jun 10 03:56:55 EDT 2005


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  <leo at lspace.org>



More information about the Python-list mailing list