Sending mail from 'current user' in Python

Marcus Alanen marcus.alanen at abo.fi
Sun Jun 12 07:24:45 EDT 2005


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



More information about the Python-list mailing list