processing email with Python on Windows?

Rob Williscroft rtw at freenet.co.uk
Fri Oct 3 17:10:21 EDT 2008


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/



More information about the Python-list mailing list