Monitoring number of smtp bytes sent through python e-mail socket

Jean-Paul Calderone exarkun at divmod.com
Mon Dec 4 20:34:17 EST 2006


On Tue, 05 Dec 2006 12:18:08 +1100, William Connery <wconnery at dodo.com.au> wrote:
>Hi,
>
>I have a small python program with e-mail capabilities that I have
>pieced together from code snippets found on the internet.
>
>The program uses the smtplib module to successfully send an e-mail with
>an attachment.
>
>I want to give users an indication of the percentage of the e-mail that
>has already been sent so as to avoid frustration when dealing with large
>attachments or a slow smtp server. But the smtplib module doesn't seem
>to provide access to the number of bytes that have already been sent.
>
>Can anyone help, or provide a working example of sending an e-mail
>attachment using basic python sockets whilst having access to the number
>of bytes that have already been sent through the socket?

You could probably hack something up with smtplib, but it's a lot easier
to do this with Twisted's SMTP client.  Twisted's sendmail function takes
a file-like object instead of a string.  You can wrap a real file and
present a read method which keeps track of how many bytes have been read
and updates some UI with this information.

You can find a simple example of using Twisted's SMTP client here:

http://twistedmatrix.com/trac/browser/trunk/doc/mail/examples/smtpclient_tls.py

Jean-Paul



More information about the Python-list mailing list