popen(), sendmail: Success?

Chuck Swiger cswiger at mac.com
Thu Apr 10 20:32:56 EDT 2003


In article <3e95d63f.2635484 at news.muenster.de>, 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?

If you plan on sending any significant amount of mail, it is recommended 
  that your code talk SMTP to port 25 and pipeline all of the messages 
you need to send in one continuous stream.  If you fork a sendmail 
subprocess for each message you send, that will create a wastefully huge 
amount of system load.

This has a nice side-effect: your code becomes more portable since you 
can point to a SMTP server anywhere that is willing to relay your 
messages, not just one on localhost.  Nor does the local host have to 
have /usr/lib/sendmail, meaning your code will run under Windows or 
elsewhere non-Unixlike.

With regard to error handling, sendmail will generate an error return 
value; adding the "-v" flag to your sendmail invocation will show the 
details of the SMTP or LMTP transaction if you want to see what's going 
on for debugging purposes.  I'd assume there is an smtp module that 
would let you see the success or error code and messages from doing 
SMTP, as well.

Good luck,
-Chuck

PS: Note that handing the mail off does not guarantee that the email 
will actually be read by the intended receipient: if that is part of 
your meaning of "the operation succeeded", you might want to generate 
messages which request the user to mail a password/token back or click 
an URL to confirm that the email went through.  See the Mailman mailing 
list program (www.list.org) for examples of "good email handling" practices.







More information about the Python-list mailing list