467,169 Members | 981 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

processing email with Python on Windows?

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.
Oct 3 '08 #1
  • viewed: 3648
Share:
3 Replies
On Oct 3, 9:01*am, Beliavsky <beliav...@aol.comwrote:
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.
Python can do it using the PyWin32 package. You'll probably connect to
it with COM or ctypes and hack at it that way. Do a Google search for
Python and Outlook and you'll find various ideas, such as the ones at
this link:

http://mail.python.org/pipermail/pyt...ly/007857.html

I recommend giving it a try and if you get stuck, post to the PyWin32
user's group:

http://mail.python.org/mailman/listinfo/python-win32

Mike
Oct 3 '08 #2
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/
Oct 3 '08 #3
On Oct 3, 4:10*pm, Rob Williscroft <r...@freenet.co.ukwrote:
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
Oct 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by christof hoeke | last post: by
1 post views Thread by webworldL@yahoo.com | last post: by
3 posts views Thread by anthony hornby | last post: by
1 post views Thread by ankit | last post: by
7 posts views Thread by pdr14@comcast.net | last post: by
1 post views Thread by Xah Lee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.