processing email with Python on Windows?

Mike Driscoll kyosohma at gmail.com
Fri Oct 3 17:27:33 EDT 2008


On Oct 3, 4:10 pm, Rob Williscroft <r... at freenet.co.uk> wrote:
> Beliavsky wrote in news:d579f554-be4b-4066-acec-49a7bafb1046
> @t41g2000hsc.googlegroups.com in comp.lang.python:
>
> > I work for a financial company where we run Windows XP and read email
> > using Microsoft Outlook 2003. I get daily files that come as email
> > attachments from various counterparties. I save them as h:\firm_name
> > \yyyymmdd.csv . Would Python be a good tool to automate the process of
> > saving reports, or would it be more convenient to use a Microsoft
> > proprietary language such as VB or C#? Of course one factor is one's
> > relative competence with the various languages.
>
> Assuming your Outlook is using Exchange (or at least a IMAP server),
> you can use imaplib in the standard library.
>
> This example should list the messages and attachments in you InBox
> fot today.
>
> EXCHANGE = '' #<-- YOUR EXCHANGE SERVER HERE
> EXCHANGE_PORT = 143 # default
> USER = '' #<-- YOUR USERNAME
> PWD ='' #<-- YOUR PASSWORD
>
> import imaplib, email
> from datetime import date
>
> today = date.today().strftime( '"%d-%b-%Y"' )
>
> imap = imaplib.IMAP4( EXCHANGE, EXCHANGE_PORT )
> imap.login( USER, PWD )
> imap.select( 'InBox' )
>
> typ, data = imap.search( None, 'SINCE', today )
> for num in data[0].split():
>   typ, data = imap.fetch(num, '(RFC822)')
>   msg = email.message_from_string(data[0][1])
>
>   print ( "%s, %s\n" % ( num, msg['subject'] ) )
>   for part in msg.walk():
>     if part.get_filename() is not None:
>       print ( "  %s\n" % part.get_filename() )
>
> imap.close()
> imap.logout()
>
> Rob.
> --http://www.victim-prime.dsl.pipex.com/

As I understand it, Exchange has to be configured specifically to
allow IMAP and POP3, so this method may not work unless his admin has
allowed it. Good idea though!

Mike



More information about the Python-list mailing list