help with parsing email

Ahmed, Shakir shahmed at sfwmd.gov
Mon Aug 18 11:24:07 EDT 2008


Thanks everyone who tried to help me to parse incoming email from an exchange server:

Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve this error and extract emails from an exchange server. 


First I tried:
>>> mailserver = 'EXCHVS01.my.org'
>>> mailuser = 'myname'
>>> mailpasswd = 'mypass'
>>> mailserver = poplib.POP3(mailserver)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python24\lib\poplib.py", line 96, in __init__
    raise socket.error, msg
error: (10061, 'Connection refused')



The other way:

>>> mailserver = 'pop.EXCHVS01.ad.my.org'
>>> mailuser = 'myname at my.org'
>>> mailpasswd = 'mypass'
>>> mailserver = poplib.POP3(mailserver)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python24\lib\poplib.py", line 84, in __init__
    for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
gaierror: (11001, 'getaddrinfo failed')


I tried above ways but getting error

Thanks
sk

-----Original Message-----
From: python-list-bounces+shahmed=sfwmd.gov at python.org [mailto:python-list-bounces+shahmed=sfwmd.gov at python.org] On Behalf Of Gabriel Genellina
Sent: Monday, August 18, 2008 6:43 AM
To: python-list at python.org
Cc: python-win32 at python.org
Subject: Re: help with parsing email

En Thu, 14 Aug 2008 12:50:57 -0300, Ahmed, Shakir <shahmed at sfwmd.gov> escribió:

> I need to grab/parse numeric numbers such as app number from incoming
> emails stored in Microsoft Outlook (Microsoft Exchange server) with
> specified subject line.
>
> The email body is like this
>
> myregion ; tst ; 11-Aug-2008
>
> http://my.xyz.com/dddd/content/ifs/apps/myDocFolder/NoticeOfapplication/080612-21_test_337683.pdf
>
> I need to extract 080612-21_ number from above line from incoming
> emails.

Help from Maric Michaud [maric at aristote.info]

Three options here :

- dealing directly with outlook mailboxes files, I used some open source code 
to parse .pst files in past, but you'll need some googling to match your 
requirements. Doesn't help if there is an instance of outlook running on the 
box.

- use the outlook COM or even some windows MAPI interface via WIN32 API 
(pywin32), can't help with this one.

- access emails directly on the Exchange server via standard python modules 
poplib or imaplib, my preferred choice if one of these protocols are 
supported by your environment. You won't need no extensions, just a standard 
python installation, and your code will work with most mail delivery agent 
and will run on all python supported platform.


==========================
Help from Gabriel Genellina

I can't help with the part dealing with Outlook - but once you retrieved the email body, it's easy:

import re
re_number = re.compile(r"NoticeOfapplication/([0-9-_]+)")
match = re_number.search(body)
if match:
  print match.group(1)

(this matches any numbers plus "-" and "_" after the words NoticeOfapplication written exactly that way)

-- 
Gabriel Genellina

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




More information about the Python-list mailing list