popen(), sendmail: Success?

Adam Hupp hupp at cs.wisc.edu
Thu Apr 10 23:44:27 CEST 2003


On Thu, Apr 10, 2003 at 08:57:17PM +0000, Martin Bless wrote:
> 
> To send mails from Python here's the example my provider provides:
> This http://faq.puretec.de/skripte/python/3.html
> 
> The relevant lines are these:
> 
> sendmail = os.popen("/usr/lib/sendmail -t", "w")
> sendmail.write(msg)
> sendmail.close()
> 
> This works just fine. But is it enough? How do I know the operation
> succeeded?
> 
> I've read the docs about os.popen[234] and the module popen2 but I'm
> not sure where to go. And since experimentation isn't easy in this
> case I'd appreciate some hints or a concrete example very much.
> 
> Do I have to use popen2.popen3() and its wait() method? I have to
> admit that I don't understand too much of what's being said about this
> in the docs ... :-(

Basic popen should be fine.  To check for sucess use 

if sendmail.close():
   # Handle error....

since .close() returns None on sucess, otherwise returns a wait() type
error.  You might want to use smtplib instead though.  I don't know
what docs you were looking at but
http://www.python.org/doc/current/lib/os-newstreams.html was fairly
clear.

-Adam





More information about the Python-list mailing list