popen(), sendmail: Success?

Martin Bless mb at muenster.de
Sun Apr 13 15:50:47 EDT 2003


Thanks a lot to all who answered. To summarize:

(1) Yes, its all about a cgi environment. Only single mails are being
sent when people submit a form.  Therefore unbearable system load
isn't an issue. Nevertheless I had been using smtplib before and was
quite happy with it. Unfortunately Puretec, a big german provider with
several million accounts changed the rules recently when they moved to
their new and wonderful computing centre. All of a sudden
S=smtplib.SMTP('any.smtp.server') would bring about error 111,
"connection refused". Connecting to 'localhost' is an interesting idea
I hadn't thought of - I'll give it a try. But I'm not very optimistic.

(2) With success I mean: Did sendmail queue my mail without
complaints? From what's being said I conclude that sendmail.close()
provides a yes or no answer and moreover sendmails exit code.
Therefore I'll probably go this direction (untested, modified from
what Carsten Gaebler suggested):

import os, sys

try:
  sendmail = os.popen("/usr/lib/sendmail -t", "w")
  sendmail.write(msg)
  status = sendmail.close()
  done = 1
except:
  done = 0

if not done:
  print "something went wrong:", sys.exc_type, sys.exc_value
elif status:
  exitcode = status >> 8
  signal = status & 0xFF
  print "sendmail exit code %d (killed by signal %d)" % (exitcode,
signal)

Of course I won't have the print statements actually print into the
resulting html page.

(3) Looks like I only have to use popen2.popen3(...) if I want to
retrieve sendmails stdout and stderr streams. Maybe I give it a try
and do some experimentation on what's possible in this specific cgi
environment.

(4) For completeness my original question:

>
>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 ... :-(
>
>Martin
>

bye again,
Martin





More information about the Python-list mailing list